python code specification automatic optimization tool Black

Black automatic optimization tools

Among the many code formatting tools, Black is a relatively new one, it is characterized *** configurable items is relatively small, personally think that this is good for the novice, because we do not give much thought to how to set Black, let Black make their own decisions just fine.

1). Installation and Use

And pylint Similarly, direct pip install black to complete the installation of the module, but depends on the black Python 3.6+, but it can still Python2 formatted code.

In use the default black python read the specified file is formatted and subjected to standardized code, and then output to the original file.

  1. l = [1, 
  2.     2, 
  3.     3, 

For example, we will save this code as above test.py, then execute black test.py command in the console, open test.py again and found that code becomes like this:

  1. l = [1, 2, 3] 

Of course, no matter how high the degree of encapsulation Black is also a custom configuration items, such as using --version view the version, use --help to view the help information, use --diff to modify the information output to the console without changing the original file, the following we also combine an example to demonstrate explained.

2). Examples demonstration shows

Here we still use the code pylint part of the presentation. We know that through the above operation, the direct use of this code pylint test output will prompt a lot of questions, and gives a score 0. Now we are the first to use black to format it, get the following code:

  1. # -*- coding:utf-8 -*- 
  2.  
  3. import pandas as pd 
  4.  
  5. data = [] 
  6. char_replace_dict = {':':'  ', '(':'(', ')':')', ',':','} 
  7.  
  8. with open('xmq_survey.txt', 'r', encoding = 'utf-8') as file: 
  9.  for line in file.readlines(): 
  10.    for key, value in char_replace_dict.items(): 
  11.      Line = Line. the replace ( Key, value) # This is the original depth reference 
  12.      # This code than more than write their own profile, and more python 
  13.    data.append(line) 
  14.  
  15. with open('survey.txt', 'w', encoding = 'utf-8') as file: 
  16.  for line in data: 
  17.    file.write(line) 
  18.  
  19. = pd.read_table raw_data ( 'survey.txt', DELIMITER =  '', header = None) # Usage View function read_table 
  20. raw_data.columns = ['Name', 'Raw Info'] 
  21. raw_data.count() 
  22. print('successful') 

Code differences may appear before and after modification is not very clear, black has been substantially across the assigned code space = parameter, the format of the comment, and so the tabs alternatives and modifications pylint we use to verify, performed pylint command module name, to give the following results:

 

It can be seen in relation to the original file, score increased from 0 to 7.3, the output of the problem prompted a lot less, the remaining problem is the lack of documentation, variable naming non-standard .black to improve our code specification is also very cost-effective High.

If you do not want black directly to the original file is modified, but would like to see where it made changes to the code, you can use --diff parameters, perform black --diff file name, black will be output to the control information table (below, where - represents the source code, the code + indicates proposed changes), the original document will not be modified.

 

In short, black is really a very nice library, especially for the novice, it is easy to regulate their own coding style. 

Guess you like

Origin www.cnblogs.com/bonelee/p/11243359.html