How to create an iterator pipeline in Python?
Is there a library or recommended way for creating an iterator pipeline in Python?
For example:
>>>all_items().get("created_by").location().surrounding_cities()
I also want to be able to access attributes of the objects in the iterators. In the above example, all_items()
returns an iterator of items, and the items have different creators.
The .get("created_by")
then returns the "created_by" attribute of the items (which are people), and then location()
returns the city of each person, and then pipes it to surrounding_cities()
, which returns all an iterator of the surrounding cities for each location (so the end result is a large list of surrou开发者_JAVA技巧nding cities).
Aren't you just processing an iterator? The natural way to use an iterator in Python is the for loop:
for item in all_items():
item.get("created_by").location().surrounding_cities()
There are other possibilities such as list comprehensions which may make more sense depending upon what you're doing (usually makes more sense if you're trying to generate a list as your output).
I suggest you look up how to implement pipes with coroutines in python, more specifically this pipe example
If you implemented your functions according to the example above, then your code would be something like this (for simplicity, I suppose you'll want to print those cities):
all_items(get_location(get_creators(get_surrounding_cities(printer()))))
in your example you only really have two iterator methods, all_items
and surrounding_cities
so you could do pretty well with just itertools.chain
:
from itertools import chain
cities = chain.from_iterable(
item.get("created_by").location().surrounding_cities() for item in all_items()
)
cities
would then be an iterator listing all the surrounding cities for the items
精彩评论