Exemple #1
0
 def remove_node(self, node):
     """Removes node and all edges connected to it."""
     if node not in self.__adjacency_list:
         raise ValueError("Unknown node: " + str(node))
     # remove node (and all out-edges) from adjacency list
     del self.__adjacency_list[node]
     # remove all in-edges from adjacency list
     for n in self.__adjacency_list.keys():
         self.__adjacency_list[n] = HashSet(
             filter(lambda x, node=node: x is not node,
                    self.__adjacency_list[n].list()))
     # remove all refering pairs in label map
     for label in self.__label_map.keys():
         lm = HashSet(filter(lambda x,node=node: \
                             (x[0] is not node) and (x[1] is not node),
                             self.__label_map[label].list()))
         # remove the entry completely if the label is now unused
         if lm.empty():
             del self.__label_map[label]
         else:
             self.__label_map[label] = lm
     # remove all refering entries in edge map
     for edge in self.__edge_map.keys():
         if edge[0] is node or edge[1] is node:
             del self.__edge_map[edge]
 def testContains(self):
     n = HashSet()
     self.assertTrue('a' not in n, "element in empty set")
     self.assertTrue(not n.contains('a'), "element in empty set (2)")
     a = HashSet(['a','b','c','d'])
     self.assertTrue('a' in a, "contained element not found")
     self.assertTrue('d' in a, "contained element not found")
     self.assertTrue('e' not in a, "not contained element found")
     self.assertTrue(68 not in a, "not contained element found")
     self.assertTrue(a.contains('a'), "contained element not found (2)")
     self.assertTrue(a.contains('d'), "contained element not found (2)")
     self.assertTrue(not a.contains('e'), "not contained element found (2)")
     self.assertTrue(not a.contains(68), "not contained element found (2)")
Exemple #3
0
 def __init__(self, nodes=[]):
     """Initializes a new Graph object."""
     self.__adjacency_list = {}  # maps parent -> set of child objects
     for n in nodes:
         self.__adjacency_list[n] = HashSet()
     self.__label_map = {}  # maps label -> set of (parent, child) pairs
     self.__edge_map = {}  # maps (parent, child) pair -> label
 def testList(self):
     a = HashSet(['a', 'b', 'c', 'd', 'e'])
     l = a.list()
     l.sort()
     self.assertEqual(l, ['a', 'b', 'c', 'd', 'e'], "incorrect list")
     l = []
     self.assertTrue('e' in a, "set rep exposure")
Exemple #5
0
 def __init__(self, reactants={}, catalysts=[], reversible=0, data=None):
     """Initializes a new Reaction object."""
     # enforce invariants on reactants:
     self.reactants = reactants.copy()
     for r in self.reactants.keys():
         if self.reactants[r] == 0:
             del self.reactants[r]
     self.catalysts = HashSet(catalysts).list()
     self.data = data
     self.reversible = reversible
 def testLen(self):
     a = HashSet()
     self.assertEqual(len(a), 0, "incorrect default size")
     a.add('a')
     a.add('b')
     self.assertEqual(len(a), 2, "incorrect size")
     a.remove('b')
     self.assertEqual(len(a), 1, "incorrect size after removal")
     a.add('a')
     self.assertEqual(len(a), 1, "incorrect size after duplicate add")
 def testLen(self):
     a = HashSet()
     self.failUnless(len(a) == 0, "incorrect default size")
     a.add('a')
     a.add('b')
     self.failUnless(len(a) == 2, "incorrect size")
     a.remove('b')
     self.failUnless(len(a) == 1, "incorrect size after removal")
     a.add('a')
     self.failUnless(len(a) == 1, "incorrect size after duplicate add")
Exemple #8
0
 def add_edge(self, source, to, label=None):
     """Adds an edge to this graph."""
     if source not in self.__adjacency_list:
         raise ValueError("Unknown <from> node: " + str(source))
     if to not in self.__adjacency_list:
         raise ValueError("Unknown <to> node: " + str(to))
     edge = (to, label)
     self.__adjacency_list[source].add(edge)
     if label not in self.__label_map:
         self.__label_map[label] = HashSet()
     self.__label_map[label].add((source, to))
Exemple #9
0
 def __init__(self, reactants={}, catalysts=[], reversible=0, data=None):
     """Initializes a new Reaction object."""
     # enforce invariants on reactants:
     self.reactants = reactants.copy()
     # loop over original, edit the copy
     for r, value in reactants.iteritems():
         if value == 0:
             del self.reactants[r]
     self.catalysts = HashSet(catalysts).list()
     self.data = data
     self.reversible = reversible
Exemple #10
0
 def add_node(self, node):
     """Adds a node to this graph."""
     if node not in self.__adjacency_list:
         self.__adjacency_list[node] = HashSet()
Exemple #11
0
 def parents(self, child):
     """Returns a list of unique parents for child."""
     s = HashSet([x[0] for x in self.parent_edges(child)])
     return s.list()
 def testSetOps(self):
     n = HashSet()
     a = HashSet(['a', 'b', 'c'])
     b = HashSet(['a', 'd', 'e', 'f'])
     c = HashSet(['g', 'h'])
     # union
     self.assertEqual(a.union(b), HashSet(['a','b','c','d','e','f']), "incorrect union")
     self.assertEqual(a.union(n), a, "incorrect union with empty set")
     # intersection
     self.assertEqual(a.intersection(b), HashSet(['a']), "incorrect intersection")
     self.assertEqual(a.intersection(c), HashSet(), "incorrect intersection")
     self.assertEqual(a.intersection(n), HashSet(), "incorrect intersection with empty set")
     # difference
     self.assertEqual(a.difference(b), HashSet(['b','c']), "incorrect difference")
     self.assertEqual(a.difference(c), HashSet(['a','b','c']), "incorrect difference")
     self.assertEqual(b.difference(a), HashSet(['d','e','f']), "incorrect difference")
     # cartesian product
     self.assertEqual(a.cartesian(c), HashSet([('a','g'),('a','h'),
                                               ('b','g'),('b','h'),
                                               ('c','g'),('c','h')]),
                      "incorrect cartesian product")
     self.assertEqual(a.cartesian(n), HashSet(), "incorrect cartesian product")
 def testEquals(self):
     self.assertEqual(HashSet(['a','b']), HashSet(['b','a']), "not equal to similar")
     self.assertEqual(HashSet(), HashSet(), "empty set not equal to similar")
     self.assertNotEqual(HashSet(['a','b','c']), HashSet(), "non-empty equal to empty")
     self.assertNotEqual(HashSet(['a','b']), HashSet(['a','b','c']), "equal to superset")
     self.assertNotEqual(HashSet(['a','b']), HashSet(['a']), "equal to subset")
Exemple #14
0
 def children(self, parent):
     """Returns a list of unique children for parent."""
     s = HashSet([x[0] for x in self.child_edges(parent)])
     return s.list()
Exemple #15
0
 def species(self):
     """Returns a list of the species in this system."""
     s = HashSet(
         reduce(lambda s, x: s + x, [x.species() for x in self.reactions()],
                []))
     return s.list()
Exemple #16
0
 def __init__(self, reactions=[]):
     """Initializes a new System object."""
     self.__reactions = HashSet(reactions)