I have a library function (which I cannot change) that takes as an input an iterator that provides a specific type of object. I implemented this input as a generator that parses the text and gives parts of it. I would like to be able to take into account other information found during the analysis of the text.
The comments in the following example give an idea of what I would like to do:
the_text = """1:-
3:-
2:+
4:-
6:+
2:-
4:-
5:+
6:-
7:+"""
def extract_ints(text):
for line in text.split("\n"):
fields = line.split(":")
yield fields[0]
def double_ints(num_source):
"""Only wants numbers."""
for num in num_source:
yield 2 * int(num)
def my_consumer_function(text):
for value in double_ints(extract_ints(text)):
print(value)
my_consumer_function(the_text)
How can I start sending this information from my generator to my consumer function along with the output of an unmodifiable library function?
source
share