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():
|
||||
|
||||
Reference in New Issue
Block a user