Chapter 3

Monolithic

image

3.1 Constraints

  • No named abstractions.
  • No, or little, use of libraries.

3.2 A Program in this Style

  1 #!/usr/bin/env python
  2
  3 import sys, string
  4 # the global list of [word, frequency] pairs
  5 word_freqs = []
  6 # the list of stop words
  7 with open('../stop_words.txt') as f:
  8 stop_words = f.read().split(',')
  9 stop_words.extend(list(string.ascii_lowercase))
 10
 11 # iterate through the file one line at a time
 12 for line in open(sys.argv[1]):
 13 start_char = None
 14 i = 0
 15 for c in line:
 16  if start_char == None:
 17    if c.isalnum():
 18    # We found the start of a word
 19    start_char = i
 20  else:
 21    if not c.isalnum(): ...

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.