annotate Lib/_weakrefset.py @ 46567:ffeecbebf3ca

Block 2.6-only doc change.
author Georg Brandl <georg@python.org>
date Sat, 26 Apr 2008 18:28:08 +0000
parents e6069b24031e
children bbd30c492a99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
45159
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
1 # Access WeakSet through the weakref module.
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
2 # This code is separated-out because it is needed
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
3 # by abc.py to load everything else at startup.
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
4
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
5 from _weakref import ref
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
6
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
7 __all__ = ['WeakSet']
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
8
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
9 class WeakSet:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
10 def __init__(self, data=None):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
11 self.data = set()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
12 def _remove(item, selfref=ref(self)):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
13 self = selfref()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
14 if self is not None:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
15 self.data.discard(item)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
16 self._remove = _remove
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
17 if data is not None:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
18 self.update(data)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
19
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
20 def __iter__(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
21 for itemref in self.data:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
22 item = itemref()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
23 if item is not None:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
24 yield item
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
25
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
26 def __contains__(self, item):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
27 return ref(item) in self.data
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
28
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
29 def __reduce__(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
30 return (self.__class__, (list(self),),
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
31 getattr(self, '__dict__', None))
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
32
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
33 def add(self, item):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
34 self.data.add(ref(item, self._remove))
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
35
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
36 def clear(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
37 self.data.clear()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
38
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
39 def copy(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
40 return self.__class__(self)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
41
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
42 def pop(self):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
43 while True:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
44 itemref = self.data.pop()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
45 item = itemref()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
46 if item is not None:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
47 return item
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
48
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
49 def remove(self, item):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
50 self.data.remove(ref(item))
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
51
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
52 def discard(self, item):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
53 self.data.discard(ref(item))
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
54
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
55 def update(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
56 if isinstance(other, self.__class__):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
57 self.data.update(other.data)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
58 else:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
59 for element in other:
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
60 self.add(element)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
61 __ior__ = update
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
62
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
63 # Helper functions for simple delegating methods.
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
64 def _apply(self, other, method):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
65 if not isinstance(other, self.__class__):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
66 other = self.__class__(other)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
67 newdata = method(other.data)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
68 newset = self.__class__()
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
69 newset.data = newdata
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
70 return newset
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
71
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
72 def _apply_mutate(self, other, method):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
73 if not isinstance(other, self.__class__):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
74 other = self.__class__(other)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
75 method(other)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
76
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
77 def difference(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
78 return self._apply(other, self.data.difference)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
79 __sub__ = difference
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
80
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
81 def difference_update(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
82 self._apply_mutate(self, self.data.difference_update)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
83 __isub__ = difference_update
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
84
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
85 def intersection(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
86 return self._apply(other, self.data.intersection)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
87 __and__ = intersection
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
88
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
89 def intersection_update(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
90 self._apply_mutate(self, self.data.intersection_update)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
91 __iand__ = intersection_update
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
92
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
93 def issubset(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
94 return self.data.issubset(ref(item) for item in other)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
95 __lt__ = issubset
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
96
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
97 def issuperset(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
98 return self.data.issuperset(ref(item) for item in other)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
99 __gt__ = issuperset
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
100
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
101 def symmetric_difference(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
102 return self._apply(other, self.data.symmetric_difference)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
103 __xor__ = symmetric_difference
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
104
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
105 def symmetric_difference_update(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
106 self._apply_mutate(other, self.data.symmetric_difference_update)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
107 __ixor__ = symmetric_difference_update
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
108
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
109 def union(self, other):
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
110 self._apply_mutate(other, self.data.union)
e6069b24031e Moved WeakSet into a bootstap module use by abc.py.
Raymond Hettinger <python@rcn.com>
parents:
diff changeset
111 __or__ = union