diff options
author | robot-piglet <[email protected]> | 2025-03-07 23:32:22 +0300 |
---|---|---|
committer | robot-piglet <[email protected]> | 2025-03-07 23:42:58 +0300 |
commit | 732794c096f3c49d5f8c830c3b0c66514248025e (patch) | |
tree | 6af5fb1b6afa0221b566922f3dfa2132935f428d /contrib/python | |
parent | 8551771a23660e0e75431bfa4c5f44d21333cfd9 (diff) |
Intermediate changes
commit_hash:7fd41aea8d2afa441f1a298f5ed29b726b7e5a87
Diffstat (limited to 'contrib/python')
-rw-r--r-- | contrib/python/cachetools/py3/.dist-info/METADATA | 4 | ||||
-rw-r--r-- | contrib/python/cachetools/py3/LICENSE | 2 | ||||
-rw-r--r-- | contrib/python/cachetools/py3/README.rst | 2 | ||||
-rw-r--r-- | contrib/python/cachetools/py3/cachetools/__init__.py | 135 | ||||
-rw-r--r-- | contrib/python/cachetools/py3/cachetools/_decorators.py | 152 | ||||
-rw-r--r-- | contrib/python/cachetools/py3/ya.make | 3 |
6 files changed, 165 insertions, 133 deletions
diff --git a/contrib/python/cachetools/py3/.dist-info/METADATA b/contrib/python/cachetools/py3/.dist-info/METADATA index 386951ecb89..b8fe3d6b383 100644 --- a/contrib/python/cachetools/py3/.dist-info/METADATA +++ b/contrib/python/cachetools/py3/.dist-info/METADATA @@ -1,6 +1,6 @@ Metadata-Version: 2.2 Name: cachetools -Version: 5.5.1 +Version: 5.5.2 Summary: Extensible memoizing collections and decorators Home-page: https://github.com/tkem/cachetools/ Author: Thomas Kemmer @@ -126,7 +126,7 @@ Related Projects License ------------------------------------------------------------------------ -Copyright (c) 2014-2024 Thomas Kemmer. +Copyright (c) 2014-2025 Thomas Kemmer. Licensed under the `MIT License`_. diff --git a/contrib/python/cachetools/py3/LICENSE b/contrib/python/cachetools/py3/LICENSE index 67da58ecd7a..762a2f0bad6 100644 --- a/contrib/python/cachetools/py3/LICENSE +++ b/contrib/python/cachetools/py3/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014-2024 Thomas Kemmer +Copyright (c) 2014-2025 Thomas Kemmer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/contrib/python/cachetools/py3/README.rst b/contrib/python/cachetools/py3/README.rst index a5627cc114a..f480b609a8b 100644 --- a/contrib/python/cachetools/py3/README.rst +++ b/contrib/python/cachetools/py3/README.rst @@ -100,7 +100,7 @@ Related Projects License ------------------------------------------------------------------------ -Copyright (c) 2014-2024 Thomas Kemmer. +Copyright (c) 2014-2025 Thomas Kemmer. Licensed under the `MIT License`_. diff --git a/contrib/python/cachetools/py3/cachetools/__init__.py b/contrib/python/cachetools/py3/cachetools/__init__.py index ad301758612..d3051c1049f 100644 --- a/contrib/python/cachetools/py3/cachetools/__init__.py +++ b/contrib/python/cachetools/py3/cachetools/__init__.py @@ -13,7 +13,7 @@ __all__ = ( "cachedmethod", ) -__version__ = "5.5.1" +__version__ = "5.5.2" import collections import collections.abc @@ -23,6 +23,7 @@ import random import time from . import keys +from ._decorators import _cached_wrapper class _DefaultSize: @@ -643,150 +644,28 @@ def cached(cache, key=keys.hashkey, lock=None, info=False): def decorator(func): if info: - hits = misses = 0 - if isinstance(cache, Cache): - def getinfo(): - nonlocal hits, misses + def make_info(hits, misses): return _CacheInfo(hits, misses, cache.maxsize, cache.currsize) elif isinstance(cache, collections.abc.Mapping): - def getinfo(): - nonlocal hits, misses + def make_info(hits, misses): return _CacheInfo(hits, misses, None, len(cache)) else: - def getinfo(): - nonlocal hits, misses + def make_info(hits, misses): return _CacheInfo(hits, misses, 0, 0) - if cache is None: - - def wrapper(*args, **kwargs): - nonlocal misses - misses += 1 - return func(*args, **kwargs) - - def cache_clear(): - nonlocal hits, misses - hits = misses = 0 - - cache_info = getinfo - - elif lock is None: - - def wrapper(*args, **kwargs): - nonlocal hits, misses - k = key(*args, **kwargs) - try: - result = cache[k] - hits += 1 - return result - except KeyError: - misses += 1 - v = func(*args, **kwargs) - try: - cache[k] = v - except ValueError: - pass # value too large - return v - - def cache_clear(): - nonlocal hits, misses - cache.clear() - hits = misses = 0 - - cache_info = getinfo - - else: - - def wrapper(*args, **kwargs): - nonlocal hits, misses - k = key(*args, **kwargs) - try: - with lock: - result = cache[k] - hits += 1 - return result - except KeyError: - with lock: - misses += 1 - v = func(*args, **kwargs) - # in case of a race, prefer the item already in the cache - try: - with lock: - return cache.setdefault(k, v) - except ValueError: - return v # value too large - - def cache_clear(): - nonlocal hits, misses - with lock: - cache.clear() - hits = misses = 0 - - def cache_info(): - with lock: - return getinfo() - + wrapper = _cached_wrapper(func, cache, key, lock, make_info) else: - if cache is None: - - def wrapper(*args, **kwargs): - return func(*args, **kwargs) - - def cache_clear(): - pass - - elif lock is None: - - def wrapper(*args, **kwargs): - k = key(*args, **kwargs) - try: - return cache[k] - except KeyError: - pass # key not found - v = func(*args, **kwargs) - try: - cache[k] = v - except ValueError: - pass # value too large - return v - - def cache_clear(): - cache.clear() - - else: - - def wrapper(*args, **kwargs): - k = key(*args, **kwargs) - try: - with lock: - return cache[k] - except KeyError: - pass # key not found - v = func(*args, **kwargs) - # in case of a race, prefer the item already in the cache - try: - with lock: - return cache.setdefault(k, v) - except ValueError: - return v # value too large - - def cache_clear(): - with lock: - cache.clear() - - cache_info = None + wrapper = _cached_wrapper(func, cache, key, lock, None) wrapper.cache = cache wrapper.cache_key = key wrapper.cache_lock = lock - wrapper.cache_clear = cache_clear - wrapper.cache_info = cache_info return functools.update_wrapper(wrapper, func) diff --git a/contrib/python/cachetools/py3/cachetools/_decorators.py b/contrib/python/cachetools/py3/cachetools/_decorators.py new file mode 100644 index 00000000000..fcac1e01b9a --- /dev/null +++ b/contrib/python/cachetools/py3/cachetools/_decorators.py @@ -0,0 +1,152 @@ +"""Extensible memoizing decorator helpers.""" + + +def _cached_locked_info(func, cache, key, lock, info): + hits = misses = 0 + + def wrapper(*args, **kwargs): + nonlocal hits, misses + k = key(*args, **kwargs) + with lock: + try: + result = cache[k] + hits += 1 + return result + except KeyError: + misses += 1 + v = func(*args, **kwargs) + with lock: + try: + # in case of a race, prefer the item already in the cache + return cache.setdefault(k, v) + except ValueError: + return v # value too large + + def cache_clear(): + nonlocal hits, misses + with lock: + cache.clear() + hits = misses = 0 + + def cache_info(): + with lock: + return info(hits, misses) + + wrapper.cache_clear = cache_clear + wrapper.cache_info = cache_info + return wrapper + + +def _cached_unlocked_info(func, cache, key, info): + hits = misses = 0 + + def wrapper(*args, **kwargs): + nonlocal hits, misses + k = key(*args, **kwargs) + try: + result = cache[k] + hits += 1 + return result + except KeyError: + misses += 1 + v = func(*args, **kwargs) + try: + cache[k] = v + except ValueError: + pass # value too large + return v + + def cache_clear(): + nonlocal hits, misses + cache.clear() + hits = misses = 0 + + wrapper.cache_clear = cache_clear + wrapper.cache_info = lambda: info(hits, misses) + return wrapper + + +def _uncached_info(func, info): + misses = 0 + + def wrapper(*args, **kwargs): + nonlocal misses + misses += 1 + return func(*args, **kwargs) + + def cache_clear(): + nonlocal misses + misses = 0 + + wrapper.cache_clear = cache_clear + wrapper.cache_info = lambda: info(0, misses) + return wrapper + + +def _cached_locked(func, cache, key, lock): + def wrapper(*args, **kwargs): + k = key(*args, **kwargs) + with lock: + try: + return cache[k] + except KeyError: + pass # key not found + v = func(*args, **kwargs) + with lock: + try: + # in case of a race, prefer the item already in the cache + return cache.setdefault(k, v) + except ValueError: + return v # value too large + + def cache_clear(): + with lock: + cache.clear() + + wrapper.cache_clear = cache_clear + return wrapper + + +def _cached_unlocked(func, cache, key): + def wrapper(*args, **kwargs): + k = key(*args, **kwargs) + try: + return cache[k] + except KeyError: + pass # key not found + v = func(*args, **kwargs) + try: + cache[k] = v + except ValueError: + pass # value too large + return v + + wrapper.cache_clear = lambda: cache.clear() + return wrapper + + +def _uncached(func): + def wrapper(*args, **kwargs): + return func(*args, **kwargs) + + wrapper.cache_clear = lambda: None + return wrapper + + +def _cached_wrapper(func, cache, key, lock, info): + if info is not None: + if cache is None: + wrapper = _uncached_info(func, info) + elif lock is None: + wrapper = _cached_unlocked_info(func, cache, key, info) + else: + wrapper = _cached_locked_info(func, cache, key, lock, info) + else: + if cache is None: + wrapper = _uncached(func) + elif lock is None: + wrapper = _cached_unlocked(func, cache, key) + else: + wrapper = _cached_locked(func, cache, key, lock) + wrapper.cache_info = None + return wrapper diff --git a/contrib/python/cachetools/py3/ya.make b/contrib/python/cachetools/py3/ya.make index 1581ce124b3..4a16cc5bc50 100644 --- a/contrib/python/cachetools/py3/ya.make +++ b/contrib/python/cachetools/py3/ya.make @@ -2,7 +2,7 @@ PY3_LIBRARY() -VERSION(5.5.1) +VERSION(5.5.2) LICENSE(MIT) @@ -11,6 +11,7 @@ NO_LINT() PY_SRCS( TOP_LEVEL cachetools/__init__.py + cachetools/_decorators.py cachetools/func.py cachetools/keys.py ) |