Home  >  Article  >  Backend Development  >  Automatic formatting tool for Python language

Automatic formatting tool for Python language

WBOY
WBOYforward
2023-04-13 09:16:122452browse

Each programming language has its own dedicated formatting tools, such as gofmt in golang language, prettier in JavaScript language, and php-cs-fixer in php language. Similar to these programming languages, python also has its own automated formatting tool. But the difference is that it has many formatting tools. Next, let’s take a look at the differences between these different formatting tools.

autopep8

This is the earliest Python formatting code tool. It uses pycodestyle to analyze the code, and then repairs the code that does not conform to the pep style. At the same time, it will also fix some deprecated code, standardize the end of the code, and add some blank line separators for some methods and functions.

autopep8 supports formatting declaration through configuration files, it supports it. Configuration files in cfg, .ini .pep8 .flake8 and other formats. Currently he has more than 4200 likes on github.

yapf

This is a formatting tool launched by Google. Because it is endorsed by a major manufacturer, it has developed rapidly and currently has 13,000 likes on github. It doesn't check pep guidelines, it just formats the code. It adopts an idea similar to ​​clang-format​​, which is to read the entire file code and then format it into the best format.

It is a configurable code format solution. Different configurations will lead to different formatting results. The current mainstream configurations include pep8, google, facebook, chromium, etc. In addition, it also supports custom configuration schemes, and the format style can be specified through​​--style​​.

x = {'a':37,'b':42,

'c':927}

y = 'hello ''world'
z = 'hello '+'world'
a = 'hello {}'.format('world')
class foo( object):
def f(self ):
return 37*-+2
def g(self, x,y=42):
return y
def f( a )
return37+-+a[42-x :y**3]

After the messy code above is formatted by ​​yapf​​, it will become very regular and easier to read.

x = {'a': 37, 'b': 42, 'c': 927}

y = 'hello ' 'world'
z = 'hello ' + 'world'
a = 'hello {}'.format('world')

class foo(object):
def f(self):
return 37 * -+2

def g(self, x, y=42):
return y

def f(a):
return 37 + -+a[42 - x:y**3]

black

black is a new python code formatting tool launched in recent years. Although it is the latest, it is the most popular. Currently, it has the highest number of points on github. There are more than 30,000 likes.

Automatic formatting tool for Python language

#black is known as an uncompromising code formatting tool. In other words, it does not allow you to manually adjust the details of the code format by default.

Automatic formatting tool for Python language

It has very efficient speed and is used by many projects.

Automatic formatting tool for Python language

Black has a comprehensive test suite with efficient parallel functional testing,

Here are some organizations using black: Facebook, Dropbox, KeepTruckin, Mozilla , Quora, Duolingo, QuantumBlack, Tesla, Archer Aviation.

You can see that many famous companies use it to format python code, just because it is so easy to use and convenient.

Summary

autopep8 has almost been abandoned. This can be seen from the number of likes on github. yapf has a more flexible and highly customized formatting solution, while black Make the program look more unified and elegant, and avoid complex configurations.

Automatic formatting tool for Python language

If your company or you are using python for project development, then it is very necessary to choose a code formatting tool, which can save your development team a lot of money. time. And whether it's your personal code or other people's code format will look the same, so everyone will be happy reading other people's code.

The above is the detailed content of Automatic formatting tool for Python language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete