def test_connect_and_dicsonnect_nodes(): """Connect and disconnect nodes.""" n1 = NodeForTesting() n2 = NodeForTesting() out_plug_a = OutputPlug('out', n1) out_plug_b = OutputPlug('out', n1) in_plug_a = InputPlug('in', n2) in_plug_b = InputPlug('in', n2) # Connect the out to the in out_plug_a >> in_plug_a assert 1 == len(out_plug_a.connections) assert 1 == len(in_plug_a.connections) # Connect the same nodes multiple times out_plug_a >> in_plug_a assert 1 == len(out_plug_a.connections) assert 1 == len(in_plug_a.connections) # Connect the in to the out in_plug_b >> out_plug_a assert 2 == len(out_plug_a.connections) assert 1 == len(in_plug_b.connections) # Connect the in to the multiple times in_plug_b >> out_plug_a assert 2 == len(out_plug_a.connections) assert 1 == len(in_plug_b.connections) # Connecting a different input disconnects the existing one assert out_plug_a == in_plug_a.connections[0] out_plug_b >> in_plug_a print(in_plug_a.connections) assert out_plug_b == in_plug_a.connections[0]
def __init__(self, name=None): """Init the node.""" super(SquareNode, self).__init__(name) InputPlug('in1', self) InputPlug('compound_in', self) OutputPlug('out', self) OutputPlug('compound_out', self)
def __init__(self, name=None, in1=None, *args, **kwargs): """Init the node.""" super(SquareNode, self).__init__(name, *args, **kwargs) InputPlug('in1', self, value=in1) InputPlug('compound_in', self) OutputPlug('out', self) OutputPlug('compound_out', self)
def __init__(self, name=None): """Initialize WriteTextToFileNode.""" super(WriteTextToFileNode, self).__init__(name) InputPlug('text', self) InputPlug('file_path', self) OutputPlug('text', self) OutputPlug('file_path', self)
def test_connect_and_dicsonnect_nodes(): """Connect and disconnect nodes.""" n1 = NodeForTesting() n2 = NodeForTesting() out_plug_a = OutputPlug('out', n1) in_plug_a = InputPlug('in_a', n2) in_plug_b = InputPlug('in_b', n2) in_plug_c = InputPlug('in_c', n2) in_plug_d = InputPlug('in_d', n2) in_plug_compound = InputPlug('in_compound', n2) out_plug_compound = OutputPlug('out_compound', n1) # Connect the out to the in out_plug_a >> in_plug_a assert 1 == len(out_plug_a.connections) assert 1 == len(in_plug_a.connections) out_plug_compound['0'] >> in_plug_c assert 1 == len(out_plug_compound['0'].connections) assert 1 == len(in_plug_c.connections) out_plug_compound['1'] >> in_plug_compound['1'] assert 1 == len(out_plug_compound['1'].connections) assert 1 == len(in_plug_compound['1'].connections) # Connect the same nodes multiple times out_plug_a >> in_plug_a assert 1 == len(out_plug_a.connections) assert 1 == len(in_plug_a.connections) out_plug_compound['0'] >> in_plug_c assert 1 == len(out_plug_compound['0'].connections) assert 1 == len(in_plug_c.connections) out_plug_compound['1'] >> in_plug_compound['1'] assert 1 == len(out_plug_compound['1'].connections) assert 1 == len(in_plug_compound['1'].connections) # Connect the in to the out in_plug_b >> out_plug_a assert 2 == len(out_plug_a.connections) assert 1 == len(in_plug_b.connections) in_plug_d >> out_plug_compound['0'] assert 2 == len(out_plug_compound['0'].connections) assert 1 == len(in_plug_d.connections) in_plug_compound['2'] >> out_plug_compound['1'] assert 2 == len(out_plug_compound['1'].connections) assert 1 == len(in_plug_compound['1'].connections) # Connect the in to the multiple times in_plug_b >> out_plug_a assert 2 == len(out_plug_a.connections) assert 1 == len(in_plug_b.connections) in_plug_d >> out_plug_compound['0'] assert 2 == len(out_plug_compound['0'].connections) assert 1 == len(in_plug_d.connections) in_plug_compound['2'] >> out_plug_compound['1'] assert 2 == len(out_plug_compound['1'].connections) assert 1 == len(in_plug_compound['1'].connections)
def test_connect_and_disconnect_nodes(clear_default_graph): """Connect and disconnect nodes.""" n1 = NodeForTesting(name="n1") n2 = NodeForTesting(name="n2") out_plug_a = OutputPlug("out", n1) in_plug_a = InputPlug("in_a", n2) in_plug_b = InputPlug("in_b", n2) in_plug_c = InputPlug("in_c", n2) in_plug_d = InputPlug("in_d", n2) in_plug_compound = InputPlug("in_compound", n2) out_plug_compound = OutputPlug("out_compound", n1) # Connect the out to the in out_plug_a >> in_plug_a assert 1 == len(out_plug_a.connections) assert 1 == len(in_plug_a.connections) out_plug_compound["0"] >> in_plug_c assert 1 == len(out_plug_compound["0"].connections) assert 1 == len(in_plug_c.connections) out_plug_compound["1"] >> in_plug_compound["1"] assert 1 == len(out_plug_compound["1"].connections) assert 1 == len(in_plug_compound["1"].connections) # Connect the same nodes multiple times out_plug_a >> in_plug_a assert 1 == len(out_plug_a.connections) assert 1 == len(in_plug_a.connections) out_plug_compound["0"] >> in_plug_c assert 1 == len(out_plug_compound["0"].connections) assert 1 == len(in_plug_c.connections) out_plug_compound["1"] >> in_plug_compound["1"] assert 1 == len(out_plug_compound["1"].connections) assert 1 == len(in_plug_compound["1"].connections) # Connect the in to the out in_plug_b >> out_plug_a assert 2 == len(out_plug_a.connections) assert 1 == len(in_plug_b.connections) in_plug_d >> out_plug_compound["0"] assert 2 == len(out_plug_compound["0"].connections) assert 1 == len(in_plug_d.connections) in_plug_compound["2"] >> out_plug_compound["1"] assert 2 == len(out_plug_compound["1"].connections) assert 1 == len(in_plug_compound["1"].connections) # Connect the in to the multiple times in_plug_b >> out_plug_a assert 2 == len(out_plug_a.connections) assert 1 == len(in_plug_b.connections) in_plug_d >> out_plug_compound["0"] assert 2 == len(out_plug_compound["0"].connections) assert 1 == len(in_plug_d.connections) in_plug_compound["2"] >> out_plug_compound["1"] assert 2 == len(out_plug_compound["1"].connections) assert 1 == len(in_plug_compound["1"].connections)
def __init__(self, name=None): """Init the node.""" super(AssetNode, self).__init__(name) InputPlug('project', self) InputPlug('name', self) InputPlug('kind', self) OutputPlug('asset', self) OutputPlug('next_version', self) OutputPlug('next_version_notes', self) OutputPlug('latest_version', self) OutputPlug('latest_version_notes', self)
def test_set_output_pushes_value_to_connected_input(clear_default_graph): """OutPlugs push their values to their connected input plugs.""" n1 = NodeForTesting(name="n1") n2 = NodeForTesting(name="n2") out_plug = OutputPlug("out", n1) in_plug = InputPlug("in", n2) out_compound_plug = OutputPlug("out_compound", n1) in_compound_plug = InputPlug("in_compound", n2) out_plug.value = "OldValue" assert in_plug.value != out_plug.value out_plug >> in_plug in_plug.is_dirty = False assert in_plug.value == out_plug.value assert not in_plug.is_dirty out_plug.value = "NewValue" assert in_plug.is_dirty assert in_plug.value == out_plug.value out_compound_plug.value = "OldValue" assert in_compound_plug.value != out_compound_plug.value out_compound_plug >> in_compound_plug in_compound_plug.is_dirty = False assert in_compound_plug.value == out_compound_plug.value assert not in_compound_plug.is_dirty out_compound_plug.value = "NewValue" assert in_compound_plug.is_dirty assert in_compound_plug.value == out_compound_plug.value
def test_set_output_pushes_value_to_connected_input(): """OutPlugs push their values to their connected input plugs.""" n1 = NodeForTesting() n2 = NodeForTesting() out_plug = OutputPlug('out', n1) in_plug = InputPlug('in', n2) out_compound_plug = OutputPlug('out_compound', n1) in_compound_plug = InputPlug('in_compound', n2) out_plug.value = 'OldValue' assert in_plug.value != out_plug.value out_plug >> in_plug in_plug.is_dirty = False assert in_plug.value == out_plug.value assert not in_plug.is_dirty out_plug.value = 'NewValue' assert in_plug.is_dirty assert in_plug.value == out_plug.value out_compound_plug.value = 'OldValue' assert in_compound_plug.value != out_compound_plug.value out_compound_plug >> in_compound_plug in_compound_plug.is_dirty = False assert in_compound_plug.value == out_compound_plug.value assert not in_compound_plug.is_dirty out_compound_plug.value = 'NewValue' assert in_compound_plug.is_dirty assert in_compound_plug.value == out_compound_plug.value
def test_serialize(): """Serialize the Plug to json.""" n1 = NodeForTesting() n2 = NodeForTesting() out_plug = OutputPlug('out', n1) in_plug = InputPlug('in', n2) out_plug >> in_plug in_serialized = in_plug.serialize() out_serialized = out_plug.serialize() assert in_plug.name == in_serialized['name'] assert in_plug.value == in_serialized['value'] assert 'out' == in_serialized['connections'][out_plug.node.identifier] assert out_plug.name == out_serialized['name'] assert out_plug.value == out_serialized['value'] assert 'in' == out_serialized['connections'][in_plug.node.identifier]
def test_rshift_into_node(clear_default_graph): """Test the syntactic sugar for rshift operator between plug and node.""" n1 = NodeForTesting(name="n1") n2 = NodeForTesting(name="n2") out_plug = OutputPlug("foo", n1) in_plug = InputPlug("foo", n2) out_plug >> n2 assert in_plug in out_plug.connections
def test_plugs_can_not_contain_dots(clear_default_graph): @Node() def A(): pass with pytest.raises(ValueError): OutputPlug(name="name.with.dots", node=A(graph=None)) with pytest.raises(ValueError): InputPlug(name="name.with.dots", node=A(graph=None))
def test_plugs_can_not_contain_dots(): @Node() def A(): pass with pytest.raises(ValueError): OutputPlug(name='name.with.dots', node=A()) with pytest.raises(ValueError): InputPlug(name='name.with.dots', node=A())
def test_plug_gets_dirty_only_on_change(clear_default_graph): """Test that plugs only change dirtyness if a real change happens.""" in_test, out_test = "foo", "bar" n1 = NodeForTesting(name="n1") n2 = NodeForTesting(name="n2") out_plug = OutputPlug("out", n1) in_plug = InputPlug("in", n2) out_plug >> in_plug in_plug.value = in_test out_plug.value = out_test assert in_plug.is_dirty assert out_plug.is_dirty in_plug.is_dirty = False out_plug.is_dirty = False assert not in_plug.is_dirty assert not out_plug.is_dirty same_val = in_plug.value in_plug.value = same_val assert not in_plug.is_dirty assert not out_plug.is_dirty out_plug.value = out_test assert not in_plug.is_dirty assert not out_plug.is_dirty out_plug.value = "baz" assert in_plug.is_dirty assert out_plug.is_dirty
def post_deserialize(self, data): """Perform more data operations after initial serialization.""" self.name = data['name'] self.identifier = data['identifier'] self.metadata = data['metadata'] self.file_location = data['file_location'] for name, input_ in data['inputs'].items(): if name not in self.inputs: InputPlug(name, self) self.inputs[name].value = input_['value'] for name, output in data['outputs'].items(): if name not in self.outputs: OutputPlug(name, self) self.outputs[name].value = output['value']
def test_change_connections_sets_plug_dirty(): """Connecting and disconnecting sets the plug dirty.""" n1 = NodeForTesting() n2 = NodeForTesting() out_plug = OutputPlug('in', n1) in_plug = InputPlug('in', n2) in_plug.is_dirty = False out_plug >> in_plug assert in_plug.is_dirty in_plug.is_dirty = False out_plug << in_plug assert in_plug.is_dirty
def test_change_connections_sets_plug_dirty(clear_default_graph): """Connecting and disconnecting sets the plug dirty.""" n1 = NodeForTesting(name='n1') n2 = NodeForTesting(name='n2') out_plug = OutputPlug('out', n1) in_plug = InputPlug('in', n2) out_compound_plug = OutputPlug('out_compound', n1) in_compound_plug = InputPlug('in_compound', n2) in_plug.is_dirty = False out_plug >> in_plug assert in_plug.is_dirty in_plug.is_dirty = False out_plug << in_plug assert in_plug.is_dirty in_compound_plug['0'].is_dirty = False out_compound_plug['0'] >> in_compound_plug['0'] assert in_compound_plug['0'].is_dirty in_compound_plug['0'].is_dirty = False out_compound_plug['0'] << in_compound_plug['0'] assert in_compound_plug['0'].is_dirty
def test_change_connections_sets_plug_dirty(clear_default_graph): """Connecting and disconnecting sets the plug dirty.""" n1 = NodeForTesting(name="n1") n2 = NodeForTesting(name="n2") out_plug = OutputPlug("out", n1) in_plug = InputPlug("in", n2) out_compound_plug = OutputPlug("out_compound", n1) in_compound_plug = InputPlug("in_compound", n2) in_plug.is_dirty = False out_plug >> in_plug assert in_plug.is_dirty in_plug.is_dirty = False out_plug << in_plug assert in_plug.is_dirty in_compound_plug["0"].is_dirty = False out_compound_plug["0"] >> in_compound_plug["0"] assert in_compound_plug["0"].is_dirty in_compound_plug["0"].is_dirty = False out_compound_plug["0"] << in_compound_plug["0"] assert in_compound_plug["0"].is_dirty
def test_nested_graphs_expand_sub_graphs(): """Nested Graphs expand all nodes of their sub graphs on evaluation.""" @function_to_node(outputs=["out_put"]) def N(in_put_1, in_put_2): return {"out_put": "G1_Node1"} # G 3 ############################# # G3 = Graph(name="G3") N5 = N(name="N5") N4 = N(name="N4") G3.add_node(N5) G3.add_node(N4) N4.outputs["out_put"] >> N5.inputs["in_put_1"] # G 2 ############################# # G2 = Graph(name="G2") N3 = N(name="N3") N2 = N(name="N2") N6 = N(name="N6") G2.add_node(N3) G2.add_node(N2) G2.add_node(N6) G2.add_node(G3) N3.outputs["out_put"] >> N4.inputs["in_put_1"] N2.outputs["out_put"] >> N4.inputs["in_put_2"] N5.outputs["out_put"] >> N6.inputs["in_put_1"] # G 1 ############################# # G1 = Graph(name="G1") OutputPlug('out_put', G1) N1 = N(name="N1") N7 = N(name="N7") G1.add_node(N1) G1.add_node(N7) G1.add_node(G2) N1.outputs["out_put"] >> N3.inputs["in_put_1"] N6.outputs["out_put"] >> N7.inputs["in_put_1"] N1.outputs["out_put"] >> N7.inputs["in_put_2"] order = [['N1', 'N2'], ['N3'], ['N4'], ['N5'], ['N6'], ['N7']] for i, nodes in enumerate(G1.evaluation_matrix): assert sorted([n.name for n in nodes]) == sorted(order[i])
def test_forbidden_connect(clear_default_graph): """Test connections between plugs that are forbidden.""" n1 = NodeForTesting(name="n1") in_plug1 = InputPlug("in", n1) out_plug1 = OutputPlug("out", n1) n2 = NodeForTesting(name="n2") InputPlug("in", n2) out_plug2 = OutputPlug("out", n2) with pytest.raises(TypeError): out_plug1.connect(out_plug2) with pytest.raises(TypeError): in_plug1.connect(in_plug1) with pytest.raises(TypeError): out_plug1.connect("a string")
def __init__(self, *args, **kwargs): super(DivideNode, self).__init__(*args, **kwargs) InputPlug('number_1', self, 0) InputPlug('number_2', self, 0) OutputPlug('result', self) self.metadata = { "datatypes": { "inputs": { "number_1": { "type": "float", }, "number_2": { "type": "float" } }, "outputs": { "result": { "type": "float" } } } }
def __init__(self, time=None, timezone=0, **kwargs): super(ConvertTime, self).__init__(**kwargs) InputPlug('time', self) InputPlug('timezone', self, timezone) OutputPlug('converted_time', self)
def __init__(self, name=None, in1=None, in2=None, **kwargs): super(NodeForTesting, self).__init__(name, **kwargs) OutputPlug('out', self) OutputPlug('out2', self) InputPlug('in1', self, in1) InputPlug('in2', self, in2)
def __init__(self, name=None, in1=None, in2=None, **kwargs): super(NodeForTesting, self).__init__(name, **kwargs) OutputPlug("out", self) OutputPlug("out2", self) InputPlug("in1", self, in1) InputPlug("in2", self, in2)
def __init__(self, amount=None, **kwargs): super(HireWorkers, self).__init__(**kwargs) InputPlug('amount', self, amount) OutputPlug('workers', self)
def test_pretty_printing(): node = NodeForTesting() in_plug = InputPlug('in', node) out_plug = OutputPlug('out', node) print(in_plug) print(out_plug)
def test_serialize(): """Serialize the Plug to json.""" n1 = NodeForTesting() n2 = NodeForTesting() out_plug = OutputPlug('out', n1) out_plug.value = 'out_value' in1_plug = InputPlug('in1', n2) in2_plug = InputPlug('in2', n2) in_plug_with_value = InputPlug('in_value', n2, 'value') compound_out_plug = OutputPlug('compound_out', n1) compound_in_plug = InputPlug('compound_in', n2) out_plug >> in1_plug out_plug >> compound_in_plug['incoming'] compound_out_plug['0'] >> in2_plug compound_in_plug['0'].value = 0 compound_in_plug['key'].value = 'value' in_serialized = in1_plug.serialize() assert in_serialized == { 'name': 'in1', 'value': 'out_value', 'connections': { out_plug.node.identifier: 'out' }, 'sub_plugs': {} } in_plug_with_value_serialized = in_plug_with_value.serialize() assert in_plug_with_value_serialized == { 'name': 'in_value', 'value': 'value', 'connections': {}, 'sub_plugs': {} } compound_in_serialized = compound_in_plug.serialize() assert compound_in_serialized == { 'name': 'compound_in', 'value': None, 'connections': {}, 'sub_plugs': { '0': { 'connections': {}, 'name': 'compound_in.0', 'value': 0 }, 'incoming': { 'connections': { out_plug.node.identifier: 'out' }, 'name': 'compound_in.incoming', 'value': 'out_value' }, 'key': { 'connections': {}, 'name': 'compound_in.key', 'value': 'value' } } } out_serialized = out_plug.serialize() assert out_serialized == { 'name': 'out', 'value': 'out_value', 'connections': { in1_plug.node.identifier: [ 'in1', 'compound_in.incoming' ] }, 'sub_plugs': {} } compound_out_serialized = compound_out_plug.serialize() assert compound_out_serialized == { 'connections': {}, 'name': 'compound_out', 'value': None, 'sub_plugs': { '0': { 'connections': { in2_plug.node.identifier: [ 'in2' ] }, 'name': 'compound_out.0', 'value': None } } } in2_plug_serialized = in2_plug.serialize() assert in2_plug_serialized == { 'connections': { compound_out_plug.node.identifier: 'compound_out.0' }, 'name': 'in2', 'value': None, 'sub_plugs': {} }
def test_serialize(clear_default_graph): """Serialize the Plug to json.""" n1 = NodeForTesting(name="n1") n2 = NodeForTesting(name="n2") out_plug = OutputPlug("out", n1) out_plug.value = "out_value" in1_plug = InputPlug("in1", n2) in2_plug = InputPlug("in2", n2) in_plug_with_value = InputPlug("in_value", n2, "value") compound_out_plug = OutputPlug("compound_out", n1) compound_in_plug = InputPlug("compound_in", n2) out_plug >> in1_plug out_plug >> compound_in_plug["incoming"] compound_out_plug["0"] >> in2_plug compound_in_plug["0"].value = 0 compound_in_plug["key"].value = "value" in_serialized = in1_plug.serialize() assert in_serialized == { "name": "in1", "value": "out_value", "connections": {out_plug.node.identifier: "out"}, "sub_plugs": {}, } in_plug_with_value_serialized = in_plug_with_value.serialize() assert in_plug_with_value_serialized == { "name": "in_value", "value": "value", "connections": {}, "sub_plugs": {}, } compound_in_serialized = compound_in_plug.serialize() assert compound_in_serialized == { "name": "compound_in", "value": None, "connections": {}, "sub_plugs": { "0": {"connections": {}, "name": "compound_in.0", "value": 0}, "incoming": { "connections": {out_plug.node.identifier: "out"}, "name": "compound_in.incoming", "value": "out_value", }, "key": { "connections": {}, "name": "compound_in.key", "value": "value", }, }, } out_serialized = out_plug.serialize() assert out_serialized == { "name": "out", "value": "out_value", "connections": { in1_plug.node.identifier: ["in1", "compound_in.incoming"] }, "sub_plugs": {}, } compound_out_serialized = compound_out_plug.serialize() assert compound_out_serialized == { "connections": {}, "name": "compound_out", "value": None, "sub_plugs": { "0": { "connections": {in2_plug.node.identifier: ["in2"]}, "name": "compound_out.0", "value": None, } }, } in2_plug_serialized = in2_plug.serialize() assert in2_plug_serialized == { "connections": {compound_out_plug.node.identifier: "compound_out.0"}, "name": "in2", "value": None, "sub_plugs": {}, }
def __init__(self, name=None): """Init the node.""" super(EnvNode, self).__init__(name) for env_var in config.env_vars: OutputPlug(env_var, self)
def __init__(self, name=None): super(NodeForTesting, self).__init__(name) OutputPlug('out', self) InputPlug('in1', self, 0) InputPlug('in2', self, 0)