在Bonobo-etl的节点中保持本地状态的最佳方法是什么?

时间:2018-11-08 17:53:03

标签: python-3.x bonobo-etl

如果我有一个包含20个数字的输入队列,如何获取例如所有数字的总和?到目前为止,这是我想出的:

import bonobo as bb
from bonobo.config import Configurable, ContextProcessor
from bonobo.util import ValueHolder

def extract_nums():
    yield 1
    yield 2
    yield 3

class TransformNumber(Configurable):
    @ContextProcessor
    def total(self, context):
        yield ValueHolder({'extract':0,'transform':0})

    def __call__(self, total, num, **kwargs):
        total['extract']+=num
        transform_num = num * 10
        total['transform']+=transform_num
        if num==3: # Final number
            print("TOTALS:",total.get())
        yield transform_num

graph = bb.Graph()
graph.add_chain(
    extract_nums,
    TransformNumber(),
    bb.PrettyPrinter()
)

可以这样做,还是有更好的方法?

1 个答案:

答案 0 :(得分:2)

有多种可用选项可以将本地状态保留在Bonobo ETL节点中。

可以像您一样做(虽然我认为很难阅读),但我倾向于使用我认为更易读的闭包(但我同意,这值得商)):

import bonobo


def CumSum():
    total = 0

    def cum_sum(x):
        nonlocal total
        total += x
        yield x, total

    return cum_sum


def get_graph(**options):
    graph = bonobo.Graph()
    graph.get_cursor() >> range(100) >> CumSum() >> print

    return graph


# The __main__ block actually execute the graph.
if __name__ == "__main__":
    parser = bonobo.get_argument_parser()
    with bonobo.parse_args(parser) as options:
        bonobo.run(get_graph(**options))

the黑猩猩源代码中提供了一些示例,请查看https://github.com/python-bonobo/bonobo/blob/develop/bonobo/nodes/basics.py(并且有使用不同样式编写的示例)。

请注意,我在这里使用Bonobo 0.7(传入)语法来构建图形,但是对于当前的稳定版本(0.6),也可以通过用add_chain调用替换“ >>”运算符来使用相同的功能。