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.Įnter Black. Instead of telling you what to do, Black is a good, industrious robot: it will fix your code for you. It is annoying to the reviewer-and even more annoying to the person whose code is reviewed. Nobody has ever been impressed by code that uses "interesting" indentation.īut even worse than inconsistent formatting is a code review that consists of nothing but formatting nits. I enjoy solving hard problems creatively, but I want my Python formatted as consistently as possible. Sometimes creativity can be a wonderful thing. In the first article, we learned about Cython today, we'll examine the Black code formatter. In this series, we'll look at seven PyPI libraries that can help you solve common Python problems. This community is the reason we have such a large, diverse range of software packages available in the Python Package Index (PyPI) to extend and improve Python and solve the inevitable glitches that crop up. By using it, you agree to cede control over minutiae of hand-formatting. Style configuration options are deliberately limited and rarely added. Black is the uncompromising Python code formatter. Also be aware that Black requires a Python v3.6. Similar language constructs are formatted with similar rules. Virtual environments are supported by way of the official Python extensions python.pythonPath setting. Python is one of the most popular programming languages 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. Black aims for consistency, generality, readability and reducing git diffs. Result: blackd is more than 10x faster! □ Neovim integrationĮditor integration for Neovim can be done using a general purpose language server. Using blackd-client ❯ hyperfine 'cat subclean/core/parser.py | blackd-client ' Time (mean ± σ ) : 23.7 ms ± 3.7 ms Range (min … max ) : 19.2 ms … 35.7 ms 84 runs Normal black -fast ❯ hyperfine 'cat subclean/core/parser.py | black -fast - ' Time (mean ± σ ) : 296.8 ms ± 41.3 ms Range (min … max ) : 260.7 ms … 403.6 ms 10 runs Output is formatted using Black ✨ Benchmark comparison Or as a launchd service using Homebrew on macOS (preferred) sudo brew services start black Usage Rename to blackd-client and put it somewhere on your PATHĪlternatively if you have Rust toolchain installed: cargo install blackd-client.Or using Homebrew on macOS (preferred) brew install black If you're using Black (or writing Python code in general) I recommend you to check it out! Install Luckily there's blackd, which is a small HTTP server that keeps the Black process running in the background so that it can be called directly without the lenghty startup time.īlackd-client is a simple helper that provides a single executable to communicate with Black, mainly for me to learn Rust. However it can be quite slow when using an editor integration with format on save, since the process is cold-started every time you call it. Tiny HTTP client for the Black ( blackd) Python code formatterīlack is a brilliant, opinionated formatter for Python.
0 Comments
Leave a Reply. |