Rev 3888: Implement update in http://bzr.arbash-meinel.com/branches/bzr/1.11/fifo_cache

John Arbash Meinel john at arbash-meinel.com
Tue Dec 9 21:50:58 GMT 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.11/fifo_cache

------------------------------------------------------------
revno: 3888
revision-id: john at arbash-meinel.com-20081209215038-uevgqhtrnlgnkecj
parent: john at arbash-meinel.com-20081209213917-c5ko2gvw831txpt8
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: fifo_cache
timestamp: Tue 2008-12-09 15:50:38 -0600
message:
  Implement update
-------------- next part --------------
=== modified file 'bzrlib/fifo_cache.py'
--- a/bzrlib/fifo_cache.py	2008-12-09 21:35:49 +0000
+++ b/bzrlib/fifo_cache.py	2008-12-09 21:50:38 +0000
@@ -111,8 +111,23 @@
         # See pop()
         raise NotImplementedError(self.popitem)
 
-    def setdefault(self):
+    def setdefault(self, key, defaultval=None):
+        """similar to dict.setdefault"""
         raise NotImplementedError(self.setdefault)
 
     def update(self, *args, **kwargs):
-        raise NotImplementedError(self.update)
+        """Similar to dict.update()"""
+        if len(args) == 1:
+            arg = args[0]
+            if isinstance(arg, dict):
+                for key, val in arg.iteritems():
+                    self.add(key, val)
+            else:
+                for key, val in args[0]:
+                    self.add(key, val)
+        elif len(args) > 1:
+            raise TypeError('update expected at most 1 argument, got %d'
+                            % len(args))
+        if kwargs:
+            for key, val in kwargs.iteritems():
+                self.add(key, val)

=== modified file 'bzrlib/tests/test_fifo_cache.py'
--- a/bzrlib/tests/test_fifo_cache.py	2008-12-09 21:39:17 +0000
+++ b/bzrlib/tests/test_fifo_cache.py	2008-12-09 21:50:38 +0000
@@ -113,11 +113,25 @@
 
     def test_setdefault_not_implemented(self):
         c = fifo_cache.FIFOCache()
-        self.assertRaises(NotImplementedError, c.setdefault)
+        self.assertRaises(NotImplementedError, c.setdefault, 1, 2)
 
-    def test_update_not_implemented(self):
-        c = fifo_cache.FIFOCache()
-        self.assertRaises(NotImplementedError, c.update)
+    def test_update(self):
+        c = fifo_cache.FIFOCache(5, 4)
+        # We allow an iterable
+        c.update([(1, 2), (3, 4)])
+        self.assertEqual({1: 2, 3: 4}, c)
+        # Or kwarg form
+        c.update(foo=3, bar=4)
+        self.assertEqual({1: 2, 3: 4, 'foo': 3, 'bar': 4}, c)
+        # Even a dict (This triggers a cleanup)
+        c.update({'baz': 'biz', 'bing': 'bang'})
+        self.assertEqual({'foo': 3, 'bar': 4, 'baz': 'biz', 'bing': 'bang'}, c)
+        # We only allow 1 iterable, just like dict
+        self.assertRaises(TypeError, c.update, [(1, 2)], [(3, 4)])
+        # But you can mix and match. kwargs take precedence over iterable
+        c.update([('a', 'b'), ('d', 'e')], a='c', q='r')
+        self.assertEqual({'baz': 'biz', 'bing': 'bang',
+                          'a': 'c', 'd': 'e', 'q': 'r'}, c)
 
     def test_cleanup_funcs(self):
         log = []



More information about the bazaar-commits mailing list