Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Code Refactoring in python

Shulou Source: shulou.com Published: 2022-06-02 07:10:37 09月29日 Update

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!

Tags: Functions code refactoring content parentheses processing four operations operations names characters strings situations methods more modules negative numbers problems encapsulation fastest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple vpn Docker Shulou Information