Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(929)

Side by Side Diff: third_party/google-endpoints/cachetools/__init__.py

Issue 2666783008: Add google-endpoints to third_party/. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 """Extensible memoizing collections and decorators."""
2
3 import functools
4 import warnings
5
6 from .cache import Cache
7 from .func import lfu_cache, lru_cache, rr_cache, ttl_cache
8 from .keys import hashkey, typedkey
9 from .lfu import LFUCache
10 from .lru import LRUCache
11 from .rr import RRCache
12 from .ttl import TTLCache
13
14 __all__ = (
15 'Cache', 'LFUCache', 'LRUCache', 'RRCache', 'TTLCache',
16 'cached', 'cachedmethod', 'hashkey', 'typedkey',
17 # make cachetools.func.* available for backwards compatibility
18 'lfu_cache', 'lru_cache', 'rr_cache', 'ttl_cache',
19 )
20
21 __version__ = '1.1.6'
22
23 _default = [] # evaluates to False
24
25 if hasattr(functools.update_wrapper(lambda f: f(), lambda: 42), '__wrapped__'):
26 _update_wrapper = functools.update_wrapper
27 else:
28 def _update_wrapper(wrapper, wrapped):
29 functools.update_wrapper(wrapper, wrapped)
30 wrapper.__wrapped__ = wrapped
31 return wrapper
32
33
34 def cached(cache, key=hashkey, lock=None):
35 """Decorator to wrap a function with a memoizing callable that saves
36 results in a cache.
37
38 """
39 def decorator(func):
40 if cache is None:
41 def wrapper(*args, **kwargs):
42 return func(*args, **kwargs)
43 elif lock is None:
44 def wrapper(*args, **kwargs):
45 k = key(*args, **kwargs)
46 try:
47 return cache[k]
48 except KeyError:
49 pass # key not found
50 v = func(*args, **kwargs)
51 try:
52 cache[k] = v
53 except ValueError:
54 pass # value too large
55 return v
56 else:
57 def wrapper(*args, **kwargs):
58 k = key(*args, **kwargs)
59 try:
60 with lock:
61 return cache[k]
62 except KeyError:
63 pass # key not found
64 v = func(*args, **kwargs)
65 try:
66 with lock:
67 cache[k] = v
68 except ValueError:
69 pass # value too large
70 return v
71 return _update_wrapper(wrapper, func)
72 return decorator
73
74
75 def cachedmethod(cache, key=_default, lock=None, typed=_default):
76 """Decorator to wrap a class or instance method with a memoizing
77 callable that saves results in a cache.
78
79 """
80 if key is not _default and not callable(key):
81 key, typed = _default, key
82 if typed is not _default:
83 warnings.warn("Passing 'typed' to cachedmethod() is deprecated, "
84 "use 'key=typedkey' instead", DeprecationWarning, 2)
85
86 def decorator(method):
87 # pass method to default key function for backwards compatibilty
88 if key is _default:
89 makekey = functools.partial(typedkey if typed else hashkey, method)
90 else:
91 makekey = key # custom key function always receive method args
92 if lock is None:
93 def wrapper(self, *args, **kwargs):
94 c = cache(self)
95 if c is None:
96 return method(self, *args, **kwargs)
97 k = makekey(self, *args, **kwargs)
98 try:
99 return c[k]
100 except KeyError:
101 pass # key not found
102 v = method(self, *args, **kwargs)
103 try:
104 c[k] = v
105 except ValueError:
106 pass # value too large
107 return v
108 else:
109 def wrapper(self, *args, **kwargs):
110 c = cache(self)
111 if c is None:
112 return method(self, *args, **kwargs)
113 k = makekey(self, *args, **kwargs)
114 try:
115 with lock(self):
116 return c[k]
117 except KeyError:
118 pass # key not found
119 v = method(self, *args, **kwargs)
120 try:
121 with lock(self):
122 c[k] = v
123 except ValueError:
124 pass # value too large
125 return v
126 _update_wrapper(wrapper, method)
127
128 # deprecated wrapper attribute
129 def getter(self):
130 warnings.warn('%s.cache is deprecated' % method.__name__,
131 DeprecationWarning, 2)
132 return cache(self)
133 wrapper.cache = getter
134 return wrapper
135 return decorator
OLDNEW
« no previous file with comments | « third_party/google-endpoints/cachetools-1.1.6.dist-info/top_level.txt ('k') | third_party/google-endpoints/cachetools/abc.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698