Example #1
0
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
Example #2
0
def test_input():
    method = lambda x: x + 1
    data = 7
    instance = apply.Apply()
    instance.input(dict(function=method, argument=data))
    instance.set_output_label('any')
    assert instance.output() == 8
Example #3
0
def test_node_serializer():
    instance = apply.Apply(function=lambda x: x + 2, argument=3, name='test')
    serialized = serializer.NodeSerializer.serialize(instance)
    deserialized = serializer.NodeSerializer.deserialize(serialized)
    assert instance.name == deserialized.name
    assert instance.output_label == deserialized.output_label
    assert instance.output() == deserialized.output()
Example #4
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
Example #5
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
Example #7
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
Example #8
0
def test_requirements():
    expected = ['function', 'argument']
    instance = apply.Apply()
    assert instance.requirements == expected
Example #9
0
def test_output():
    data = [4, 6, 9]
    instance = apply.Apply(function=sum, argument=data)
    instance.set_output_label('any')
    assert instance.output() == 19