Python 3: A Step-by-Step Guide to Learn, in an Easy Way, the Fundamentals of Python Programming Language by John Bach

Python 3: A Step-by-Step Guide to Learn, in an Easy Way, the Fundamentals of Python Programming Language by John Bach

Author:John Bach [Bach, John]
Language: eng
Format: azw3, epub, azw, pdf
Published: 2020-09-17T16:00:00+00:00


Regex substitutions are extremely powerful, and the \ 1 syntax makes them even more powerful. But the whole operation, combined into one regular expression, also becomes difficult to read, in addition, this method does not directly correlate with how you originally described the rules for forming the plural. Initially, you designed rules in the form "if a word ends in S , X, or Z , add ES ." And if you are looking at a function, then you have two lines of code that say "if the word ends in S , X, or Z , add ES ." It will not work even closer to the original version.

Feature List

Now you will add an abstraction layer. You started by defining a list of rules: if this is true, do that, otherwise refer to the next rule. Let's temporarily complicate part of the program so that you can simplify another part of it.

import re def match_sxz ( noun ) : return re . search ( '[sxz] $' , noun ) def apply_sxz ( noun ) : return re . sub ( '$' , 'es' , noun ) def match_h ( noun ) : 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 = (( 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: ④ if matches_rule ( noun ) : return apply_rule ( noun )



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.