用Python的发电机更换C ++ STL输出迭代器 [英] Replacing C++ STL output iterator with a Python generator

查看:96
本文介绍了用Python的发电机更换C ++ STL输出迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python没有输出迭代器的内置等价物;特别是内置或标准库容器不支持任何通用的接口,允许客户端code至不知道具体的集装箱式数据发送给他们。

Python doesn't have a builtin equivalent of OutputIterator; in particular, builtin or standard library containers do not support any generic interface that allows client code to send data to them without knowing the specific container type.

据<一个href=\"http://stackoverflow.com/questions/3707418/why-are-collections-not-handled-uniformly-in-python#comment3915694_3707535\">@Steven Rumbalski的评论和 @Glenn梅纳德的答案,这通常不是一个问题,因为一个函数,在C ++将有采取了输出迭代的说法,蟒蛇将被简单地写成一台发电机。

According to @Steven Rumbalski's comment and @Glenn Maynard's answer, this is not usually a problem because a function that in C++ would have taken an OutputIterator argument, in python would be simply written as a generator.

通常情况下,我使用的发电机没有问题的,从来不觉得我需要在Python的输出迭代。然而,在这种一种情况下,我坚持。

Normally, I have no problem using generators, and never felt I needed an OutputIterator in Python. However, in this one case, I'm stuck.

我在Python重新实现从一些Boost Graph库的算法。一个典型的图形遍历算法,比如 depth_first_search ,作为一个参数一个访客的对象。参观者基本上是一堆的遍历算法要求,因为它遇到在其执行不同的事件回调函数(例如,发现了一个新的顶点,检查边等)。在C ++中,我可以有一个或几个这些回调函数将数据发送到输出迭代对象,在从客户端code初始化中获得的访问者对象。 (举例来说,这是precisely如何 topological_sort 实现:它需要一个输出迭代器,并将其传递给 dfs_visitor 对象,访问者对象则监视器事件 finished_vertex 并发送它接收到指定的输出迭代的顶点。当然,更复杂的情况下,需要多个输出迭代对象和多个回调函数。)

I am re-implementing in Python some of the algorithms from the Boost Graph Library. A typical graph traversal algorithm, say depth_first_search, takes as a parameter a "visitor" object. The visitor is essentially a bunch of callback functions that the traversal algorithm calls as it encounters different events in its execution (e.g., discovering a new vertex, examining an edge, etc.). In C++, I can have one or several of these callback functions send data to the OutputIterator objects that the visitor object obtained at its initialization from the client code. (For instance, that's precisely how topological_sort is implemented: it takes an OutputIterator, passes it to a dfs_visitor object, the visitor object then "monitors" the event finished_vertex and sends the vertexes it receives to the specified OutputIterator. Of course, more complex cases require multiple OutputIterator objects and multiple callback functions.)

我如何达到同样的用Python生成?

How do I achieve the same with Python generators?

我需要以某种方式发送数据,在发电机风格,从 depth_first_search 来多个指定数据的消费者。我只是无法弄清楚如何做到这一点。 (我使用Python 3.3。)

I need to somehow send data, in the generator "style", from depth_first_search to multiple designated data consumers. I just can't figure out how to do it. (I'm using Python 3.3.)

推荐答案

generator.send 方法做了你想要的这里,我想:

The generator.send method does what you want here, I think:

def depth_first_search(some_args, edge_consumer, vertex_consumer):
    # start the generators
    next(edge_consumer)
    next(vertex_consumer)

    ...
    edge_consumer.send(some_edge)
    vertex_consumer.send(some_vertex)

    ...
    edge_consumer.close()
    vertex_consumer.close()

def edge_handler():
    try:
        while True:
            e = yield
            print "E", e
    except GeneratorExit:
        return

def vertex_handler():
    try:
        while True:
            v = yield
            print "V", v
    except GeneratorExit:
        return


depth_first_search(..., edge_handler(), vertex_handler())

这篇关于用Python的发电机更换C ++ STL输出迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆