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

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. from itertools import ifilter, ifilterfalse
  5. __all__ = [
  6.     'BaseSet',
  7.     'Set',
  8.     'ImmutableSet']
  9. import warnings
  10. warnings.warn('the sets module is deprecated', DeprecationWarning, stacklevel = 2)
  11.  
  12. class BaseSet(object):
  13.     __slots__ = [
  14.         '_data']
  15.     
  16.     def __init__(self):
  17.         if self.__class__ is BaseSet:
  18.             raise TypeError, 'BaseSet is an abstract class.  Use Set or ImmutableSet.'
  19.  
  20.     
  21.     def __len__(self):
  22.         return len(self._data)
  23.  
  24.     
  25.     def __repr__(self):
  26.         return self._repr()
  27.  
  28.     __str__ = __repr__
  29.     
  30.     def _repr(self, sorted = False):
  31.         elements = self._data.keys()
  32.         if sorted:
  33.             elements.sort()
  34.         return '%s(%r)' % (self.__class__.__name__, elements)
  35.  
  36.     
  37.     def __iter__(self):
  38.         return self._data.iterkeys()
  39.  
  40.     
  41.     def __cmp__(self, other):
  42.         raise TypeError, "can't compare sets using cmp()"
  43.  
  44.     
  45.     def __eq__(self, other):
  46.         if isinstance(other, BaseSet):
  47.             return self._data == other._data
  48.         return None
  49.  
  50.     
  51.     def __ne__(self, other):
  52.         if isinstance(other, BaseSet):
  53.             return self._data != other._data
  54.         return None
  55.  
  56.     
  57.     def copy(self):
  58.         result = self.__class__()
  59.         result._data.update(self._data)
  60.         return result
  61.  
  62.     __copy__ = copy
  63.     
  64.     def __deepcopy__(self, memo):
  65.         deepcopy = deepcopy
  66.         import copy
  67.         result = self.__class__()
  68.         memo[id(self)] = result
  69.         data = result._data
  70.         value = True
  71.         for elt in self:
  72.             data[deepcopy(elt, memo)] = value
  73.         
  74.         return result
  75.  
  76.     
  77.     def __or__(self, other):
  78.         if not isinstance(other, BaseSet):
  79.             return NotImplemented
  80.         return None.union(other)
  81.  
  82.     
  83.     def union(self, other):
  84.         result = self.__class__(self)
  85.         result._update(other)
  86.         return result
  87.  
  88.     
  89.     def __and__(self, other):
  90.         if not isinstance(other, BaseSet):
  91.             return NotImplemented
  92.         return None.intersection(other)
  93.  
  94.     
  95.     def intersection(self, other):
  96.         if not isinstance(other, BaseSet):
  97.             other = Set(other)
  98.         if len(self) <= len(other):
  99.             little = self
  100.             big = other
  101.         else:
  102.             little = other
  103.             big = self
  104.         common = ifilter(big._data.__contains__, little)
  105.         return self.__class__(common)
  106.  
  107.     
  108.     def __xor__(self, other):
  109.         if not isinstance(other, BaseSet):
  110.             return NotImplemented
  111.         return None.symmetric_difference(other)
  112.  
  113.     
  114.     def symmetric_difference(self, other):
  115.         result = self.__class__()
  116.         data = result._data
  117.         value = True
  118.         selfdata = self._data
  119.         
  120.         try:
  121.             otherdata = other._data
  122.         except AttributeError:
  123.             otherdata = Set(other)._data
  124.  
  125.         for elt in ifilterfalse(otherdata.__contains__, selfdata):
  126.             data[elt] = value
  127.         
  128.         for elt in ifilterfalse(selfdata.__contains__, otherdata):
  129.             data[elt] = value
  130.         
  131.         return result
  132.  
  133.     
  134.     def __sub__(self, other):
  135.         if not isinstance(other, BaseSet):
  136.             return NotImplemented
  137.         return None.difference(other)
  138.  
  139.     
  140.     def difference(self, other):
  141.         result = self.__class__()
  142.         data = result._data
  143.         
  144.         try:
  145.             otherdata = other._data
  146.         except AttributeError:
  147.             otherdata = Set(other)._data
  148.  
  149.         value = True
  150.         for elt in ifilterfalse(otherdata.__contains__, self):
  151.             data[elt] = value
  152.         
  153.         return result
  154.  
  155.     
  156.     def __contains__(self, element):
  157.         
  158.         try:
  159.             return element in self._data
  160.         except TypeError:
  161.             transform = getattr(element, '__as_temporarily_immutable__', None)
  162.             if transform is None:
  163.                 raise 
  164.             return transform() in self._data
  165.  
  166.  
  167.     
  168.     def issubset(self, other):
  169.         self._binary_sanity_check(other)
  170.         if len(self) > len(other):
  171.             return False
  172.         for elt in ifilterfalse(other._data.__contains__, self):
  173.             return False
  174.         return True
  175.  
  176.     
  177.     def issuperset(self, other):
  178.         self._binary_sanity_check(other)
  179.         if len(self) < len(other):
  180.             return False
  181.         for elt in ifilterfalse(self._data.__contains__, other):
  182.             return False
  183.         return True
  184.  
  185.     __le__ = issubset
  186.     __ge__ = issuperset
  187.     
  188.     def __lt__(self, other):
  189.         self._binary_sanity_check(other)
  190.         if len(self) < len(other):
  191.             pass
  192.         return self.issubset(other)
  193.  
  194.     
  195.     def __gt__(self, other):
  196.         self._binary_sanity_check(other)
  197.         if len(self) > len(other):
  198.             pass
  199.         return self.issuperset(other)
  200.  
  201.     __hash__ = None
  202.     
  203.     def _binary_sanity_check(self, other):
  204.         if not isinstance(other, BaseSet):
  205.             raise TypeError, 'Binary operation only permitted between sets'
  206.  
  207.     
  208.     def _compute_hash(self):
  209.         result = 0
  210.         for elt in self:
  211.             result ^= hash(elt)
  212.         
  213.         return result
  214.  
  215.     
  216.     def _update(self, iterable):
  217.         data = self._data
  218.         if isinstance(iterable, BaseSet):
  219.             data.update(iterable._data)
  220.             return None
  221.         value = None
  222.         if type(iterable) in (list, tuple, xrange):
  223.             it = iter(iterable)
  224.             while True:
  225.                 
  226.                 try:
  227.                     for element in it:
  228.                         data[element] = value
  229.                     
  230.                     return None
  231.                 continue
  232.                 except TypeError:
  233.                     transform = getattr(element, '__as_immutable__', None)
  234.                     if transform is None:
  235.                         raise 
  236.                     data[transform()] = value
  237.                     continue
  238.                 
  239.  
  240.         else:
  241.             for element in iterable:
  242.                 
  243.                 try:
  244.                     data[element] = value
  245.                 continue
  246.                 except TypeError:
  247.                     transform = getattr(element, '__as_immutable__', None)
  248.                     if transform is None:
  249.                         raise 
  250.                     data[transform()] = value
  251.                     continue
  252.                 
  253.  
  254.             
  255.  
  256.  
  257.  
  258. class ImmutableSet(BaseSet):
  259.     __slots__ = [
  260.         '_hashcode']
  261.     
  262.     def __init__(self, iterable = None):
  263.         self._hashcode = None
  264.         self._data = { }
  265.         if iterable is not None:
  266.             self._update(iterable)
  267.  
  268.     
  269.     def __hash__(self):
  270.         if self._hashcode is None:
  271.             self._hashcode = self._compute_hash()
  272.         return self._hashcode
  273.  
  274.     
  275.     def __getstate__(self):
  276.         return (self._data, self._hashcode)
  277.  
  278.     
  279.     def __setstate__(self, state):
  280.         (self._data, self._hashcode) = state
  281.  
  282.  
  283.  
  284. class Set(BaseSet):
  285.     __slots__ = []
  286.     
  287.     def __init__(self, iterable = None):
  288.         self._data = { }
  289.         if iterable is not None:
  290.             self._update(iterable)
  291.  
  292.     
  293.     def __getstate__(self):
  294.         return (self._data,)
  295.  
  296.     
  297.     def __setstate__(self, data):
  298.         (self._data,) = data
  299.  
  300.     
  301.     def __ior__(self, other):
  302.         self._binary_sanity_check(other)
  303.         self._data.update(other._data)
  304.         return self
  305.  
  306.     
  307.     def union_update(self, other):
  308.         self._update(other)
  309.  
  310.     
  311.     def __iand__(self, other):
  312.         self._binary_sanity_check(other)
  313.         self._data = (self & other)._data
  314.         return self
  315.  
  316.     
  317.     def intersection_update(self, other):
  318.         if isinstance(other, BaseSet):
  319.             self &= other
  320.         else:
  321.             self._data = self.intersection(other)._data
  322.  
  323.     
  324.     def __ixor__(self, other):
  325.         self._binary_sanity_check(other)
  326.         self.symmetric_difference_update(other)
  327.         return self
  328.  
  329.     
  330.     def symmetric_difference_update(self, other):
  331.         data = self._data
  332.         value = True
  333.         if not isinstance(other, BaseSet):
  334.             other = Set(other)
  335.         if self is other:
  336.             self.clear()
  337.         for elt in other:
  338.             if elt in data:
  339.                 del data[elt]
  340.                 continue
  341.             data[elt] = value
  342.         
  343.  
  344.     
  345.     def __isub__(self, other):
  346.         self._binary_sanity_check(other)
  347.         self.difference_update(other)
  348.         return self
  349.  
  350.     
  351.     def difference_update(self, other):
  352.         data = self._data
  353.         if not isinstance(other, BaseSet):
  354.             other = Set(other)
  355.         if self is other:
  356.             self.clear()
  357.         for elt in ifilter(data.__contains__, other):
  358.             del data[elt]
  359.         
  360.  
  361.     
  362.     def update(self, iterable):
  363.         self._update(iterable)
  364.  
  365.     
  366.     def clear(self):
  367.         self._data.clear()
  368.  
  369.     
  370.     def add(self, element):
  371.         
  372.         try:
  373.             self._data[element] = True
  374.         except TypeError:
  375.             transform = getattr(element, '__as_immutable__', None)
  376.             if transform is None:
  377.                 raise 
  378.             self._data[transform()] = True
  379.  
  380.  
  381.     
  382.     def remove(self, element):
  383.         
  384.         try:
  385.             del self._data[element]
  386.         except TypeError:
  387.             transform = getattr(element, '__as_temporarily_immutable__', None)
  388.             if transform is None:
  389.                 raise 
  390.             del self._data[transform()]
  391.  
  392.  
  393.     
  394.     def discard(self, element):
  395.         
  396.         try:
  397.             self.remove(element)
  398.         except KeyError:
  399.             pass
  400.  
  401.  
  402.     
  403.     def pop(self):
  404.         return self._data.popitem()[0]
  405.  
  406.     
  407.     def __as_immutable__(self):
  408.         return ImmutableSet(self)
  409.  
  410.     
  411.     def __as_temporarily_immutable__(self):
  412.         return _TemporarilyImmutableSet(self)
  413.  
  414.  
  415.  
  416. class _TemporarilyImmutableSet(BaseSet):
  417.     
  418.     def __init__(self, set):
  419.         self._set = set
  420.         self._data = set._data
  421.  
  422.     
  423.     def __hash__(self):
  424.         return self._set._compute_hash()
  425.  
  426.  
  427.