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]
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))
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.
-for line in <fileobject> idiom.
+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.
+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.
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.
-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:
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)