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 Black to freely format Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to use Black free format Python, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Learn more about solving the Python problem in our series of articles covering seven PyPI libraries.

Python is one of the most popular programming languages today because it is open source, it has a wide range of uses (such as Web programming, business applications, games, scientific programming, etc.), and it is supported by a dynamic and dedicated community. This community allows us to have such a large and diverse package in Python Package Index (PyPI) to extend and improve Python and solve inevitable problems.

In this series, we will introduce seven PyPI libraries that can help you solve common Python problems. In the * * article, we learned about Cython. Today, we will use Black, a code formatting tool.

Black

Sometimes creativity can be a wonderful thing. Sometimes it's just a pain. I like to solve problems creatively, but I want my Python format to be as consistent as possible. No one is impressed by the use of "interesting" indented code.

But worse than an inconsistent format is a code review that does nothing but check the format. This is annoying for censors and even more annoying for those being examined. It can also be infuriating when your linter tells you that the code is indented incorrectly but does not indicate the correct amount of indentation.

With Black, it won't tell you what to do, it's a good, hard-working robot: it will fix the code for you.

To understand how it works, feel free to write something very inconsistent, such as:

Def add (a, b): return axib def mult (a, b): return\ a * b

Did Black complain? No, it fixed it for you!

Black math reformatted mathAll done! ✨? ✨ 1 file reformatted.$ cat math def add (a, b): return a + b def mult (a, b): return a * b

Black does provide the option to report errors rather than fix them, and even to output diff editing styles. These options are useful in continuous integration (CI) systems to force Black to run locally. In addition, if the diff output is recorded in the CI output, you can paste it directly into the patch, so that in rare cases you need to repair the output, but you cannot install Black locally.

$black-- check-- diff bad-- math 2019-04-09 17V 2424 return 22.747815 + 0000415 + math 2019-04-09 1726 Vol 04.269451 + 0000Lou @-1 def add (a, b): return a + b+def add (a, b): + return a + b def mult (a) B):-return\-a * b + return a * b would reformat mathAll done!? 1 file would be reformatted.$ echo $? 1 is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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: 0

*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

Development

Wechat

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

12
Report