示例#1
0
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
示例#2
0
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)
示例#3
0
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
示例#4
0
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
示例#5
0
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
示例#6
0
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
示例#7
0
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
示例#8
0
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 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
示例#10
0
def test_input():
    expected = '1234'
    instance = value.Value()
    instance.input(dict(value=expected))
    instance.set_output_label('any')
    assert instance.output() == expected
示例#11
0
def test_requirements():
    expected = ['value']
    instance = value.Value()
    assert instance.requirements == expected
示例#12
0
def test_output():
    expected = dict(expected='1234')
    instance = value.Value(value=expected)
    instance.set_output_label('any')
    assert instance.output() == expected