Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@cachedmethod(operator.attrgetter('cache'), key=keys.typedkey)
def get_typed(self, value):
count = self.count
self.count += 1
return count
wrapper = cachetools.cached(cache, key=key)(self.func)
self.assertEqual(len(cache), 0)
self.assertEqual(wrapper.__wrapped__, self.func)
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertNotIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
def test_decorator_typed(self):
cache = self.cache(3)
key = cachetools.keys.typedkey
wrapper = cachetools.cached(cache, key=key)(self.func)
self.assertEqual(len(cache), 0)
self.assertEqual(wrapper.__wrapped__, self.func)
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertNotIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
def test_decorator_typed(self):
cache = self.cache(3)
key = cachetools.keys.typedkey
wrapper = cachetools.cached(cache, key=key)(self.func)
self.assertEqual(len(cache), 0)
self.assertEqual(wrapper.__wrapped__, self.func)
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertNotIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
def test_typedkey(self, key=cachetools.keys.typedkey):
self.assertEqual(key(), key())
self.assertEqual(hash(key()), hash(key()))
self.assertEqual(key(1, 2, 3), key(1, 2, 3))
self.assertEqual(hash(key(1, 2, 3)), hash(key(1, 2, 3)))
self.assertEqual(key(1, 2, 3, x=0), key(1, 2, 3, x=0))
self.assertEqual(hash(key(1, 2, 3, x=0)), hash(key(1, 2, 3, x=0)))
self.assertNotEqual(key(1, 2, 3), key(3, 2, 1))
self.assertNotEqual(key(1, 2, 3), key(1, 2, 3, x=None))
self.assertNotEqual(key(1, 2, 3, x=0), key(1, 2, 3, x=None))
self.assertNotEqual(key(1, 2, 3, x=0), key(1, 2, 3, y=0))
with self.assertRaises(TypeError):
hash(key({}))
# typed keys compare unequal
self.assertNotEqual(key(1, 2, 3), key(1.0, 2.0, 3.0))
key = cachetools.keys.typedkey
wrapper = cachetools.cached(cache, key=key)(self.func)
self.assertEqual(len(cache), 0)
self.assertEqual(wrapper.__wrapped__, self.func)
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertNotIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
def test_decorator_typed(self):
cache = self.cache(3)
key = cachetools.keys.typedkey
wrapper = cachetools.cached(cache, key=key)(self.func)
self.assertEqual(len(cache), 0)
self.assertEqual(wrapper.__wrapped__, self.func)
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertNotIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
self.assertIn(cachetools.keys.typedkey(0), cache)
self.assertIn(cachetools.keys.typedkey(1), cache)
self.assertIn(cachetools.keys.typedkey(1.0), cache)
def decorator(func):
key = keys.typedkey if typed else keys.hashkey
lock = RLock()
stats = [0, 0]
def wrapper(*args, **kwargs):
k = key(*args, **kwargs)
with lock:
try:
v = cache[k]
stats[0] += 1
return v
except KeyError:
stats[1] += 1
v = func(*args, **kwargs)
try:
with lock:
cache[k] = v