Chapter 27

Lazy Rivers

image

27.1 Constraints

  • Data is available in streams, rather than as a complete whole.
  • Functions are filters/transformers from one kind of data stream to another.
  • Data is processed from upstream on a need basis from downstream.

27.2 A Program in this Style

  1 #!/usr/bin/env python
  2 import sys, operator, string
  3
  4 def characters(filename):
  5 for line in open(filename):
  6  for c in line:
  7    yield c
  8
  9 def all_words(filename):
 10 start_char = True
 11 for c in characters(filename):
 12  if start_char == True:
 13    word = ""
 14    if c.isalnum():
 15    # We found the start of a word
 16    word = c.lower()
 17    start_char = False
 18    else: pass ...

Get Exercises in Programming Style now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.