當前位置: 首頁>>代碼示例>>Python>>正文


Python functools.lru_cache方法代碼示例

本文整理匯總了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) 
開發者ID:mars-project,項目名稱:mars,代碼行數:26,代碼來源:distributor.py

示例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 
開發者ID:hyperledger,項目名稱:sawtooth-core,代碼行數:19,代碼來源:state_view.py

示例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. 
開發者ID:pdbpp,項目名稱:pdbpp,代碼行數:26,代碼來源:pdbpp.py

示例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 
開發者ID:hitachi-speech,項目名稱:EEND,代碼行數:26,代碼來源:kaldi_data.py

示例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 
開發者ID:openstack,項目名稱:designate,代碼行數:21,代碼來源:utils.py

示例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 
開發者ID:reBiocoder,項目名稱:bioforum,代碼行數:19,代碼來源:trans_real.py

示例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) 
開發者ID:icon-project,項目名稱:loopchain,代碼行數:20,代碼來源:test_lru_cache.py

示例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') 
開發者ID:QUANTAXIS,項目名稱:QUANTAXIS,代碼行數:18,代碼來源:QADataStruct.py

示例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 [] 
開發者ID:feralvam,項目名稱:easse,代碼行數:19,代碼來源:lcs.py

示例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 
開發者ID:jackmaney,項目名稱:python-stdlib-list,代碼行數:26,代碼來源:base.py

示例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,
    ) 
開發者ID:HazyResearch,項目名稱:fonduer,代碼行數:26,代碼來源:utils_table.py

示例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 的合適時間尺度 
開發者ID:refraction-ray,項目名稱:xalpha,代碼行數:31,代碼來源:universal.py

示例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)) 
開發者ID:containers,項目名稱:python-podman,代碼行數:5,代碼來源:__init__.py

示例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) 
開發者ID:pyGSTio,項目名稱:pyGSTi,代碼行數:10,代碼來源:opttools.py

示例15: _memo

# 需要導入模塊: import functools [as 別名]
# 或者: from functools import lru_cache [as 別名]
def _memo(fn):
    return property(functools.lru_cache(maxsize=1)(fn)) 
開發者ID:pyGSTio,項目名稱:pyGSTi,代碼行數:4,代碼來源:references.py


注:本文中的functools.lru_cache方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。