本文整理汇总了Python中functools.lru_cache方法的典型用法代码示例。如果您正苦于以下问题:Python functools.lru_cache方法的具体用法?Python functools.lru_cache怎么用?Python functools.lru_cache使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类functools
的用法示例。
在下文中一共展示了functools.lru_cache方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: gen_distributor
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def gen_distributor(scheduler_n_process, worker_n_process):
class LocalClusterDistributor(Distributor):
def __init__(self, n_process):
super().__init__(n_process)
self._scheduler_distributor = MarsDistributor(scheduler_n_process, 's:h1:')
self._worker_distributor = MarsDistributor(worker_n_process, 'w:0:')
@staticmethod
def _is_worker_uid(uid):
return isinstance(uid, str) and uid.startswith('w:')
@functools.lru_cache(100)
def distribute(self, uid):
if self._is_worker_uid(uid):
return self._worker_distributor.distribute(uid) + scheduler_n_process
return self._scheduler_distributor.distribute(uid)
def make_same_process(self, uid, uid_rel, delta=0):
if self._is_worker_uid(uid_rel):
return self._worker_distributor.make_same_process(uid, uid_rel, delta=delta)
return self._scheduler_distributor.make_same_process(uid, uid_rel, delta=delta)
return LocalClusterDistributor(scheduler_n_process + worker_n_process)
示例2: lru_cached_method
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def lru_cached_method(*lru_args, **lru_kwargs):
def decorator(wrapped_fn):
@wraps(wrapped_fn)
def wrapped(self, *args, **kwargs):
# Use a weak reference to self; this prevents a self-reference
# cycle that fools the garbage collector into thinking the instance
# shouldn't be dropped when all external references are dropped.
weak_ref_to_self = weakref.ref(self)
@wraps(wrapped_fn)
@lru_cache(*lru_args, **lru_kwargs)
def cached(*args, **kwargs):
return wrapped_fn(weak_ref_to_self(), *args, **kwargs)
setattr(self, wrapped_fn.__name__, cached)
return cached(*args, **kwargs)
return wrapped
return decorator
示例3: lru_cache
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def lru_cache(maxsize):
"""Simple cache (with no maxsize basically) for py27 compatibility.
Given that pdb there uses linecache.getline for each line with
do_list a cache makes a big differene."""
def dec(fn, *args):
cache = {}
@wraps(fn)
def wrapper(*args):
key = args
try:
ret = cache[key]
except KeyError:
ret = cache[key] = fn(*args)
return ret
return wrapper
return dec
# If it contains only _, digits, letters, [] or dots, it's probably side
# effects free.
示例4: load_wav
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def load_wav(wav_rxfilename, start=0, end=None):
""" This function reads audio file and return data in numpy.float32 array.
"lru_cache" holds recently loaded audio so that can be called
many times on the same audio file.
OPTIMIZE: controls lru_cache size for random access,
considering memory size
"""
if wav_rxfilename.endswith('|'):
# input piped command
p = subprocess.Popen(wav_rxfilename[:-1], shell=True,
stdout=subprocess.PIPE)
data, samplerate = sf.read(io.BytesIO(p.stdout.read()),
dtype='float32')
# cannot seek
data = data[start:end]
elif wav_rxfilename == '-':
# stdin
data, samplerate = sf.read(sys.stdin, dtype='float32')
# cannot seek
data = data[start:end]
else:
# normal wav file
data, samplerate = sf.read(wav_rxfilename, start=start, stop=end)
return data, samplerate
示例5: cache_result
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def cache_result(function):
"""A function decorator to cache the result of the first call, every
additional call will simply return the cached value.
If we were python3 only, we would have used functools.lru_cache() in place
of this. If there's a python2 backport in a lightweight library, then we
should switch to that.
"""
# NOTE: We're cheating a little here, by using a mutable type (a list),
# we're able to read and update the value from within in inline
# wrapper method. If we used an immutable type, the assignment
# would not work as we want.
cache = []
def wrapper(cls_instance):
if not cache:
cache.append(function(cls_instance))
return cache[0]
return wrapper
示例6: check_for_language
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def check_for_language(lang_code):
"""
Check whether there is a global language file for the given language
code. This is used to decide whether a user-provided language is
available.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
# First, a quick check to make sure lang_code is well-formed (#21458)
if lang_code is None or not language_code_re.search(lang_code):
return False
for path in all_locale_paths():
if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:
return True
return False
示例7: test_cache_clear
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def test_cache_clear(self):
from unittest.mock import MagicMock
call_check_mock = MagicMock()
@lru_cache(maxsize=4)
def target_func():
call_check_mock()
target_func()
self.assertEqual(call_check_mock.call_count, 1)
target_func()
self.assertEqual(call_check_mock.call_count, 1)
# WHEN
target_func.cache_clear()
target_func()
self.assertEqual(call_check_mock.call_count, 2)
示例8: tradetime
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def tradetime(self):
"""返回交易所日历下的日期
Returns:
[type] -- [description]
"""
try:
return self.date
except:
return None
# @property
# @lru_cache()
# def semiannual(self):
# return self.resample('SA')
示例9: get_lcs
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def get_lcs(seq1, seq2):
'''Returns the longest common subsequence using memoization (only in local scope)'''
@lru_cache(maxsize=None)
def recursive_lcs(seq1, seq2):
if len(seq1) == 0 or len(seq2) == 0:
return []
if seq1[-1] == seq2[-1]:
return recursive_lcs(seq1[:-1], seq2[:-1]) + [seq1[-1]]
else:
return max(recursive_lcs(seq1[:-1], seq2), recursive_lcs(seq1, seq2[:-1]), key=lambda seq: len(seq))
try:
return recursive_lcs(tuple(seq1), tuple(seq2))
except RecursionError as e:
print(e)
# TODO: Handle this case
return []
示例10: in_stdlib
# 需要导入模块: import functools [as 别名]
# 或者: from functools 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
示例11: _min_range_diff
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def _min_range_diff(coordinates: Tuple[Tuple[int, int]], absolute: bool = True) -> int:
"""Get the minimum range difference.
# Using Tuple instead of list because list is unhashable with `lru_cache`
# if absolute=True, return the absolute value of minimum magnitude difference
# if absolute=False, return the raw value of minimum magnitude difference
# TODO: move back to efficient implementation once it sees that
# min_range_diff(3,3,2,3) = 0 return max(0, max(a_end - b_start, b_end -
# a_start))
:param coordinates: A tuple of a couple (start, end) indexes of the objects.
:param absolute: Whether use absolute value, defaults to True.
:return: The minimum range difference.
"""
f = lambda x: (abs(x) if absolute else x)
return min(
[
f(min([x - y for x, y in zip(ii[:-1], ii[1:])], key=abs))
for ii in itertools.product(
*[range(start, end + 1) for start, end in coordinates]
)
],
key=abs,
)
示例12: lru_cache_time
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def lru_cache_time(ttl=None, maxsize=None):
"""
TTL support on lru_cache
:param ttl: float or int, seconds
:param maxsize: int, maxsize for lru_cache
:return:
"""
def wrapper(func):
# Lazy function that makes sure the lru_cache() invalidate after X secs
@lru_cache(maxsize)
def time_aware(_ttl, *args, **kwargs):
return func(*args, **kwargs)
setattr(thismodule, func.__name__ + "_ttl", time_aware)
@wraps(func)
def newfunc(*args, **kwargs):
ttl_hash = round(time.time() / ttl)
f_ttl = getattr(thismodule, func.__name__ + "_ttl")
return f_ttl(ttl_hash, *args, **kwargs)
return newfunc
return wrapper
# TODO: 缓存 token 的合适时间尺度
示例13: cached_property
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def cached_property(fn):
"""Decorate property to cache return value."""
return property(functools.lru_cache(maxsize=8)(fn))
示例14: cache_by_hashed_args
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def cache_by_hashed_args(obj):
""" Decorator for caching a function values
.. deprecated:: v0.9.8.3
:func:`cache_by_hashed_args` will be removed in pyGSTi
v0.9.9. Use :func:`functools.lru_cache` instead.
"""
return lru_cache(maxsize=128)(obj)
示例15: _memo
# 需要导入模块: import functools [as 别名]
# 或者: from functools import lru_cache [as 别名]
def _memo(fn):
return property(functools.lru_cache(maxsize=1)(fn))