O'Reilly logo

Exercises in Programming Style by Cristina Videira Lopes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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(): ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required