def test_topological_sort_cycle(self): n1 = Node("1") n2 = Node("2") n1.incoming = [n2] n2.incoming = [n1] generator = cfg_utils.topological_sort([n1, n2]) self.assertRaises(ValueError, list, generator)
def test_topological_sort(self): n1 = Node("1") n2 = Node("2", n1) n3 = Node("3", n2) n4 = Node("4", n2, n3) for permutation in itertools.permutations([n1, n2, n3, n4]): self.assertEqual(list(cfg_utils.topological_sort(permutation)), [n1, n2, n3, n4])
def testTopologicalSortSubCycle(self): n1 = Node("1") n2 = Node("2") n3 = Node("3") n1.incoming = [n2] n2.incoming = [n1] n3.incoming = [n1, n2] generator = cfg_utils.topological_sort([n1, n2, n3]) self.assertRaises(ValueError, list, generator)
def process(self): """Postprocesses all options in self.input_options. This will iterate through all options in self.input_options and make them attributes on self.output_options. If, for an option {name}, there is a _store_{name} method on this class, it'll call the method instead of storing the option directly. """ # Because of the mutual dependency between input and output, we process # them outside of the normal flow. if hasattr(self.input_options, "input"): self.input_options.input, output = self._parse_arguments( self.input_options.input) else: output = None if output and "output" in self.names: if getattr(self.input_options, "output", None): self.error("x:y notation not allowed with -o") self.input_options.output = output # prepare function objects for topological sort: class Node(object): # pylint: disable=g-wrong-blank-lines def __init__(self, name, processor): # pylint: disable=g-wrong-blank-lines self.name = name self.processor = processor nodes = { name: Node(name, getattr(self, "_store_" + name, None)) for name in self.names } for f in nodes.values(): if f.processor: # option has a _store_{name} method dependencies = uses.lookup.get(f.processor.__name__) if dependencies: # that method has a @uses decorator f.incoming = tuple(nodes[use] for use in dependencies) # process the option list in the right order: for node in cfg_utils.topological_sort(nodes.values()): value = getattr(self.input_options, node.name) if node.processor is not None: node.processor(value) else: setattr(self.output_options, node.name, value)
def test_topological_sort_getattr(self): self.assertEqual(list(cfg_utils.topological_sort([1])), [1])
def test_topological_sort2(self): n1 = Node("1") n2 = Node("2", n1) self.assertEqual( list(cfg_utils.topological_sort([n1, n2, 3, 4]))[-1], n2)