本文整理匯總了Python中functools32.lru_cache方法的典型用法代碼示例。如果您正苦於以下問題:Python functools32.lru_cache方法的具體用法?Python functools32.lru_cache怎麽用?Python functools32.lru_cache使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類functools32
的用法示例。
在下文中一共展示了functools32.lru_cache方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: in_stdlib
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
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
示例2: memoized_method
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
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
示例3: cases_generator
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
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
示例4: setup_dataset
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
def setup_dataset(db):
# this is run once per db thanks to the lru_cache decorator
print("setup for %s" % db)
示例5: finalize_dataset
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
def finalize_dataset(db):
# this is run once per db thanks to the lru_cache decorator
print("teardown for %s" % db)
示例6: LruCache
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
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
示例7: __enter__
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
def __enter__(self):
self._wrapper_fn = functools32.lru_cache(maxsize=self._maxsize)(self._fn)
self._overrideModuleFunctionWith(self._wrapper_fn)
return self
示例8: _inverse_phi
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
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)
示例9: _inverse_phi
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
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)
示例10: lru_cache
# 需要導入模塊: import functools32 [as 別名]
# 或者: from functools32 import lru_cache [as 別名]
def lru_cache(maxsize=100):
def decorate(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
return decorate