From 76f6737e9daefc102d9d6a3b6eb0b56be5785935 Mon Sep 17 00:00:00 2001 From: Alan Jenkins Date: Fri, 14 Aug 2015 10:14:17 +0100 Subject: [PATCH] lrucache: cleanup (-10 lines) dict.pop() will raise KeyError for us if necessary. I was confused because we used to have lrucache.pop() with a bug, that returned None instead. Great catch by @ThomasWaldmann. --- attic/lrucache.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/attic/lrucache.py b/attic/lrucache.py index 0e1a2eea5..4d3ba73b7 100644 --- a/attic/lrucache.py +++ b/attic/lrucache.py @@ -1,6 +1,3 @@ -class _NotFound: - pass - class LRUCache: def __init__(self, capacity, dispose): self._cache = {} @@ -18,22 +15,15 @@ def __setitem__(self, key, value): self._cache[key] = value def __getitem__(self, key): - try: - self._lru.remove(key) - self._lru.append(key) - except ValueError: - pass - return self._cache[key] + value = self._cache[key] # raise KeyError if not found + self._lru.remove(key) + self._lru.append(key) + return value def __delitem__(self, key): - try: - self._lru.remove(key) - except ValueError: - pass - item = self._cache.pop(key, _NotFound) - if item is _NotFound: - raise KeyError(key) - self._dispose(item) + value = self._cache.pop(key) # raise KeyError if not found + self._dispose(value) + self._lru.remove(key) def __contains__(self, key): return key in self._cache