September 2008
Intermediate to advanced
280 pages
6h 31m
English
Let's take as our example tf.py, which counts words, lines, and paragraphs in a text file, as with our Perl example above.

Figure 8-5. Perl debugging screen
1 class textfile: 2 ntfiles = 0 # count of number of textfile objects 3 def __init__(self,fname): 4 textfile.ntfiles += 1 5 self.name = fname # name 6 self.fh = open(fname) 7 self.nlines = 0 # number of lines 8 self.nwords = 0 # number of words 9 self.npars = 0 # number of words 10 self.lines = self.fh.readlines() 11 self.wordlineparcount() 12 def wordlineparcount(self): 13 "finds the number of lines and words in the file" 14 self.nlines = len(self.lines) 15 inparagraph = 0 ...
Read now
Unlock full access