Package Bio :: Package Pathway :: Package Rep :: Module HashSet
[hide private]
[frames] | no frames]

Source Code for Module Bio.Pathway.Rep.HashSet

  1  # Copyright 2001 by Tarjei Mikkelsen. All rights reserved. 
  2  # This code is part of the Biopython distribution and governed by its 
  3  # license.  Please see the LICENSE file that should have been included 
  4  # as part of this package. 
  5   
  6  import warnings 
  7  warnings.warn("The module Bio.Pathway.Rep.HashSet is now deprecated, " 
  8                "and will be removed in a future release of Biopython. " 
  9                "Use Python's built in set object instead.", 
 10                DeprecationWarning) 
 11   
 12   
13 -class HashSet(object):
14 """A set abstraction supporting the basic set operations. 15 16 This implementation requires that all elements are hashable, 17 which implies that elements must not mutate while contained. 18 """
19 - def __init__(self, elements = []):
20 """Initializes a new HashSet.""" 21 self._elements = {} 22 for e in elements: 23 self._elements[e] = 1
24
25 - def __contains__(self, element):
26 """Returns true iff this set contains element.""" 27 return element in self._elements
28
29 - def __eq__(self, set):
30 """Returns true iff x == y for all elements in self, set.""" 31 if not isinstance(set, HashSet): 32 return 0 33 for x in self.list(): 34 if not (x in set): 35 return 0 36 for x in set.list(): 37 if not (x in self): 38 return 0 39 return 1
40
41 - def __len__(self):
42 """Returns the number of elements in this set.""" 43 return len(self._elements)
44
45 - def __ne__(self, set):
46 """Returns true iff this set is not equal to set.""" 47 return not self.__eq__(set)
48
49 - def __repr__(self):
50 """Returns a debugging string representation of this set.""" 51 return "HashSet(" + repr(self.list()) + ")"
52
53 - def __str__(self):
54 """Returns a string representation of this set.""" 55 return "{" + ",".join(map(str, self.list())) + "}"
56 57 # Element access: 58
59 - def add(self, element):
60 """Adds element to this set.""" 61 self._elements[element] = 1
62
63 - def contains(self, element):
64 """Returns true iff this set contains element.""" 65 return self.__contains__(element)
66
67 - def remove(self, element):
68 """Removes element from this set.""" 69 try: 70 del self._elements[element] 71 except KeyError: 72 pass
73
74 - def list(self):
75 """Returns the elements of this set in a list.""" 76 return self._elements.keys()
77 78 # Information: 79
80 - def empty(self):
81 """Returns true iff this set is empty.""" 82 return len(self._elements) == 0
83 84 # Set operations: 85
86 - def union(self, s):
87 """Returns the union of this set and s.""" 88 return HashSet(self.list() + s.list())
89
90 - def intersection(self, s):
91 """Returns the intersection of this set and s.""" 92 return HashSet(filter(lambda e,s=s: e in s, self.list()))
93
94 - def difference(self, s):
95 """Returns the difference of this set and s.""" 96 return HashSet(filter(lambda e,s=s: e not in s, self.list()))
97
98 - def cartesian(self,s):
99 """Returns the Cartesian product of this set and s.""" 100 p = [] 101 for i in self.list(): 102 for j in s.list(): 103 p.append((i,j)) 104 return HashSet(p)
105