The python script replaces the specified line implementation step

  • 2020-06-12 09:31:49
  • OfStack

The python script replaces the specified line implementation step

This paper mainly introduces the script replacement of Python. Due to the need of work, the logging system must be updated. Here is a good article searched on the Internet.

You need to migrate the code and update the original logging system to the current format, which was used to get log


AuctionPoolLoggerUtil.getLogger() 

Now the format for getting log is:


LoggerFactory.getLogger(XXXXX.class) 

XXXXX here needs to be replaced with the current class name. If there aren't many java files like this, you can replace them with a personal one. Once you have a lot of files like this, especially if you migrate a lot of files, you will find that it is a disaster. In fact, we find that much of the work is mechanically monotonous. What the substitution feature in ide does not do is replace XXXXX with the current class name. python, on the other hand, is easy to work with text, and regular expressions make it easy to get the class name and replace xxxxx.

Implementation code:


import fileinput 
import os 
import re 
 
__author__ = 'ykdsg' 
 
packDir='/Users/ykdsg/svn_workspace/auctionplatform/misc_refactory/auctionplatform/ap-biz/src/main/java/com/yk/misccenter' 
# To find the class name 
findClassNameP=re.compile(r"(?<=class\s)\w*") 
findXP=re.compile(r"XXXXX") 
 
 
def processDirectory(args,dirname,filenames): 
  # print 'Directory',dirname 
  for filename in filenames: 
 
    if os.path.splitext(filename)[1]=='.java': 
      # print 'file',filename 
      fullFileUrl=dirname+ "/"+filename 
      fileObj=open(fullFileUrl) 
      className='' 
      # Optional in-place filtering: if the keyword argument inplace=1 is passed to fileinput.input() or to 
      # the FileInput constructor, the file is moved to a backup file and standard output is directed to the 
      # input file (if a file of the same name as the backup file already exists, it will be replaced silently) 
      # . This makes it possible to write a filter that rewrites its input file in place. If the backup 
      # parameter is given (typically as backup='.<some extension>'), it specifies the extension for the 
      # backup file, and the backup file remains around; by default, the extension is '.bak' and it is deleted 
      # when the output file is closed. In-place filtering is disabled when standard input is read. 
      for line in fileinput.input(fullFileUrl, inplace=1): 
        matchClass = findClassNameP.search(line) 
        if matchClass: 
          className = matchClass.group() 
        matchX=findXP.search(line) 
        if matchX: 
          #print  You need to have ,  Otherwise there will be an extra blank line  
          print line.replace('XXXXX',className), 
        else: 
          print line, 
 
 
def search(): 
  os.path.walk(packDir,processDirectory,None) 
 
if __name__ == '__main__': 
  search() 

Most of the script above is a comment on fileinput.input, which means that inplace=1 essentially puts the contents of the source file into the cache and writes the contents directly to the source file

findClassNameP is a regular expression that looks for class name. The logic above is to go through the file line by line and get class name. Then analyze whether the current row has xxxxx, replace it with class name if it does, and output the original row if it doesn't.

The above is a simple example of using python script to replace the specified line. If you have any questions or better ways, please leave a comment. Thank you for reading.


Related articles: