typestar

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

  1. Each yield hands one value downstream.
  2. only_odd and scaled wrap the previous generator.
  3. Nothing computes until list pulls the pipeline.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 5 in Generators & itertools, step 10 of 53 in Pythonic Python.

← Previous Next →