4

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 surrounding cities).

2

3 Answers 3

4

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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()))))

2 Comments

Why not all_items(get_creators(get_location(get_surrounding_cities(printer())))) ? Is there a reason for all_items and get_creators to behave differently than get_location and get_surrounding_cities?
Actually you're right, no need to behave differently, I edited my answer
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.