فهرست منبع

LRUCache: added missing .pop() implementation

Jonas Borgström 12 سال پیش
والد
کامیت
19cb9058ac
1فایلهای تغییر یافته به همراه18 افزوده شده و 0 حذف شده
  1. 18 0
      darc/lrucache.py

+ 18 - 0
darc/lrucache.py

@@ -35,6 +35,17 @@ class LRUCache(dict):
             pass
         return super(LRUCache, self).__delitem__(key)
 
+    def pop(self, key, default=None):
+        try:
+            self._lru.remove(key)
+        except ValueError:
+            pass
+        return super(LRUCache, self).pop(key, default)
+
+    def _not_implemented(self, *args, **kw):
+        raise NotImplementedError
+    popitem = setdefault = update = _not_implemented
+
 
 class LRUCacheTestCase(unittest.TestCase):
 
@@ -66,6 +77,13 @@ class LRUCacheTestCase(unittest.TestCase):
         self.assertRaises(KeyError, lambda: c['c'])
         self.assertEqual(c['e'], 4)
 
+    def test_pop(self):
+        c = LRUCache(2)
+        c[1] = 1
+        c[2] = 2
+        c.pop(1)
+        c[3] = 3
+
 
 def suite():
     return unittest.TestLoader().loadTestsFromTestCase(LRUCacheTestCase)