Black
Why do I need it?
Life is too short to tidy up code by hand. Black is an opinionated python formatter. By using it, you agree to cede control over minutiae of hand-formatting.
install
pip install black
Why would I use it?
Blackened code looks the same regardless of the project you’re reading. Formatting becomes transparent after a while and you can focus on the content instead.
Show me more!
Given this code
#!/usr/bin/env python
dxdy = (
(-1, -2),(0, -2), (1, -2),
(-2, -1),
(-1, -1),(0, -1),
(1, -1), (2, -1),
(-2, 0 ),
(-1, 0 ),
(1, 0),(2, 0),
(-2, 1),
(-1, 1),(0, 1),
(1,1),
(2,1),
(-1,2),(0, 2),(1, 2), (0, 0),)
ny=71
nx=71
coords = [17, 0]
for dx, dy in dxdy:
neighbour_coords = coords[0] + dx, coords[1] + dy
if not (0 <= neighbour_coords[0] < nx
and 0 <= neighbour_coords[1] < ny):
print(f'off {coords[0] + dx} {coords[1] + dy}')
else:
print(f"on {coords[0] + dx} {coords[1] + dy}")
running
python -m black test.py
or black test.py
results in
#!/usr/bin/env python
dxdy = (
(-1, -2),
(0, -2),
(1, -2),
(-2, -1),
(-1, -1),
(0, -1),
(1, -1),
(2, -1),
(-2, 0),
(-1, 0),
(1, 0),
(2, 0),
(-2, 1),
(-1, 1),
(0, 1),
(1, 1),
(2, 1),
(-1, 2),
(0, 2),
(1, 2),
(0, 0),
)
ny = 71
nx = 71
coords = [17, 0]
for dx, dy in dxdy:
neighbour_coords = coords[0] + dx, coords[1] + dy
if not (0 <= neighbour_coords[0] < nx and 0 <= neighbour_coords[1] < ny):
print(f"off {coords[0] + dx} {coords[1] + dy}")
else:
print(f"on {coords[0] + dx} {coords[1] + dy}")
Now what?
There are a few command line arguments (for example I like to use -l 120
to make the columns a little larger ) read the documents from
here
Perhaps you can add a git hook to run black by default on your project when you commit.