Python functools32.lru_cache() Examples
The following are 10
code examples of functools32.lru_cache().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
functools32
, or try the search function
.
Example #1
Source File: base.py From python-stdlib-list with MIT License | 6 votes |
def in_stdlib(module_name, version=None): """ Return a ``bool`` indicating if module ``module_name`` is in the list of stdlib symbols for python version ``version``. If ``version`` is ``None`` (default), the version of current python interpreter is used. Note that ``True`` will be returned for built-in modules too, since this project considers they are part of stdlib. See :issue:21. It relies on ``@lru_cache`` to cache the stdlib list and query results for similar calls. Therefore it is much more efficient than ``module_name in stdlib_list()`` especially if you wish to perform multiple checks. :param str|None module_name: The module name (as a string) to query for. :param str|None version: The version (as a string) whose list of libraries you want (one of ``"2.6"``, ``"2.7"``, ``"3.2"``, ``"3.3"``, ``"3.4"``, or ``"3.5"``). If not specified, the current version of Python will be used. :return: A bool indicating if the given module name is part of standard libraries for the specified version of Python. :rtype: list """ ref_list = _stdlib_list_with_cache(version=version) return module_name in ref_list
Example #2
Source File: idapython.py From python-idb with Apache License 2.0 | 6 votes |
def memoized_method(*lru_args, **lru_kwargs): def decorator(func): @functools.wraps(func) def wrapped_func(self, *args, **kwargs): # We're storing the wrapped method inside the instance. If we had # a strong reference to self the instance would never die. self_weak = weakref.ref(self) @functools.wraps(func) @functools.lru_cache(*lru_args, **lru_kwargs) def cached_method(*args, **kwargs): return func(self_weak(), *args, **kwargs) setattr(self, func.__name__, cached_method) return cached_method(*args, **kwargs) return wrapped_func return decorator
Example #3
Source File: case_funcs.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def cases_generator(names=None, # type: Union[str, Callable[[Any], str], Iterable[str]] lru_cache=False, # type: bool, case_func=DECORATED, **param_ranges # type: Iterable[Any] ): """ Decorator to declare a case function as being a cases generator. `param_ranges` should be a named list of parameter ranges to explore to generate the cases. The decorator will use `itertools.product` to create a cartesian product of the named parameter ranges, and create a case for each combination. When the case function will be called for a given combination, the corresponding parameters will be passed to the decorated function. >>> @cases_generator("test with i={i}", i=range(10)) >>> def case_10_times(i): >>> ''' Generates 10 cases ''' >>> ins = dict(a=i, b=i+1) >>> outs = i+1, i+2 >>> return ins, outs, None :param names: a name template, that will be transformed into the case name using `names.format(**params)` for each case, where `params` is the dictionary of parameter values for this generated case. Alternately a callable returning a string can be provided, in which case `names(**params)` will be used. Finally an explicit list of names can be provided, in which case it should have the correct length (an error will be raised otherwise). :param lru_cache: a boolean (default False) indicating if the generated cases should be cached. This is identical to decorating the function with an additional `@lru_cache(maxsize=n)` where n is the total number of generated cases. :param param_ranges: named parameters and for each of them the list of values to be used to generate cases. For each combination of values (a cartesian product is made) the parameters will be passed to the underlying function so they should have names the underlying function can handle. :return: """ kwarg_values = list(product(*param_ranges.values())) setattr(case_func, _GENERATOR_FIELD, (names, param_ranges.keys(), kwarg_values)) if lru_cache: nb_cases = len(kwarg_values) # decorate the function with the appropriate lru cache size case_func = lru(maxsize=nb_cases)(case_func) return case_func
Example #4
Source File: test_so2.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def setup_dataset(db): # this is run once per db thanks to the lru_cache decorator print("setup for %s" % db)
Example #5
Source File: test_so2.py From python-pytest-cases with BSD 3-Clause "New" or "Revised" License | 5 votes |
def finalize_dataset(db): # this is run once per db thanks to the lru_cache decorator print("teardown for %s" % db)
Example #6
Source File: cache.py From Spectrum-Access-System with Apache License 2.0 | 5 votes |
def LruCache(maxsize=None): """LRU Cache decorator. Args: maxsize: the maximum cache size, or None for unlimited size. """ def wrapper(fn): return functools32.lru_cache(maxsize=maxsize)(fn) return wrapper # Cache management
Example #7
Source File: cache.py From Spectrum-Access-System with Apache License 2.0 | 5 votes |
def __enter__(self): self._wrapper_fn = functools32.lru_cache(maxsize=self._maxsize)(self._fn) self._overrideModuleFunctionWith(self._wrapper_fn) return self
Example #8
Source File: gumbel.py From chaospy with MIT License | 5 votes |
def _inverse_phi(self, u_loc, theta, order): @lru_cache(None) def iphi(n): if n: return sum(special.comb(n, i-1)*iphi(n-i)*sigma(i) for i in range(1, n+1)) return numpy.e**(-u_loc**(1/theta)) @lru_cache(None) def sigma(n): return self._sigma(u_loc, theta, n) return iphi(order)
Example #9
Source File: joe.py From chaospy with MIT License | 5 votes |
def _inverse_phi(self, u_loc, theta, order): if not order: return 1-(1-numpy.e**-u_loc)**(1/theta) @lru_cache(None) def rho(n, m=1): if n == m: return self._sigma(1-numpy.e**-u_loc, theta, n)*numpy.e**(-n*theta) return rho(n, m+1)-m*rho(n-1, m) return rho(order)
Example #10
Source File: ABuFixes.py From abu with GNU General Public License v3.0 | 5 votes |
def lru_cache(maxsize=100): def decorate(func): @functools.wraps(func) def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper return decorate