home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_694 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.7 KB  |  449 lines

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