Skip to content

Instantly share code, notes, and snippets.

@loic
Created January 27, 2014 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loic/8644072 to your computer and use it in GitHub Desktop.
Save loic/8644072 to your computer and use it in GitHub Desktop.
WeakKeyDictionary
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 260, in remove
del self.data[k]
KeyError: <weakref at 0x10be01a48; dead>
self.data:
{}
k:
<weakref at 0x10be01a48; dead>
Exception KeyError: KeyError(<weakref at 0x10be01a48; dead>,) in <function remove at 0x1056f8aa0> ignored
WeakKeyDictionary
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 260, in remove
del self.data[k]
KeyError: <weakref at 0x10be01940; dead>
self.data:
{}
k:
<weakref at 0x10be01940; dead>
class WeakValueDictionary(UserDict.UserDict):
def __init__(self, *args, **kw):
def remove(wr, selfref=ref(self)):
try:
self = selfref()
if self is not None:
del self.data[wr.key]
except:
print "WeakValueDictionary:"
import traceback
traceback.print_exc()
print "self.data:"
print self.data
print "wr.key:"
print wr.key
print
raise
self._remove = remove
UserDict.UserDict.__init__(self, *args, **kw)
class WeakKeyDictionary(UserDict.UserDict):
def __init__(self, dict=None):
self.data = {}
def remove(k, selfref=ref(self)):
try:
self = selfref()
if self is not None:
del self.data[k]
except:
print "WeakKeyDictionary"
import traceback
traceback.print_exc()
print "self.data:"
print self.data
print "k:"
print k
print
raise
self._remove = remove
if dict is not None: self.update(dict)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment