-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
The output (in "value") is an object {"doi":"...", "status":"..."} But I don't know if status is the correct word. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
I would have used two different fields:
|
And so, had a third field who validates the bib. ref. ? |
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 :