def _base(self, code, inputs, outputs, conditional_outputs, dep_graph):
        b = Block(code)

        # Compare inputs and outputs
        self.assertEqual(set(b.inputs), set(inputs))
        self.assertEqual(set(b.outputs), set(outputs))
        self.assertEqual(set(b.conditional_outputs), set(conditional_outputs))

        # Compare dependency graphs: since the real dep_graphs contain crazy
        # block objects, the given dep_graph specifies them by their index in
        # the sequence, e.g.
        #   for code
        #       'a=z; b=f(a,y)'
        #   the given dep_graph should be
        #       { '0':'z',  # Command 0 depends on input z
        #         '1':'0y', # Command 1 depends on command 0 and input y
        #         'a':'0',  # Output a depends on command 0
        #         'b':'1' } # Output b depends on command 1

        # Resolve indices to blocks
        def num_to_block(x):
            '''If x is number-like, lookup the xth block in b.sub_blocks;
            otherwise, leave x unchanged.'''
            try:
                return b.sub_blocks[int(x)]
            except ValueError:  # If int(x) fails
                return x
            except TypeError:  # In case b doesn't decompose
                return b

        dep_graph = graph.map(num_to_block, dep_graph)

        # Hand-make a trivial dep graph if 'b' doesn't have sub-blocks
        if b.sub_blocks is not None:
            b_dep_graph = b._dep_graph
        else:
            b_dep_graph = {}
            if b.inputs:
                b_dep_graph[b] = b.inputs
            for o in b.all_outputs:
                b_dep_graph[o] = set([b])

        # Compare dep_graphs
        self.assertEqual(
            map_values(set, b_dep_graph), map_values(set, dep_graph))
Example #2
0
    def _base(self, code, inputs, outputs, conditional_outputs,
              dep_graph):
        b = Block(code)

        # Compare inputs and outputs
        self.assertEqual(set(b.inputs), set(inputs))
        self.assertEqual(set(b.outputs), set(outputs))
        self.assertEqual(set(b.conditional_outputs), set(conditional_outputs))

        # Compare dependency graphs: since the real dep_graphs contain crazy
        # block objects, the given dep_graph specifies them by their index in
        # the sequence, e.g.
        #   for code
        #       'a=z; b=f(a,y)'
        #   the given dep_graph should be
        #       { '0':'z',  # Command 0 depends on input z
        #         '1':'0y', # Command 1 depends on command 0 and input y
        #         'a':'0',  # Output a depends on command 0
        #         'b':'1' } # Output b depends on command 1

        # Resolve indices to blocks
        def num_to_block(x):
            '''If x is number-like, lookup the xth block in b.sub_blocks;
            otherwise, leave x unchanged.'''
            try:
                return b.sub_blocks[int(x)]
            except ValueError: # If int(x) fails
                return x
            except TypeError: # In case b doesn't decompose
                return b
        dep_graph = graph.map(num_to_block, dep_graph)

        # Hand-make a trivial dep graph if 'b' doesn't have sub-blocks
        if b.sub_blocks is not None:
            b_dep_graph = b._dep_graph
        else:
            b_dep_graph = {}
            if b.inputs:
                b_dep_graph[b] = b.inputs
            for o in b.all_outputs:
                b_dep_graph[o] = set([b])

        # Compare dep_graphs
        self.assertEqual(map_values(set, b_dep_graph),
                         map_values(set, dep_graph))
 def test_map(self):
     self.assertEqual(G.map(str, {}), {})
     self.assertEqual(G.map(str, {1: [2, 3]}), {'1': ['2', '3']})
     self.assertEqual(G.map(lambda x: x, {1: [2, 3]}), {1: [2, 3]})
Example #4
0
 def test_map(self):
     self.assertEqual(G.map(str, {}), {})
     self.assertEqual(G.map(str, {1:[2,3]}), {'1':['2','3']})
     self.assertEqual(G.map(lambda x: x, {1:[2,3]}), {1:[2,3]})