Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Python code formatting tool pycodestyle

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use the Python code formatting tool pycodestyle". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Look at the following code:

Import time, datetime

Class ListNode:

Def _ _ init__ (self, val):

Self.val = val

Self.next = None

# in python next is a reversed word

Def reverse (self, head):

Prev = None

While head:

Temp = head.next

Head.next = prev

Prev = head

Head = temp

A = [

[

one,

U'hello world'

0

]

[

two,

"hello python"

0

]

]

This is a code snippet that does not conform to PEP8. You can use pycodestyle to detect where it does not conform to PEP8 style.

$pycodestyle link.pylink.py:1: [E401] multiple imports on one linelink.py:3: [E302] expected 2 blank lines, found 1

Pycodestyle reminds us that there are two areas that do not conform to the specification. The first is that there are multiple import in a single line, and the second is that there are two blank lines between classes and modules. This is just a simple code example. The real business code may have hundreds or even hundreds of lines. It will be very time-consuming if we want to modify the recommendations one by one according to the PEP8 specification. In our development process, if we always pay attention to whether each line of code is fully compliant with PEP8. Will affect the efficiency of development.

And there is such a tool Black, known as uncompromising code formatting tool, why is it called uncompromising? Because it detects code styles that do not conform to the specification and directly formats them for you, you don't need to be sure at all and make decisions for you directly. It is also one of the favorite tools of requests authors.

It is very easy to use. After successful installation, it is used like other system commands. You only need to specify the file or directory to be formatted after the black command to ok.

Black link.py

This is a small and beautiful tool, it is not completely formatted according to the PEP8 specification, for example, the default number of characters per line of code is 88, of course, you can customize the length through the parameter-l, and the finished code can be displayed on one line, such as a list of multiple elements.

# in:

L = [1

two,

three,

]

# out:

L = [1, 2, 3]

The latter puts multiple elements on one line, which is obviously easier to read and the code is more compact (not recommended if your salary is based on the number of lines of code), and Black is a strict subset of PEP8. My best practice is to use the formatting tool that comes with PyCharm with Black. Because Black also supports integration into Pycharm.

How to integrate Pycharm

1. Install black:

Pip install black

2. Find the installation path of black

$which black # linux/mac $where black # windows

3. Add extension tools, open Preferences- > Tools- > External Tools, and add a new extension tool. Enter the installation path of black for Program and $FilePath$ for Arguments.

4, select Tools- > External Tools- > Black to format the currently opened file jin watermelon code, of course, you can also specify a shortcut key to make the operation more convenient.

This is the end of the content of "how to use the Python Code formatting tool pycodestyle". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 297

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report