def sort_and_unique(input_file_path, output_file_path): file_reader = files.TextFileReader(filepath=input_file_path, encoding='UTF-8', name="file_reader") to_string_list = apply.Apply(function=lambda c: c.split('\n'), name="to_string_list") def remove_duplicates_from(collection): coll_type = type(collection) items = set(collection) return coll_type(items) uniquer = apply.Apply(function=remove_duplicates_from, name='uniquer') sorter = apply.Apply(function=lambda unsorted: sorted(unsorted), name="sorter") to_string = apply.Apply(function=lambda token_list: '\n'.join(token_list), name='to_string') file_writer = files.TextFileWriter(filepath=output_file_path, encoding='UTF-8', name='file_writer') g = graph.Graph('sort_and_unique', [file_reader, to_string_list, sorter, uniquer, to_string, file_writer]) g.connect(file_writer, to_string, 'data') g.connect(to_string, sorter, 'argument') g.connect(sorter, uniquer, 'argument') g.connect(uniquer, to_string_list, 'argument') g.connect(to_string_list, file_reader, 'argument') return g
def test_add_node(): n = Identity(name='testnode') instance = graph.Graph('testgraph') assert len(instance.nodes) == 0 instance.add_node(n) nodes = instance.nodes assert len(nodes) == 1 assert n in nodes
def test_get_nodes(): n1 = Identity(name='testnode') n2 = Identity(name='testnode') instance = graph.Graph('testgraph', [n1, n2]) nodes = instance.nodes assert len(nodes) == 2 assert n1 in nodes assert n2 in nodes
def test_root_node(): root = Identity(name='root') leaf1 = Identity(name='leaf1') leaf2 = Identity(name='leaf2') instance = graph.Graph('testgraph', [leaf1, leaf2, root]) instance.connect(root, leaf1, 'any1') instance.connect(root, leaf2, 'any2') result = instance.root_node assert result == root
def test_graph_serializer(): a = value.Value(value=1, name='val_a') b = value.Value(value=2, name='val_b') s = buffers.Buffer(name='buffer') g = graph.Graph('test', [a, b, s]) g.connect(s, a, 'val_a') g.connect(s, b, 'val_b') serialized = serializer.GraphSerializer.serialize(g) deserialized = serializer.GraphSerializer.deserialize(serialized) assert g.name == deserialized.name assert g.nxgraph == g.nxgraph
def make_a_graph_with_isles(): root = Identity(name='root') med1 = Identity(name='med1') med2 = Identity(name='med2') leaf1 = Identity(name='leaf1') leaf2 = Identity(name='leaf2') g = graph.Graph('testgraph', [leaf1, med1, med2, leaf2, root]) g.connect(root, med1, '1') g.connect(root, med2, '2') g.connect(med1, leaf1, '3') return g
def scraper_image(img_url, target_path): url = value.Value(value=img_url) client = http.Get(mime_type='image/png', ) writer = files.BinaryFileWriter(filepath=target_path) g = graph.Graph('scrape_image', [url, client, writer]) g.connect(writer, client, 'data') g.connect(client, url, 'url') return g
def test_reset(): n1 = WithParameters(a=1, b=2) n2 = WithParameters(c=1, d=2) g = graph.Graph('testgraph', [n1, n2]) for n in g.nodes: for p in n.parameters.values(): assert p is not None g.reset() for n in g.nodes: for p in n.parameters.values(): assert p is None
def test_get_edges(): n1 = WithParameters(a=1, b=2) n2 = WithParameters(c=1, d=2) root = WithParameters(x='x') g = graph.Graph('testgraph', [n1, n2, root]) assert len(g.edges) == 0 g.connect(root, n1, 'child_1') g.connect(root, n2, 'child_2') result = g.edges assert len(result) == 2 assert dict(node_from=root, node_to=n1, output_label='child_1') in result assert dict(node_from=root, node_to=n2, output_label='child_2') in result
def replace_word(text): t = value.Value(value=text, name="text") s = branching.IfThenApply(condition=lambda x: "hello" in x, function_true=lambda x: x.replace("hello", "ciao"), function_false=lambda x: x.replace(" ", "_"), name="if") p = printer.ConsolePrinter() g = graph.Graph('replace_word', [t, s, p]) g.connect(p, s, 'message') g.connect(s, t, 'data') return g
def test_remove_nodes(): root = Identity(name='root') leaf1 = Identity(name='leaf1') leaf2 = Identity(name='leaf2') instance = graph.Graph('testgraph', [leaf1, leaf2, root]) instance.connect(root, leaf1, 'any1') instance.connect(root, leaf2, 'any2') assert len(instance.nodes) == 3 instance.remove_nodes([leaf1, leaf2]) nodes = instance.nodes assert len(nodes) == 1 assert root in nodes assert not leaf1 in nodes assert not leaf2 in nodes
def execution_stop(number): def stop_here(value): if value >= 0: raise exceptions.StopGraphExecutionSignal('arg is positive') raise exceptions.StopGraphExecutionSignal('arg is negative') v = value.Value(value=number) a = apply.Apply(function=stop_here) g = graph.Graph('execution_stop', [a, v]) g.connect(a, v, 'argument') return g
def test_has_isles(): root = Identity(name='root') med1 = Identity(name='med1') med2 = Identity(name='med2') leaf1 = Identity(name='leaf1') leaf2 = Identity(name='leaf2') g = graph.Graph('testgraph', [leaf1, med1, med2, leaf2, root]) g.connect(root, med1, '1') g.connect(root, med2, '2') g.connect(med1, leaf1, '3') g.connect(med2, leaf2, '4') assert not g.has_isles() g.remove_node(med2) assert g.has_isles()
def sum_and_product(list_of_numbers): v = value.Value(value=list_of_numbers) s = apply.Apply(function=sum) m = apply.Apply(function=lambda c: reduce(lambda x, y: x * y, c)) b = buffers.Buffer() p = printer.ConsolePrinter() g = graph.Graph('sum_and_product', [v, s, m, p, b]) g.connect(p, b, 'message') g.connect(b, s, 'sum value') g.connect(b, m, 'product value') g.connect(s, v, 'argument') g.connect(m, v, 'argument') return g
def plot(): v1 = value.Value(value=[9, -3, 8.7], name='v1') v2 = value.Value(value=[86, -0.43], name='v2') s = maths.Sum(name='sum') m = maths.Product(name='product') b = buffers.Buffer(name='buffer') p = printer.ConsolePrinter(name='printer') g = graph.Graph('sum_and_product', [v1, v2, s, m, p, b]) g.connect(p, b, 'message') g.connect(b, s, 'sum of v1') g.connect(b, m, 'product of v2') g.connect(s, v1, 'argument') g.connect(m, v2, 'argument') plotter.show_plot(g)
def email_geolocated_ip(recipients_list, smtp_server_params, ip_addr): subject = value.Value(value='Test mail') smtp_server_params['sender'] = '*****@*****.**' smtp_server_params['mime_type'] = 'text/html' smtp_server_params['recipients_list'] = recipients_list sendmail = email.SmtpEmail(**smtp_server_params) http_params = dict(url='https://api.ip2country.info/ip?'+ip_addr, mime_type='application/json') geolocate = http.Get(**http_params) g = graph.Graph('email_geolocated_ip', [subject, geolocate, sendmail]) g.connect(sendmail, geolocate, 'body') g.connect(sendmail, subject, 'subject') return g
def logged_sum_and_product(list_of_numbers): v = value.Value(value=list_of_numbers) s = apply.Apply(function=sum) m = apply.Apply(function=lambda c: reduce(lambda x, y: x * y, c)) b = buffers.Buffer() logging.basicConfig(level=logging.ERROR) p = printer.LogPrinter(logger=logging.getLogger(__name__), loglevel=logging.ERROR) g = graph.Graph('logged_sum_and_product', [v, s, m, b, p]) g.connect(p, b, 'message') g.connect(b, s, 'sum value') g.connect(b, m, 'product value') g.connect(s, v, 'argument') g.connect(m, v, 'argument') return g
def deserialize(cls, json_string): """ Builds a Graph from a JSON representation :param json_string: JSON str :return: datamodel.base.graph.Graph instance """ try: graph_dict = json.loads(json_string) result = graph.Graph(name=graph_dict['name']) nodes = dict() for item in graph_dict['nodes']: nodes[item['id']] = NodeSerializer.from_dict(item['data']) result.add_nodes(nodes.values()) for e in graph_dict['edges']: result.connect(nodes[e['id_node_from']], nodes[e['id_node_to']], e['output_label']) return result except Exception as e: raise
def test_connect(): n1 = WithParameters(a=1, b=2) n2 = WithParameters(c=1, d=2) g = graph.Graph('testgraph', [n1, n2]) not_included = WithParameters(e=1, f=2) # Errors when trying to link nodes coming from out of the graph with pytest.raises(exceptions.NodeConnectionError): g.connect(n1, not_included, 'blabla') pytest.fail() with pytest.raises(exceptions.NodeConnectionError): g.connect(not_included, n2, 'blabla') pytest.fail() # Now the correct procedure assert n2.output_label is None assert len(g.nxgraph.edges()) == 0 g.connect(n1, n2, 'label') assert n2.output_label == 'label' assert len(g.nxgraph.edges()) == 1
def delayed_sum_and_product(list_of_numbers, delay): val = value.Value(value=list_of_numbers) summer = apply.Apply(function=sum) multiplier = apply.Apply(function=lambda c: reduce(lambda x, y: x * y, c)) delayed_value_buffer = buffers.DelayedBuffer(seconds=delay) printout = printer.ConsolePrinter() g = graph.Graph('sum_and_product', [ val, summer, multiplier, printout, delayed_value_buffer, delayed_value_buffer ]) g.connect(printout, delayed_value_buffer, 'message') g.connect(delayed_value_buffer, summer, 'sum value') g.connect(delayed_value_buffer, multiplier, 'product value') g.connect(summer, val, 'argument') g.connect(multiplier, val, 'argument') return g
def test_len(): root = Identity(name='root') leaf1 = Identity(name='leaf1') leaf2 = Identity(name='leaf2') instance = graph.Graph('testgraph', [leaf1, leaf2, root]) assert len(instance) == 3