I would like to ignore a specific multi-line code by black
python formatter. Particularly, this is used for np.array
or matrix construction which turned ugly when formatted. Below is the example.
JavaScript
x
11
11
1
np.array(
2
[
3
[1, 0, 0, 0],
4
[0, -1, 0, 0],
5
[0, 0, 1, 0],
6
[0, 0, 0, -1],
7
]
8
)
9
# Will be formatted to
10
np.array([[1, 0, 0, 0], [0, -1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])
11
I found this issue in black
github, but that only works for inline command, which is not what I have here.
Is there anything I can do to achieve this for a multi-line code?
Advertisement
Answer
You can use #fmt: on/off
as explained in the issue linked. In your case it would look like:
JavaScript
1
11
11
1
# fmt: off
2
np.array(
3
[
4
[1, 0, 0, 0],
5
[0, -1, 0, 0],
6
[0, 0, 1, 0],
7
[0, 0, 0, -1],
8
]
9
)
10
# fmt: on
11
# fmt: off
disables formatting for all following lines until formatting is activated again with # fmt: on