Code: Alles auswählen
#!/usr/local/bin/python3
import re
def match_sxz(noun):
return re.search('[sxz]$', noun)
def apply_sxz(noun):
return re.sub('$', 'es', noun)
def match_h(noun):
# 'fish' would match, 'match' would match.
return re.search('[^aeioudgkprt]h$', noun)
def apply_h(noun):
return re.sub('$', 'es', noun)
def match_y(noun):
return re.search('[^aeiou]y$', noun)
def apply_y(noun):
return re.sub('y$', 'ies', noun)
def match_default(noun):
return True
def apply_default(noun):
return noun + 's'
# Rules data structure.
rules = ((match_sxz, apply_sxz),
(match_y, apply_y),
(match_h, apply_h),
(match_default, apply_default)
)
def plural(noun):
for matches_rule, apply_rule in rules:
print("Checking:", matches_rule)
if matches_rule(noun):
return apply_rule(noun)
print(plural("vacancy"))
Code: Alles auswählen
>>> a=4
4
>>> for i in range(10):
... print a==i
...
False
False
False
False
True
False
False
False
False
False
>>>
Stattdessen
Code: Alles auswählen
Checking: <function match_sxz at 0x61bdb0>
Checking: <function match_y at 0x633150>
vacancies