In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to achieve code refactoring in python, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Before reconstruction
Import redef count (s): while'/'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +) / ([-]?\ d +\.\ d + | [-]?\ d +)', s) for i in result:s = s.replace (f'{I [0]} / {I [1]}' F'{float (I [0]) / float (I [1])}') while'*'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)\ * ([-]?\ d +)', s) for i in result:if float (I [0]) < 0 and float (I [1]) < 0in s:result s = s.replace (f'{I [0]} * {I [1]}') Float + {float (I [0]) * float (I [1])}') else:s = s.replace (f'{I [0]} * {I [1]}', f'{float (I [0]) * float (I [1])}') result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)' S) x = 0for I in result:x + = float (I) s = str (x) return sdef cal (s): s = s.replace (',') while'('in s or')'in s:ret = re.findall ('\ ([^ ()] +)\), s) for i in ret:s = s.replace (f'({I})', count (I)) s = s.replace ('- -' '+') else:s = count (s) return s reconstruction principle
The refactoring code should analyze the original code, find out the duplicate code and encapsulate it into a function.
The comments are clear and complete, which is convenient for future upgrade iterations.
Code modularization, modularization can improve the code reuse rate and isolate bug.
The refactored code import redef cal (s):''deals with the main program of four parenthesized operation strings. First calculate the content in parentheses, replace the content with the calculated value, and finally calculate the result.'' Def count (s):''to calculate four operations without parentheses, first calculate multiplication and division Then calculate the addition and subtraction 'while' /'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +) / ([-]?\ d +\.\ d + | [-]?\ d +)', s) for i in result:s = s.replace (f'{I [0]} / {I [1]}' F'{float (I [0]) / float (I [1])}') while'*'in s:result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)\ * ([-]?\ d +\.\ d + | [-]?\ d +)' S) for i in result:if float (I [0]) < 0 and float (I [1]) < 0: # Special case s = s.replace (f'{I [0]} * {I [1]}', flip + {float (I [0]) * float (I [1])}') else:s = s.replace (f'{I [0]} * {I [1]}') F'{float (I [0]) * float (I [1])}') result = re.findall ('([-]?\ d +\.\ d + | [-]?\ d +)', s) x = 0for I in result:x + = float (I) s = str (x) return sdef symbol (s): multiple consecutive + signs and-signs' while'+ +'in Spurs = s.replace ('+ +') appear in the four operation strings '+') while'+ -'in svig s = s.replace ('+ -','-') while'- +'in svie s = s.replace ('- +','-') while'- -'in svie s = s.replace ('- -','+') return s s = s.replace (',') while'('in s or')'in s:ret = re.findall ('\ ([^ ()] +?)\) S) for i in ret:s = s.replace (f'({I})' Count (I)) s = symbol (s) # deals with minus signs after stripping parentheses else:s = count (s) return sprint (cal ('10-3 * (50-30 + (- 10-10) 5) * (9-2 + (- 10) 5) * (9-2) 5 ~ (9-2) ~ 99 ~ (4) ~ (10 * 789 ~ 15) print (cal ('10-10)) ) print (cal ('10-3 * (20-10 + (20-10 + (- 10-5)) * 27)
Please pay attention to the writing of the defined function in the function. For example, only the cal function can call the above two functions, count and symbol, so the definition inside the cal function is the best choice. In this way, the encapsulation is better and the operation efficiency is higher.
When calling other functions within a function, you will first find the name from your own namespace, go to the outer layer if you can't find it, and look for it globally if you can't find it. So the name defined inside the function is the fastest to find.
After reading the above, have you mastered how to implement code refactoring in python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.