python black_如何使用Black自动格式化Python代码

python black

Writing Python code is one thing and writing the code in a good format is another thing. Junior programmers often focus on making sure their code is working and forget to format the code properly along the way.

编写Python代码是一回事,以良好的格式编写代码是另一回事。 初级程序员经常专注于确保他们的代码正常工作,而忘记沿途正确格式化代码。

If you write a small program (with 1000 lines of codes) you can probably get away without formatting your code.

如果您编写一个小型程序(包含1000行代码),则可能无需格式化代码就可以逃脱。

But as programs get more and more complex, they get harder and harder to understand. At some point (around 15,000 lines of code), it becomes harder to understand the code that you yourself wrote.

但是,随着程序变得越来越复杂,它们变得越来越难理解。 在某个时候(大约15,000行代码),变得很难理解您自己编写的代码。

The difference between working on well-formatted code and working on badly formatted code is like the difference between living in a palace and living in a dirty house.

处理格式正确的代码与处理格式错误的代码之间的区别就像住在宫殿和脏房子里的区别。

为什么格式化python代码很重要 (Why formatting your python code is important)

可读性 (Readability)

Formatting your code will help you read your code efficiently. It looks more organized, and when someone looks at your code they'll get a good impression.

格式化代码将帮助您有效地 阅读代码。 它看起来更有条理,当有人查看您的代码时,他们会得到很好的印象。

这将有助于您的编码面试 (It will help in your coding interviews)

When you're in a coding interview, sometime the interviewers will care if you’re formatting your code properly. If you forget to do that formatting you might lose your job prospects, just because of your poorly formatted code.

在进行编码面试时,面试官有时会关心您是否正确设置了代码格式。 如果您忘记进行这种格式化,则可能会因为代码格式不正确而失去工作机会。

团队支持 (Team support)

Formatting your code becomes more important when you are working in a team. Several people will likely be working on the same software project and code you write must be understood by your teammates. Otherwise it becomes harder to work together.

团队中工作时,格式化代码变得更加重要 可能会有几个人在同一个软件项目上工作,并且您的队友必须理解您编写的代码。 否则,一起工作将变得更加困难。

轻松发现错误 (It makes it easy to spot bugs)

Badly formatted code can make it really, really hard to spot bugs or even to work on a program. It is also just really horrible to look at. It’s an offense to your eyes.

格式不正确的代码会使查找错误甚至在程序上工作变得非常困难。 看着真的也很恐怖。 对您来说是冒犯。

皮林特和弗莱克8 (Pylint and Flake8)

Most Python developers enjoy using Pylint or Flake8 to check their code for errors and style guides.

大多数Python开发者喜欢使用pylint的或Flake8检查他们的错误和风格指南的代码。

Pylint is a tool that checks for errors in Python. It tries to enforce a coding standard and looks for code smells. It can also look for certain type errors, it can recommend suggestions about how particular blocks can be refactored, and can offer you details about the code’s complexity.

Pylint是用于检查Python中错误的工具。 它试图强制执行编码标准并查找代码气味。 它还可以查找某些类型错误,可以建议有关如何重构特定块的建议,并可以为您提供有关代码复杂性的详细信息。

Flake8 is a Python library that wraps PyFlakes, pycodestyle and Ned Batchelder’s McCabe script. It is a great toolkit for checking your code base against coding style (PEP8), programming errors like “library imported but unused”, “Undefined name” and code which is not indented.

Flake8是一个Python库,其中包装了PyFlakespycodestyleNed Batchelder的McCabe脚本 。 这是一个用于检查您的代码库是否符合编码样式(PEP8) ,编程错误(如“已导入但未使用的库”,“未定义的名称”和未缩进的代码)的出色工具包。

The problem is that these tools only report the problems they identify in the source code and leave the burden to the Python developers to fix them!

问题在于这些工具仅报告它们在源代码中识别出的问题,而让Python开发人员去解决这些问题!

But what if we had a tool that could identify and solve the problem at the same time? Black is a tool that allows you to identify errors and format your python code at the same time. Thus it makes you more productive.

但是,如果我们拥有可以同时识别和解决问题的工具,该怎么办? Black是一种工具,可让您识别错误并同时设置python代码的格式 。 因此,它使您工作效率更高。

黑色概论 (Introduction to Black)

From the project README:

从项目自述文件:

By using Black, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

通过使用Black ,您同意放弃对手工格式化细节的控制。 作为回报, 布莱克为您提供速度,确定性和免于pycodestyle烦恼格式化的自由。 您将为更重要的事情节省时间和精力。

Black can reformat your entire file in place according to the Black code style. It helps your brain focus on the problem you want to solve and code solutions, rather than getting distracted by code structure and minor stylistic differences.

Black可以根据Black代码样式重新格式化整个文件。 它帮助您的大脑专注于要解决的问题和对解决方案进行编码,而不必因代码结构和较小的样式差异而分心。

So let's see how to use it.

因此,让我们看看如何使用它。

安装黑色 (Install Black)

Black can be installed by running pip install black. It requires Python 3.6.0+ to run. Once Black is installed, you will have a new command-line tool called black available to you in your shell, and you’re ready to start!

可以通过运行pip install black来安装Black。 它需要Python 3.6.0+才能运行。 安装Black后,您将在Shell中使用一个名为black的新命令行工具,并且可以开始使用了!

To get started right away with sensible defaults, choose the python file you want to format and then write black filename.py in the terminal. Then Black will format your python file.

要立即开始使用明智的默认设置,请选择要设置格式的python文件,然后在终端中写入黑色filename.py 。 然后Black将格式化您的python文件。

Now we'll see what Black can help us do.

现在,我们将了解Black可以帮助我们做什么。

格式化单个文件 (Format a Single File)

Let's look at this simple example: here are my two python functions in my python file called sample_code.py.

让我们看一个简单的示例:这是我的python文件中名为sample_code.py的两个python函数。

You can use black sample_code.py in the terminal to change the format. After running Black, you will see the following output:

您可以在终端中使用black sample_code.py更改格式。 运行Black之后,您将看到以下输出:

Then you can open sample_code.py to see formatted python code:

然后,您可以打开sample_code.py查看格式化的python代码:

The Python code is now formatted and it’s more readable.

Python代码现在已格式化,并且更具可读性。

格式化多个文件 (Format Multiple Files)

To format more than one python file, write black folder_name/ in the terminal.

要格式化多个python文件,请在终端中写入black folder_name/

Three python files within the folder named python_with_black have been reformatted.

文件夹python_with_black中的三个python文件已经重新格式化。

检查文件以进行格式化 (Checking Files for Formatting)

If you don’t want Black to change your file, but you want to know if Black thinks a file should be changed, you can use one of the following commands:

如果您不希望Black更改文件,但是想知道Black是否认为应该更改文件,则可以使用以下命令之一:

black --check .: This will check which python file(s) can be formatted in the current folder (but doesn’t actually modify the python file(s)).

black --check . :这将检查可以在当前文件夹中格式化哪些python文件(但实际上不会修改python文件)。

black --check --diff file_name.py : This shows what needs to be done to the file but doesn’t modify the file.

black --check --diff file_name.py :这显示需要对文件执行的操作,但不修改文件。

更改每行字符数 (Change Number of Characters per Line)

Note that Black defaults to 88 characters for its line length, but you can change that using the “-l” or “- -line-length” option.

请注意,黑色的行长默认为88个字符,但是您可以使用“ -l”或“--line-length”选项来更改它。

For example, to change to 60 characters: black -l 60 python_file.py .

例如,要更改为60个字符: black -l 60 python_file.py

Jupyter笔记本中的黑色 (Black in Jupyter Notebook)

For Jupyter notebook users, you can still auto-format your python code with this simple extension called Jupyter Black. This extension reformats/prettifies code in a notebook’s code cell by black.

对于Jupyter笔记本用户,您仍然可以使用名为Jupyter Black的简单扩展名自动格式化python代码。 此扩展名用black重新格式化/美化笔记本计算机代码单元中的代码。

The Jupyter Black extension provides

Jupyter Black扩展程序提供

  • A toolbar button.

    工具栏按钮。
  • A keyboard shortcut for reformatting the current code-cell (default: Ctrl-B).

    用于重新格式化当前代码单元的键盘快捷键(默认值:Ctrl-B)。
  • A keyboard shortcut for reformatting whole code-cells (default: Ctrl-Shift-B).

    用于重新格式化整个代码单元的键盘快捷键(默认值:Ctrl-Shift-B)。

安装Jupyter Black (Install Jupyter Black)

First make sure you have installed jupyter-contrib-nbextensions and black, then run the following commands.

首先,请确保已安装jupyter-contrib-nbextensions和black ,然后运行以下命令。

jupyter nbextension install https://github.com/drillan/jupyter-black/archive/master.zip — user

Then enable the extension by running:

然后通过运行启用扩展:

jupyter nbextension enable jupyter-black-master/jupyter-black

Now you can start formatting your python code in each notebook cell.

现在,您可以开始在每个笔记本单元格中格式化python代码了。

First, select the notebook cell you want to format your python code then click the extension button called Black.

首先,选择要格式化python代码的笔记本单元,然后单击名为Black的扩展按钮。

Then click the Jupyter Black button:

然后单击Jupyter Black按钮:

编辑器整合 (Editor Integration)

You can integrate Black with your favorite editors. Currently Black supports PyCharm/IntelliJ IDEA, Wing IDE, Vim, Visual Studio Code, Sublime Text 3, Atom/Nuclide, Kakoune, and Thonny. Follow the instruction here to integrate Black with your favorite editor.

您可以将Black与您喜欢的编辑器集成在一起。 目前,Black支持PyCharm / IntelliJ IDEA,Wing IDE,Vim,Visual Studio Code,Sublime Text 3,Atom / Nuclide,Kakoune和Thonny。 按照此处的说明将Black与您喜欢的编辑器集成。

If you want to learn more about Black, I recommend watching the PyCon 2019 talk by Łukasz Langa.

如果您想了解有关Black的更多信息,我建议观看ŁukaszLanga的PyCon 2019演讲 。

If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post! I can also be reached on Twitter @Davis_McDavid.

如果您学习了新知识或喜欢阅读本文,请与他人分享,以便其他人可以看到。 在那之前,在下一篇文章中见! 也可以通过Twitter @Davis_McDavid与我联系 。

翻译自: https://www.freecodecamp.org/news/auto-format-your-python-code-with-black/

python black

你可能感兴趣的:(python,java,linux,编程语言,人工智能)