Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(biblio-ref): update output of service #40

Merged
merged 3 commits into from
Feb 2, 2024

Conversation

leogail
Copy link
Collaborator

@leogail leogail commented Feb 1, 2024

now service return the doi if found AND a single key "status" (instead of retracted + found) but the value's type is no more boolean.
Example of output :

  • "error_data" (data not string),
  • "found",
  • "not_found" (no doi or doi doesn't match) ,
  • "error_service" (when response from crossref api isn't a 200 or 404)

@leogail
Copy link
Collaborator Author

leogail commented Feb 1, 2024

The output (in "value") is an object

{"doi":"...", "status":"..."} 

But I don't know if status is the correct word.
I want to return if a publication is correct, not_found or retracted.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought (non-blocking): Memoization ?

The commit message made me think that you used a memoize function, which is more advanced way of not calling twice a function with the same parameter.

Here is a Google Bard advice:

Il existe plusieurs façons de memoizer une fonction en Python :

1. functools.lru_cache:

Le module functools propose un décorateur lru_cache qui est un outil simple et efficace pour memoizer des fonctions. Il met en cache les résultats des appels de fonction en fonction de leurs arguments et les renvoie directement lors d'appels ultérieurs avec les mêmes arguments.

Exemple :

from functools import lru_cache

@lru_cache(maxsize=None)
def factorielle(n):
    if n < 2:
        return 1
    return n * factorielle(n-1)

print(factorielle(5))  # Affiche 120
print(factorielle(5))  # Affiche 120 (résultat récupéré du cache)

2. @wraps et un dictionnaire:

Vous pouvez également créer votre propre décorateur memoize en utilisant un dictionnaire pour stocker les résultats mis en cache. Le décorateur utilise @wraps pour préserver le nom et la docstring de la fonction d'origine.

Exemple :

def memoize(func):
    cache = {}
    @wraps(func)
    def wrapper(*args, **kwargs):
        key = (args, json.dumps(kwargs, sort_keys=True))
        if key not in cache:
            cache[key] = func(*args, **kwargs)
        return cache[key]
    return wrapper

@memoize
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))  # Affiche 55
print(fibonacci(10))  # Affiche 55 (résultat récupéré du cache)

3. Modules externes:

Il existe également des modules externes comme functools_lru_cache et cachetools qui proposent des fonctionnalités de memoization plus avancées, telles que la gestion de l'expiration des éléments du cache et la prise en charge de différents types de clés.

Points importants à retenir :

  • La memoization peut améliorer considérablement les performances des fonctions coûteuses en temps, mais elle peut également augmenter la consommation de mémoire.
  • Il est important de choisir la méthode de memoization appropriée en fonction de vos besoins spécifiques.
  • Assurez-vous que les arguments de la fonction sont hachables et peuvent être utilisés comme clés de dictionnaire.

En résumé, il existe plusieurs options pour memoizer des fonctions en Python. functools.lru_cache est un bon point de départ pour la plupart des cas, tandis que les modules externes peuvent offrir des fonctionnalités plus avancées.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be an optimal option. Just have to check :

  • cache size (?) (in future, response will be "n" objects json)
  • If the time saving is not negligible compared to calling the Crossref API

@parmentf
Copy link
Contributor

parmentf commented Feb 2, 2024

The output (in "value") is an object

{"doi":"...", "status":"..."} 

But I don't know if status is the correct word. I want to return if a publication is correct, not_found or retracted.

I would have used two different fields:

  1. retracted: which could contain "true", "false", or "unknown"
  2. error: which could contain an error message

@leogail
Copy link
Collaborator Author

leogail commented Feb 2, 2024

The output (in "value") is an object

{"doi":"...", "status":"..."} 

But I don't know if status is the correct word. I want to return if a publication is correct, not_found or retracted.

I would have used two different fields:

  1. retracted: which could contain "true", "false", or "unknown"
  2. error: which could contain an error message

And so, had a third field who validates the bib. ref. ?

@leogail leogail merged commit 175a896 into main Feb 2, 2024
1 check passed
@leogail leogail deleted the services/biblio-ref/feature branch February 2, 2024 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants