-
-
Save Morreski/c1d08a3afa4040815eafd3891e16b945 to your computer and use it in GitHub Desktop.
| from datetime import datetime, timedelta | |
| import functools | |
| def timed_cache(**timedelta_kwargs): | |
| def _wrapper(f): | |
| update_delta = timedelta(**timedelta_kwargs) | |
| next_update = datetime.utcnow() + update_delta | |
| # Apply @lru_cache to f with no cache size limit | |
| f = functools.lru_cache(None)(f) | |
| @functools.wraps(f) | |
| def _wrapped(*args, **kwargs): | |
| nonlocal next_update | |
| now = datetime.utcnow() | |
| if now >= next_update: | |
| f.cache_clear() | |
| next_update = now + update_delta | |
| return f(*args, **kwargs) | |
| return _wrapped | |
| return _wrapper |
Thanks guys ! Btw it can leads to a
TypeError: unhashable type: 'list'if you have list args. A fix could be to cast those args to tuple (more info : https://stackoverflow.com/a/49210802) This piece of code will fix this :for arg, value in kwargs.items(): kwargs[arg] = tuple(value) if type(value) == list else value
The behavior remains the same but I would suggest to use isinstance() instead of type()
for arg, value in kwargs.items():
kwargs[arg] = tuple(value) if isinstance(value, list) else valueThanks for the implementations! Really helpful!
Something I noticed is that neither of these implementations work with pytest-antilru. This is likely due to the lru_cache which is monkeypatched is not patched early enough: ipwnponies/pytest-antilru#28.
args = [tuple(v) if isinstance(v, list) else v for v in args]
for args too
Thanks guys ! Btw it can leads to a
TypeError: unhashable type: 'list'if you have list args. A fix could be to cast those args to tuple (more info : https://stackoverflow.com/a/49210802) This piece of code will fix this :for arg, value in kwargs.items(): kwargs[arg] = tuple(value) if type(value) == list else valueThe behavior remains the same but I would suggest to use
isinstance()instead oftype()for arg, value in kwargs.items(): kwargs[arg] = tuple(value) if isinstance(value, list) else value
Thanks guys ! Btw it can leads to a
TypeError: unhashable type: 'list'if you have list args.A fix could be to cast those args to tuple (more info : https://stackoverflow.com/a/49210802)
This piece of code will fix this :