How To Create An Iterator Pipeline In Python?
Solution 1:
Aren't you just processing an iterator? The natural way to use an iterator in Python is the for loop:
for item inall_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).
Solution 2:
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()))))
Solution 3:
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 inall_items()
)
cities
would then be an iterator listing all the surrounding cities for the items
Post a Comment for "How To Create An Iterator Pipeline In Python?"