Generator pipelines in Python
Chaining generators so data flows through stages lazily.
def numbers(limit):
for n in range(limit):
yield n
def only_odd(source):
for n in source:
if n % 2:
yield n
def scaled(source, factor):
for n in source:
yield n * factor
pipeline = scaled(only_odd(numbers(10)), 100)
result = list(pipeline)
How it works
- Each
yieldhands one value downstream. only_oddandscaledwrap the previous generator.- Nothing computes until
listpulls the pipeline.
Keywords and builtins used here
defforiflistnumbersonly_oddrangescaledyield
The run, in numbers
- Lines
- 18
- Characters to type
- 245
- Tokens
- 71
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 43 seconds.
Step 1 of 5 in Generators & itertools, step 10 of 53 in Pythonic Python.