mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-22 23:00:57 +08:00
translated
This commit is contained in:
parent
5d0d3b1462
commit
9f24387e19
@ -1,99 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Format Python however you like with Black)
|
||||
[#]: via: (https://opensource.com/article/19/5/python-black)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez/users/moshez/users/moshez)
|
||||
|
||||
Format Python however you like with Black
|
||||
======
|
||||
Learn more about solving common Python problems in our series covering
|
||||
seven PyPI libraries.
|
||||
![OpenStack source code \(Python\) in VIM][1]
|
||||
|
||||
Python is one of the most [popular programming languages][2] in use today—and for good reasons: it's open source, it has a wide range of uses (such as web programming, business applications, games, scientific programming, and much more), and it has a vibrant and dedicated community supporting it. This community is the reason we have such a large, diverse range of software packages available in the [Python Package Index][3] (PyPI) to extend and improve Python and solve the inevitable glitches that crop up.
|
||||
|
||||
In this series, we'll look at seven PyPI libraries that can help you solve common Python problems. In the first article, we learned about [Cython][4]; today, we'll examine the **[Black][5]** code formatter.
|
||||
|
||||
### Black
|
||||
|
||||
Sometimes creativity can be a wonderful thing. Sometimes it is just a pain. I enjoy solving hard problems creatively, but I want my Python formatted as consistently as possible. Nobody has ever been impressed by code that uses "interesting" indentation.
|
||||
|
||||
But even worse than inconsistent formatting is a code review that consists of nothing but formatting nits. It is annoying to the reviewer—and even more annoying to the person whose code is reviewed. It's also infuriating when your linter tells you that your code is indented incorrectly, but gives no hint about the _correct_ amount of indentation.
|
||||
|
||||
Enter Black. Instead of telling you _what_ to do, Black is a good, industrious robot: it will fix your code for you.
|
||||
|
||||
To see how it works, feel free to write something beautifully inconsistent like:
|
||||
|
||||
|
||||
```
|
||||
def add(a, b): return a+b
|
||||
|
||||
def mult(a, b):
|
||||
return \
|
||||
a * b
|
||||
```
|
||||
|
||||
Does Black complain? Goodness no, it just fixes it for you!
|
||||
|
||||
|
||||
```
|
||||
$ black math
|
||||
reformatted math
|
||||
All done! ✨ 🍰 ✨
|
||||
1 file reformatted.
|
||||
$ cat math
|
||||
def add(a, b):
|
||||
return a + b
|
||||
|
||||
def mult(a, b):
|
||||
return a * b
|
||||
```
|
||||
|
||||
Black does offer the option of failing instead of fixing and even outputting a **diff** -style edit. These options are great in a continuous integration (CI) system that enforces running Black locally. In addition, if the **diff** output is logged to the CI output, you can directly paste it into **patch** in the rare case that you need to fix your output but cannot install Black locally.
|
||||
|
||||
|
||||
```
|
||||
$ black --check --diff bad
|
||||
\--- math 2019-04-09 17:24:22.747815 +0000
|
||||
+++ math 2019-04-09 17:26:04.269451 +0000
|
||||
@@ -1,7 +1,7 @@
|
||||
-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 math
|
||||
All done! 💥 💔 💥
|
||||
1 file would be reformatted.
|
||||
$ echo $?
|
||||
1
|
||||
```
|
||||
|
||||
In the next article in this series, we'll look at **attrs** , a library that helps you write concise, correct code quickly.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/5/python-black
|
||||
|
||||
作者:[Moshe Zadka ][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez/users/moshez/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/openstack_python_vim_1.jpg?itok=lHQK5zpm (OpenStack source code (Python) in VIM)
|
||||
[2]: https://opensource.com/article/18/5/numbers-python-community-trends
|
||||
[3]: https://pypi.org/
|
||||
[4]: https://opensource.com/article/19/4/7-python-problems-solved-cython
|
||||
[5]: https://pypi.org/project/black/
|
@ -0,0 +1,99 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Format Python however you like with Black)
|
||||
[#]: via: (https://opensource.com/article/19/5/python-black)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez/users/moshez/users/moshez)
|
||||
|
||||
使用 Black 随意格式化 Python
|
||||
======
|
||||
在我们覆盖 7 个 PyPI 库的系列文章中了解更多解决 Python 问题的信息。
|
||||
![OpenStack source code \(Python\) in VIM][1]
|
||||
|
||||
Python 是当今使用最多的[流行编程语言][2]之一,因为:它是开源的,它有广泛的用途(例如 Web 编程、业务应用、游戏、科学编程等等),它有一个充满活力和专注的社区支持它。这个社区是我们在 [Python Package Index][3](PyPI)中有如此庞大、多样化的软件包的原因,用以扩展和改进 Python 并解决不可避免的问题。
|
||||
|
||||
在本系列中,我们将介绍七个可以帮助你解决常见 Python 问题的 PyPI 库。在第一篇文章中,我们了解了 [Cython][4]。今天,我们将使用 **[Black] [5]** 这个代码格式化工具。
|
||||
|
||||
### Black
|
||||
|
||||
有时创意可能是一件美妙的事情。有时它只是一种痛苦。我喜欢创造性地解决难题,但我希望我的 Python 格式尽可能一致。没有人对使用“有趣”缩进的代码印象深刻。
|
||||
|
||||
但是比不一致的格式更糟糕的是除了检查格式之外什么都没有的代码审查。这对审查者来说很烦人,对于被审查者人来说甚至更烦人。当你的 linter 告诉你你的代码缩进不正确时,但没有提示_正确_的缩进量,这也会令人气愤。
|
||||
|
||||
使用 Black,它不会告诉你_要_做什么,它是一个优良、勤奋的机器人:它将为你修复代码。
|
||||
|
||||
要了解它如何工作的,请随意写一些非常不一致的内容,例如:
|
||||
|
||||
|
||||
```
|
||||
def add(a, b): return a+b
|
||||
|
||||
def mult(a, b):
|
||||
return \
|
||||
a * b
|
||||
```
|
||||
|
||||
Black 抱怨了么?并没有,它为你修复了!
|
||||
|
||||
|
||||
```
|
||||
$ black math
|
||||
reformatted math
|
||||
All done! ✨ 🍰 ✨
|
||||
1 file reformatted.
|
||||
$ cat math
|
||||
def add(a, b):
|
||||
return a + b
|
||||
|
||||
|
||||
def mult(a, b):
|
||||
return a * b
|
||||
```
|
||||
|
||||
Black 确实提供了错误而不是修复的选项,甚至还有输出 **diff** 编辑样式的选项。这些选项在持续集成 (CI) 系统中非常有用,可以在本地强制运行 Black。此外,如果 **diff** 输出被记录到 CI 输出中,你可以直接将其粘贴到 **patch** 中,以便在极少数情况下你需要修复输出,但无法本地安装 Black。
|
||||
|
||||
|
||||
```
|
||||
$ black --check --diff bad
|
||||
--- math 2019-04-09 17:24:22.747815 +0000
|
||||
+++ math 2019-04-09 17:26:04.269451 +0000
|
||||
@@ -1,7 +1,7 @@
|
||||
-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 math
|
||||
All done! 💥 💔 💥
|
||||
1 file would be reformatted.
|
||||
$ echo $?
|
||||
1
|
||||
```
|
||||
|
||||
在本系列的下一篇文章中,我们将介绍 **attrs** ,这是一个可以帮助你快速编写简洁,正确的代码的库。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/5/python-black
|
||||
|
||||
作者:[Moshe Zadka ][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez/users/moshez/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/openstack_python_vim_1.jpg?itok=lHQK5zpm (OpenStack source code (Python) in VIM)
|
||||
[2]: https://opensource.com/article/18/5/numbers-python-community-trends
|
||||
[3]: https://pypi.org/
|
||||
[4]: https://opensource.com/article/19/4/7-python-problems-solved-cython
|
||||
[5]: https://pypi.org/project/black/
|
Loading…
Reference in New Issue
Block a user