From 3478b62a6dcb2019acc2368a48fd44880c05449c Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 9 Jul 2009 01:21:14 -0400 Subject: [PATCH] use with instead of try..finally, add forward references to still-unwritten Files chapter --- examples/plural4.py | 5 +---- examples/plural5.py | 7 ++++--- generators.html | 30 +++++++++++++----------------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/examples/plural4.py b/examples/plural4.py index 560a5dd..dc7e2b8 100644 --- a/examples/plural4.py +++ b/examples/plural4.py @@ -15,14 +15,11 @@ def build_match_and_apply_functions(pattern, search, replace): return [matches_rule, apply_rule] rules = [] -pattern_file = open('plural4-rules.txt') -try: +with open('plural4-rules.txt') as pattern_file: for line in pattern_file: pattern, search, replace = line.split(None, 3) rules.append(build_match_and_apply_functions( pattern, search, replace)) -finally: - pattern_file.close() def plural(noun): for matches_rule, apply_rule in rules: diff --git a/examples/plural5.py b/examples/plural5.py index 7d28b2f..14845c7 100644 --- a/examples/plural5.py +++ b/examples/plural5.py @@ -15,9 +15,10 @@ def build_match_and_apply_functions(pattern, search, replace): return [matches_rule, apply_rule] def rules(): - for line in open('plural5-rules.txt'): - pattern, search, replace = line.split(None, 3) - yield build_match_and_apply_functions(pattern, search, replace) + with open('plural5-rules.txt') as pattern_file: + for line in pattern_file: + pattern, search, replace = line.split(None, 3) + yield build_match_and_apply_functions(pattern, search, replace) def plural(noun): for matches_rule, apply_rule in rules(): diff --git a/generators.html b/generators.html index 207a901..245163b 100644 --- a/generators.html +++ b/generators.html @@ -263,8 +263,6 @@ $ $ s

Now let’s see how you can use this rules file. -

[FIXME: now that this chapter comes before the I/O chapter, need to at least mention what open() does] -

[FIXME: try/finally -> with]

[download plural4.py]

import re
 
@@ -276,20 +274,16 @@ $                    $    s
return [matches_rule, apply_rule] rules = [] -pattern_file = open('plural4-rules.txt') -try: +with open('plural4-rules.txt') as pattern_file: for line in pattern_file: pattern, search, replace = line.split(None, 3) rules.append(build_match_and_apply_functions( - pattern, search, replace)) -finally: - pattern_file.close() + pattern, search, replace))
  1. The build_match_and_apply_functions() function has not changed. You’re still using closures to build two functions dynamically that use variables defined in the outer function. -
  2. Open the file that contains the pattern strings. -
  3. Read through the file one line at a time, using the for line in <fileobject> idiom. +
  4. The global open() function opens a file and returns a file object. In this case, the file we’re opening contains the pattern strings for pluralizing nouns. The with statement creates what’s called a context: when the with block ends, Python will automatically close the file, even if an exception is raised inside the with block. You’ll learn more about with blocks in the Files chapter. +
  5. The for line in <fileobject> idiom reads data from the open file, one line at a time, and assigns the text to the line variable. A “line” of a text file is just what you think it is — a sequence of characters delimited by a carriage return. Of course, it can’t really be that simple, can it? Text files can use several different characters to mark the end of a line. Some use a carriage return character, others use a line feed character, and some use both characters at the end of every line. Python handles all of these cases automatically, so you can say, “Hey, I want to read this text file one line at a time” and it will Just Work. You’ll learn more about reading from and writing to in the Files chapter.
  6. Each line in the file really has three values, but they’re separated by whitespace (tabs or spaces, it makes no difference). To split it out, use the split() string method. The first argument to the split() method is None, which means “split on any whitespace (tabs or spaces, it makes no difference).” The second argument is 3, which means “split on whitespace 3 times, then discard the rest of the line.” A line like [sxz]$ $ es will be broken up into the list ['[sxz]$', '$', 'es'], which means that pattern will get '[sxz]$', search will get '$', and replace will get 'es'. That’s a lot of power in one little line of code. -
  7. Use a try..finally block to ensure the file object is closed.

The improvement here is that you’ve completely separated the pluralization rules into an external file, so it can be maintained separately from the code that uses it. Code is code, data is data, and life is good. @@ -302,9 +296,10 @@ finally:

[download plural5.py]

def rules():
-    for line in open('plural5-rules.txt'):
-        pattern, search, replace = line.split(None, 3)
-        yield build_match_and_apply_functions(pattern, search, replace)
+    with open('plural5-rules.txt') as pattern_file:
+        for line in pattern_file:
+            pattern, search, replace = line.split(None, 3)
+            yield build_match_and_apply_functions(pattern, search, replace)
 
 def plural(noun):
     for matches_rule, apply_rule in rules():
@@ -377,12 +372,13 @@ def plural(noun):
 

Let’s go back to plural5.py and see how this version of the plural() function works.

def rules():
-    for line in open('plural5-rules.txt'):
-        pattern, search, replace = line.split(None, 3)                   
-        yield build_match_and_apply_functions(pattern, search, replace)  
+    with open('plural5-rules.txt') as pattern_file:
+        for line in pattern_file:
+            pattern, search, replace = line.split(None, 3)                   
+            yield build_match_and_apply_functions(pattern, search, replace)  
 
 def plural(noun):
-    for matches_rule, apply_rule in rules():                             
+    for matches_rule, apply_rule in rules():                                 
         if matches_rule(noun):
             return apply_rule(noun)