Mercurial > repos > shellac > sam_consensus_v3
diff env/lib/python3.9/site-packages/humanfriendly/decorators.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/env/lib/python3.9/site-packages/humanfriendly/decorators.py Mon Mar 22 18:12:50 2021 +0000 @@ -0,0 +1,43 @@ +# Human friendly input/output in Python. +# +# Author: Peter Odding <peter@peterodding.com> +# Last Change: March 2, 2020 +# URL: https://humanfriendly.readthedocs.io + +"""Simple function decorators to make Python programming easier.""" + +# Standard library modules. +import functools + +# Public identifiers that require documentation. +__all__ = ('RESULTS_ATTRIBUTE', 'cached') + +RESULTS_ATTRIBUTE = 'cached_results' +"""The name of the property used to cache the return values of functions (a string).""" + + +def cached(function): + """ + Rudimentary caching decorator for functions. + + :param function: The function whose return value should be cached. + :returns: The decorated function. + + The given function will only be called once, the first time the wrapper + function is called. The return value is cached by the wrapper function as + an attribute of the given function and returned on each subsequent call. + + .. note:: Currently no function arguments are supported because only a + single return value can be cached. Accepting any function + arguments at all would imply that the cache is parametrized on + function arguments, which is not currently the case. + """ + @functools.wraps(function) + def wrapper(): + try: + return getattr(wrapper, RESULTS_ATTRIBUTE) + except AttributeError: + result = function() + setattr(wrapper, RESULTS_ATTRIBUTE, result) + return result + return wrapper