mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
use with instead of try..finally, add forward references to still-unwritten Files chapter
This commit is contained in:
+1
-4
@@ -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:
|
||||
|
||||
+4
-3
@@ -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():
|
||||
|
||||
+13
-17
@@ -263,8 +263,6 @@ $ $ s</code></pre>
|
||||
|
||||
<p>Now let’s see how you can use this rules file.
|
||||
|
||||
<p>[FIXME: now that this chapter comes before the I/O chapter, need to at least mention what open() does]
|
||||
<p>[FIXME: try/finally -> with]
|
||||
<p class=d>[<a href=examples/plural4.py>download <code>plural4.py</code></a>]
|
||||
<pre><code class=pp>import re
|
||||
|
||||
@@ -276,20 +274,16 @@ $ $ s</code></pre>
|
||||
return [matches_rule, apply_rule]
|
||||
|
||||
rules = []
|
||||
<a>pattern_file = open('plural4-rules.txt') <span class=u>②</span></a>
|
||||
try:
|
||||
<a>with open('plural4-rules.txt') as pattern_file: <span class=u>②</span></a>
|
||||
<a> for line in pattern_file: <span class=u>③</span></a>
|
||||
<a> pattern, search, replace = line.split(None, 3) <span class=u>④</span></a>
|
||||
<a> rules.append(build_match_and_apply_functions( <span class=u>⑤</span></a>
|
||||
pattern, search, replace))
|
||||
finally:
|
||||
<a> pattern_file.close() <span class=u>⑥</span></a></code></pre>
|
||||
pattern, search, replace))</code></pre>
|
||||
<ol>
|
||||
<li>The <code>build_match_and_apply_functions()</code> function has not changed. You’re still using closures to build two functions dynamically that use variables defined in the outer function.
|
||||
<li>Open the file that contains the pattern strings.
|
||||
<li>Read through the file one line at a time, using the <code>for line in <fileobject></code> idiom.
|
||||
<li>The global <code>open()</code> 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 <code>with</code> statement creates what’s called a <i>context</i>: when the <code>with</code> block ends, Python will automatically close the file, even if an exception is raised inside the <code>with</code> block. You’ll learn more about <code>with</code> blocks in the <a href=files.html>Files</a> chapter.
|
||||
<li>The <code>for line in <fileobject></code> idiom reads data from the open file, one line at a time, and assigns the text to the <var>line</var> 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 <a href=files.html>Files</a> chapter.
|
||||
<li>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 <code>split()</code> string method. The first argument to the <code>split()</code> method is <code>None</code>, which means “split on any whitespace (tabs or spaces, it makes no difference).” The second argument is <code>3</code>, which means “split on whitespace 3 times, then discard the rest of the line.” A line like <code>[sxz]$ $ es</code> will be broken up into the list <code>['[sxz]$', '$', 'es']</code>, which means that <var>pattern</var> will get <code>'[sxz]$'</code>, <var>search</var> will get <code>'$'</code>, and <var>replace</var> will get <code>'es'</code>. That’s a lot of power in one little line of code.
|
||||
<li>Use a <code>try..finally</code> block to ensure the file object is closed.
|
||||
</ol>
|
||||
|
||||
<p>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:
|
||||
|
||||
<p class=d>[<a href=examples/plural5.py>download <code>plural5.py</code></a>]
|
||||
<pre><code class=pp>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):
|
||||
<p>Let’s go back to <code>plural5.py</code> and see how this version of the <code>plural()</code> function works.
|
||||
|
||||
<pre><code class=pp>def rules():
|
||||
for line in open('plural5-rules.txt'):
|
||||
<a> pattern, search, replace = line.split(None, 3) <span class=u>②</span></a>
|
||||
<a> yield build_match_and_apply_functions(pattern, search, replace) <span class=u>③</span></a>
|
||||
with open('plural5-rules.txt') as pattern_file:
|
||||
for line in pattern_file:
|
||||
<a> pattern, search, replace = line.split(None, 3) <span class=u>②</span></a>
|
||||
<a> yield build_match_and_apply_functions(pattern, search, replace) <span class=u>③</span></a>
|
||||
|
||||
def plural(noun):
|
||||
<a> for matches_rule, apply_rule in rules(): <span class=u>④</span></a>
|
||||
<a> for matches_rule, apply_rule in rules(): <span class=u>④</span></a>
|
||||
if matches_rule(noun):
|
||||
return apply_rule(noun)</code></pre>
|
||||
<ol>
|
||||
|
||||
Reference in New Issue
Block a user