Python Code Optimization Practices

Source: Internet
Author: User

The most recent script file that writes a one-click replacement file in Python, the approximate function is to pass a local or sftp directory parameter to the program, and the program can replace all files in the specified directory with the corresponding directory for the particular application. The program provides the following 2 command line invocations:

Usage:demo.py [SourceDir]
Usage:demo.py [SourceDir] Bydir

The actual operation of the first invocation is to read all the files in a particular application directory and get the full path as a collection, and then match the files in the Parameters folder to the files in the collection by file name, and perform the replace operation if the match.

The actual operation of the second invocation is to completely replace the corresponding directory of the application by the path to the directory where the parameter folder is stored.

The following is the initial code implementation:

#执行本地文件替换的具体操作 def replacelocalfiles (filepath, context, Filecontext, Softpath, Bydir): if (":" Not in filepath) or (not Os.path.isdir (filepath)): Printandwritelog (U "Destination path is wrong, please confirm it is directory and then retry") return "error" FileList = Os.walk (filep                ATH) for root, dirs, files in filelist:for file in Files:if bydir: #如果按目录进行替换的话走下面这个逻辑分支                Filefullpath = Os.path.join (root, file) Targetfullpath = Filefullpath.replace (filepath, Softpath) Shutil.copy2 (Filefullpath, Targetfullpath) printandwritelog (u "file%s copy to%s success"% (Filefullpath, t                Argetfullpath) Else: #如果自行查找文件路径进行替换的话先走下这个逻辑分支 filecounts = checkcount (file, Filecontext)                    if (0 = = filecounts): Printandwritelog (U "did not find the path to the file%s, replace it with the specified path"% file) Continue Elif (1 < filecounts): Printandwritelog (U "file%s has%s path, replace with specified path"%    (file, filecounts))                Continue elif (1 = = filecounts): For line in Context.split ("\ n"): filename = line.split ("\ \") [-1] If file = = Filename:os                             . Rename (line, line + str (random.randint (0))) Shutil.copy2 (Os.path.join (root, file), line)                    Printandwritelog (U "file%s copy to%s success"% (Os.path.join (root, file), line)) Else: Printandwritelog (U "replacement file number error%s"% file) #判断如果是本地文件则直接调用替换函数, if it is a network path, first download the file and then replace the Def relpacefiles (FilePath, CO ntext, Filecontext, Softpath, Bydir): If ":" In Filepath:printandwritelog (U "provided local path, walk local path file replacement process") Replac Elocalfiles (filepath, context, Filecontext, Softpath, Bydir) else:printandwritelog (U "ftp path provided, first download the file to local and then perform the replacement process" ) Sourcefiledir = Cur_file_dir () + R "\testdir" if Os.path.isdir (sourcefiledir): Shutil.rmtree (SOU Rcefiledir) obj = Netutilex. SFTP ("192.168.1.100", "Test", "Testpwd") Obj.syncsftpdir (filepath, Sourcefiledir) obj.close () Replace Localfiles (Sourcefiledir, Context, Filecontext, Softpath, Bydir) #先处理替换前的前置操作, the environment is ready to perform the replacement operation Def Replaceallfiles (        filepath, bydir): Softpath = Checkinst () if ("notinst" = = Softpath): Printandwritelog (U "did not detect the defender installation directory, please confirm and retry") Return "error" else:context, Filecontext = Getallfiles (softpath) relpacefiles (filepath, context, Filecontext, Softpath, Bydir)

The functions of the following functions are described briefly:

Replacelocalfiles: The main function function, realizes the concrete substitution operation;
Relpacefiles: According to the incoming parameters to determine whether the network path, if it is the first to download the file to local, and then call Replacelocalfiles to perform the replacement operation;
Replaceallfiles: Do some environment preparation, and then call the actual function function Relpacefiles to do the work;
Printandwritelog: Logging and output;
Checkinst: Check whether the target program is installed and return the installation path if installed;
Getallfiles: Gets the file full path collection of the target application;
Checkcount: Gets the number of occurrences of the specified file name in the target application file collection
Netutilex: A standalone library file that operates on SFTP.

There are at least 2 locations that can be optimized from the current code:

    1. There are too many parameters to pass between functions, so you can see if it is all necessary, and consider how to streamline it.
    2. Some of the business logic is too granular, with duplicated code implementations, resulting in a more bloated implementation.

For the 1th, the idea of optimization is: for non-all functions must be called parameters, as far as possible to solidify into the actual use of the function, to avoid the function only do the work of the transitive.

For the 2nd, the idea of optimization is: Merge similar terms, for the part of repeating code, as far as possible to extract to the common logic implementation.

The following is the optimized code:

#执行本地文件替换的具体操作 def replacelocalfiles (filepath, Bydir): if (":" Not in filepath) or (not Os.path.isdir (filepath)): Printandwritelog (U "Destination path is incorrect, please verify that it is a valid directory and retry") return "error" Softpath = Checkinst () if ("notinst" = = Softpat     h): Printandwritelog (U "not get to target software installation directory, please confirm and retry") return "error" context, Filecontext = Getallfiles (Softpath)  FileList = Os.walk (filepath) for root, dirs, files in filelist:for file in Files:filefullpath = Os.path.join (root, file) Targetfullpath = Filefullpath.replace (filepath, Softpath) if not bydir: #如果                    Find the file path to replace it first, go down this logical branch filecounts = Checkcount (file, Filecontext) if (0 = = filecounts): Printandwritelog (U "did not find the path to the file%s, replace it with the specified path"% file) continue Elif (1 & Lt                    filecounts): Printandwritelog (U "file%s has%s path, replace with specified path"% (file, filecounts))           Continue     elif (1 = = filecounts): For line in Context.split ("\ n"): filename = line.sp                Lit ("\ \") [-1] If file = = Filename:targetfullpath = Line                Else:printandwritelog (U "number of files replaced%s"% file) if Os.path.isfile (Targetfullpath):            Randomend = Random.randint (0, +) os.rename (Targetfullpath, Targetfullpath + str (randomend)) Shutil.copy2 (Filefullpath, Targetfullpath) printandwritelog (u "file%s copy to%s success"% (Filefullpath, Targetfullpat        h)) #先处理替换前的前置操作, the environment is ready to perform the replacement operation Def Replaceallfiles (filepath, bydir): Sourcefiledir = FilePath if ":" In filepath: Printandwritelog (U "provided local path, walk local path file replacement process") Else:printandwritelog (U "provided ftp path, first download the file to local and then perform the replacement process") sour        Cefiledir = Cur_file_dir () + R "\testdir" if Os.path.isdir (sourcefiledir): Shutil.rmtree (Sourcefiledir) obj = Netutilex. SFTP ("192.168.1.100", "Test", "Testpwd") Obj.syncsftpdir (filepath, Sourcefiledir) obj.close () replacelocalf Iles (Sourcefiledir, Bydir)

The specific optimization operations are:

The implementation of the function Checkinst and getallfiles is put into the function body of the replacelocalfiles of its return value, which reduces the multiple passes of 2 parameters;
The specific copy2 operation in the function replacelocalfiles is extracted, because bydir and non-bydir will eventually go to this operation;
The operation of function replacelocalfiles in function replacefiles is extracted, and the function replaceallfiles and replacefiles are combined.

Does the optimized result look a lot fresher?

Python Code Optimization Practices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.