[Zodb-checkins] CVS: ZODB/src/persistent - wref.py:1.1.2.5

Jeremy Hylton jeremy at zope.com
Sat Feb 14 00:11:50 EST 2004


Update of /cvs-repository/ZODB/src/persistent
In directory cvs.zope.org:/tmp/cvs-serv17593

Modified Files:
      Tag: zope3-zodb3-devel-branch
	wref.py 
Log Message:
Fix get() -- takes an optional second argument -- and write test.

Writing the test lead to fixing the constructor to match current
dict constructor and adding an update() method.  Since I wrote a test
for get(), I took the liberty of adding update() without writing a
test for it.


=== ZODB/src/persistent/wref.py 1.1.2.4 => 1.1.2.5 ===
--- ZODB/src/persistent/wref.py:1.1.2.4	Fri Feb 13 23:44:59 2004
+++ ZODB/src/persistent/wref.py	Sat Feb 14 00:11:49 2004
@@ -238,8 +238,15 @@
     #     It would be helpful if the data manager/connection cached these.
 
     
-    def __init__(self):
+    def __init__(self, adict=None, **kwargs):
         self.data = {}
+        if adict is not None:
+            keys = getattr(adict, "keys", None)
+            if keys is None:
+                adict = dict(adict)
+            self.update(adict)
+        if kwargs:
+            self.update(kwargs)
 
     def __getstate__(self):
         state = Persistent.__getstate__(self)
@@ -262,8 +269,20 @@
     def __delitem__(self, key):
         del self.data[WeakRef(key)]
 
-    def get(self, key):
-        return self.data.get(WeakRef(key))
+    def get(self, key, default=None):
+        """D.get(k[, d]) -> D[k] if k in D, else d.
+
+        >>> import ZODB.tests.util
+        >>> key = ZODB.tests.util.P("key")
+        >>> missing = ZODB.tests.util.P("missing")
+        >>> d = PersistentWeakKeyDictionary([(key, 1)])
+        >>> d.get(key)
+        1
+        >>> d.get(missing)
+        >>> d.get(missing, 12)
+        12
+        """
+        return self.data.get(WeakRef(key), default)
 
     def __contains__(self, key):
         return WeakRef(key) in self.data
@@ -272,5 +291,12 @@
         for k in self.data:
             yield k()
 
+    def update(self, adict):
+        if isinstance(adict, PersistentWeakKeyDictionary):
+            self.data.update(adict.update)
+        else:
+            for k, v in adict.items():
+                self.data[WeakRef(k)] = v
+        
     # XXX Someone else can fill out the rest of the methods, with tests. :)
     




More information about the Zodb-checkins mailing list