mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
use lists instead of tuples in generator chapter too [thanks A.H.]
This commit is contained in:
+5
-5
@@ -31,11 +31,11 @@ def match_default(noun):
|
||||
def apply_default(noun):
|
||||
return noun + 's'
|
||||
|
||||
rules = ((match_sxz, apply_sxz),
|
||||
(match_h, apply_h),
|
||||
(match_y, apply_y),
|
||||
(match_default, apply_default)
|
||||
)
|
||||
rules = [[match_sxz, apply_sxz],
|
||||
[match_h, apply_h],
|
||||
[match_y, apply_y],
|
||||
[match_default, apply_default]
|
||||
]
|
||||
|
||||
def plural(noun):
|
||||
for matches_rule, apply_rule in rules:
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
return re.search(pattern, word)
|
||||
def apply_rule(word):
|
||||
return re.sub(search, replace, word)
|
||||
return (matches_rule, apply_rule)
|
||||
return [matches_rule, apply_rule]
|
||||
|
||||
patterns = \
|
||||
[
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
return re.search(pattern, word)
|
||||
def apply_rule(word):
|
||||
return re.sub(search, replace, word)
|
||||
return (matches_rule, apply_rule)
|
||||
return [matches_rule, apply_rule]
|
||||
|
||||
rules = []
|
||||
pattern_file = open('plural4-rules.txt')
|
||||
|
||||
+1
-1
@@ -12,7 +12,7 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
return re.search(pattern, word)
|
||||
def apply_rule(word):
|
||||
return re.sub(search, replace, word)
|
||||
return (matches_rule, apply_rule)
|
||||
return [matches_rule, apply_rule]
|
||||
|
||||
def rules():
|
||||
for line in open('plural5-rules.txt'):
|
||||
|
||||
+3
-3
@@ -12,13 +12,13 @@ def build_match_and_apply_functions(pattern, search, replace):
|
||||
return re.search(pattern, word)
|
||||
def apply_rule(word):
|
||||
return re.sub(search, replace, word)
|
||||
return (matches_rule, apply_rule)
|
||||
return [matches_rule, apply_rule]
|
||||
|
||||
class LazyRules:
|
||||
rules_f = 'plural6-rules.txt'
|
||||
rules_filename = 'plural6-rules.txt'
|
||||
|
||||
def __init__(self):
|
||||
self.pattern_file = open(self.rules_f)
|
||||
self.pattern_file = open(self.rules_filename)
|
||||
self.cache = []
|
||||
|
||||
def __iter__(self):
|
||||
|
||||
Reference in New Issue
Block a user