home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / _weakrefset.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  9.0 KB  |  248 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. from _weakref import ref
  5. __all__ = [
  6.     'WeakSet']
  7.  
  8. class _IterationGuard(object):
  9.     
  10.     def __init__(self, weakcontainer):
  11.         self.weakcontainer = ref(weakcontainer)
  12.  
  13.     
  14.     def __enter__(self):
  15.         w = self.weakcontainer()
  16.         if w is not None:
  17.             w._iterating.add(self)
  18.         return self
  19.  
  20.     
  21.     def __exit__(self, e, t, b):
  22.         w = self.weakcontainer()
  23.         if w is not None:
  24.             s = w._iterating
  25.             s.remove(self)
  26.             if not s:
  27.                 w._commit_removals()
  28.             
  29.  
  30.  
  31.  
  32. class WeakSet(object):
  33.     
  34.     def __init__(self, data = None):
  35.         self.data = set()
  36.         
  37.         def _remove(item, selfref = ref(self)):
  38.             self = selfref()
  39.             if self is not None:
  40.                 if self._iterating:
  41.                     self._pending_removals.append(item)
  42.                 else:
  43.                     self.data.discard(item)
  44.  
  45.         self._remove = _remove
  46.         self._pending_removals = []
  47.         self._iterating = set()
  48.         if data is not None:
  49.             self.update(data)
  50.  
  51.     
  52.     def _commit_removals(self):
  53.         l = self._pending_removals
  54.         discard = self.data.discard
  55.         while l:
  56.             discard(l.pop())
  57.  
  58.     
  59.     def __iter__(self):
  60.         with _IterationGuard(self):
  61.             for itemref in self.data:
  62.                 item = itemref()
  63.                 if item is not None:
  64.                     yield item
  65.                     continue
  66.  
  67.     
  68.     def __len__(self):
  69.         return sum((lambda .0: pass)(self.data))
  70.  
  71.     
  72.     def __contains__(self, item):
  73.         return ref(item) in self.data
  74.  
  75.     
  76.     def __reduce__(self):
  77.         return (self.__class__, (list(self),), getattr(self, '__dict__', None))
  78.  
  79.     __hash__ = None
  80.     
  81.     def add(self, item):
  82.         if self._pending_removals:
  83.             self._commit_removals()
  84.         self.data.add(ref(item, self._remove))
  85.  
  86.     
  87.     def clear(self):
  88.         if self._pending_removals:
  89.             self._commit_removals()
  90.         self.data.clear()
  91.  
  92.     
  93.     def copy(self):
  94.         return self.__class__(self)
  95.  
  96.     
  97.     def pop(self):
  98.         if self._pending_removals:
  99.             self._commit_removals()
  100.         while True:
  101.             
  102.             try:
  103.                 itemref = self.data.pop()
  104.             except KeyError:
  105.                 raise KeyError('pop from empty WeakSet')
  106.  
  107.             item = itemref()
  108.             if item is not None:
  109.                 return item
  110.  
  111.     
  112.     def remove(self, item):
  113.         if self._pending_removals:
  114.             self._commit_removals()
  115.         self.data.remove(ref(item))
  116.  
  117.     
  118.     def discard(self, item):
  119.         if self._pending_removals:
  120.             self._commit_removals()
  121.         self.data.discard(ref(item))
  122.  
  123.     
  124.     def update(self, other):
  125.         if self._pending_removals:
  126.             self._commit_removals()
  127.         if isinstance(other, self.__class__):
  128.             self.data.update(other.data)
  129.         else:
  130.             for element in other:
  131.                 self.add(element)
  132.             
  133.  
  134.     
  135.     def __ior__(self, other):
  136.         self.update(other)
  137.         return self
  138.  
  139.     
  140.     def _apply(self, other, method):
  141.         if not isinstance(other, self.__class__):
  142.             other = self.__class__(other)
  143.         newdata = method(other.data)
  144.         newset = self.__class__()
  145.         newset.data = newdata
  146.         return newset
  147.  
  148.     
  149.     def difference(self, other):
  150.         return self._apply(other, self.data.difference)
  151.  
  152.     __sub__ = difference
  153.     
  154.     def difference_update(self, other):
  155.         if self._pending_removals:
  156.             self._commit_removals()
  157.         if self is other:
  158.             self.data.clear()
  159.         else:
  160.             self.data.difference_update((lambda .0: pass)(other))
  161.  
  162.     
  163.     def __isub__(self, other):
  164.         if self._pending_removals:
  165.             self._commit_removals()
  166.         if self is other:
  167.             self.data.clear()
  168.         else:
  169.             self.data.difference_update((lambda .0: pass)(other))
  170.         return self
  171.  
  172.     
  173.     def intersection(self, other):
  174.         return self._apply(other, self.data.intersection)
  175.  
  176.     __and__ = intersection
  177.     
  178.     def intersection_update(self, other):
  179.         if self._pending_removals:
  180.             self._commit_removals()
  181.         self.data.intersection_update((lambda .0: pass)(other))
  182.  
  183.     
  184.     def __iand__(self, other):
  185.         if self._pending_removals:
  186.             self._commit_removals()
  187.         self.data.intersection_update((lambda .0: pass)(other))
  188.         return self
  189.  
  190.     
  191.     def issubset(self, other):
  192.         return self.data.issubset((lambda .0: pass)(other))
  193.  
  194.     __lt__ = issubset
  195.     
  196.     def __le__(self, other):
  197.         return self.data <= set((lambda .0: pass)(other))
  198.  
  199.     
  200.     def issuperset(self, other):
  201.         return self.data.issuperset((lambda .0: pass)(other))
  202.  
  203.     __gt__ = issuperset
  204.     
  205.     def __ge__(self, other):
  206.         return self.data >= set((lambda .0: pass)(other))
  207.  
  208.     
  209.     def __eq__(self, other):
  210.         if not isinstance(other, self.__class__):
  211.             return NotImplemented
  212.         return None.data == set((lambda .0: pass)(other))
  213.  
  214.     
  215.     def symmetric_difference(self, other):
  216.         return self._apply(other, self.data.symmetric_difference)
  217.  
  218.     __xor__ = symmetric_difference
  219.     
  220.     def symmetric_difference_update(self, other):
  221.         if self._pending_removals:
  222.             self._commit_removals()
  223.         if self is other:
  224.             self.data.clear()
  225.         else:
  226.             self.data.symmetric_difference_update((lambda .0: pass)(other))
  227.  
  228.     
  229.     def __ixor__(self, other):
  230.         if self._pending_removals:
  231.             self._commit_removals()
  232.         if self is other:
  233.             self.data.clear()
  234.         else:
  235.             self.data.symmetric_difference_update((lambda .0: pass)(other))
  236.         return self
  237.  
  238.     
  239.     def union(self, other):
  240.         return self._apply(other, self.data.union)
  241.  
  242.     __or__ = union
  243.     
  244.     def isdisjoint(self, other):
  245.         return len(self.intersection(other)) == 0
  246.  
  247.  
  248.