http://python.sourceforge.net/peps/pep-0205.html
"""
+# Naming convention: Variables named "wr" are weak reference objects;
+# they are called this instead of "ref" to avoid name collisions with
+# the module-global ref() function imported from _weakref.
+
import UserDict
from _weakref import \
"WeakKeyDictionary", "ReferenceType", "ProxyType",
"CallableProxyType", "ProxyTypes", "WeakValueDictionary"]
+
class WeakValueDictionary(UserDict.UserDict):
+ """Mapping class that references values weakly.
+ Entries in the dictionary will be discarded when no strong
+ reference to the value exists anymore
+ """
# We inherit the constructor without worrying about the input
# dictionary; since it uses our .update() method, we get the right
# checks (if the other dictionary is a WeakValueDictionary,
def copy(self):
new = WeakValueDictionary()
- for key, ref in self.data.items():
- o = ref()
+ for key, wr in self.data.items():
+ o = wr()
if o is not None:
new[key] = o
return new
def get(self, key, default=None):
try:
- ref = self.data[key]
+ wr = self.data[key]
except KeyError:
return default
else:
- o = ref()
+ o = wr()
if o is None:
# This should only happen
return default
def items(self):
L = []
- for key, ref in self.data.items():
- o = ref()
+ for key, wr in self.data.items():
+ o = wr()
if o is not None:
L.append((key, o))
return L
def popitem(self):
while 1:
- key, ref = self.data.popitem()
- o = ref()
+ key, wr = self.data.popitem()
+ o = wr()
if o is not None:
return key, o
def setdefault(self, key, default):
try:
- ref = self.data[key]
+ wr = self.data[key]
except KeyError:
def remove(o, data=self.data, key=key):
del data[key]
- ref = ref(default, remove)
- self.data[key] = ref
+ self.data[key] = ref(default, remove)
return default
else:
- return ref()
+ return wr()
def update(self, dict):
d = self.data
- L = []
for key, o in dict.items():
def remove(o, data=d, key=key):
del data[key]
- L.append((key, ref(o, remove)))
- for key, r in L:
- d[key] = r
+ d[key] = ref(o, remove)
def values(self):
L = []
- for ref in self.data.values():
- o = ref()
+ for wr in self.data.values():
+ o = wr()
if o is not None:
L.append(o)
return L
class WeakKeyDictionary(UserDict.UserDict):
+ """ Mapping class that references keys weakly.
+
+ Entries in the dictionary will be discarded when there is no
+ longer a strong reference to the key. This can be used to
+ associate additional data with an object owned by other parts of
+ an application without adding attributes to those objects. This
+ can be especially useful with objects that override attribute
+ accesses.
+ """
def __init__(self, dict=None):
self.data = {}
def keys(self):
L = []
- for ref in self.data.keys():
- o = ref()
+ for wr in self.data.keys():
+ o = wr()
if o is not None:
L.append(o)
return L
def update(self, dict):
d = self.data
- L = []
for key, value in dict.items():
- L.append((ref(key, self._remove), value))
- for key, r in L:
- d[key] = r
+ d[ref(key, self._remove)] = value
+
# no longer needed
del UserDict