提交 dbe755dc authored 作者: Frederic Bastien's avatar Frederic Bastien

Make OrderedSet pickable

(so fix tests broken by the previous commit that use them more frequently)
上级 f40080b8
...@@ -42,10 +42,34 @@ if MutableSet is not None: ...@@ -42,10 +42,34 @@ if MutableSet is not None:
## {{{ http://code.activestate.com/recipes/576696/ (r5) ## {{{ http://code.activestate.com/recipes/576696/ (r5)
import collections import collections
from weakref import proxy from weakref import proxy
import weakref
class Link(object): class Link(object):
__slots__ = 'prev', 'next', 'key', '__weakref__' __slots__ = 'prev', 'next', 'key', '__weakref__'
def __getstate__(self):
ret = [self.prev, self.next,
isinstance(self.prev, weakref.ProxyType),
isinstance(self.next, weakref.ProxyType),
]
try:
ret.append(self.key)
except AttributeError:
pass
return ret
def __setstate__(self, state):
if state[2]:
self.prev = proxy(state[0])
else:
self.prev = state[0]
if state[3]:
self.next = proxy(state[1])
else:
self.next = state[1]
if len(state) == 5:
self.key = state[4]
class OrderedSet(collections.MutableSet): class OrderedSet(collections.MutableSet):
'Set the remembers the order elements were added' 'Set the remembers the order elements were added'
# Big-O running times for all methods are the same as for regular sets. # Big-O running times for all methods are the same as for regular sets.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论