首页 > 解决方案 > Formatting numpy arrays with black

问题描述

I am using vscode along with black as a formatter. I don't like how it formats numpy arrays and how it is effected by line length, for example

F = np.array([[1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1]])

versus

F = np.array(
        [
            [1, 1, 0.5, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 1, 0.5, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 1, 1, 0.5],
        ]
    )

For readability I prefer the latter, but black formats the latter style when the former style would violate the line length.

Is there a way to customize black to prefer formatting the latter style without changing the line length parameter.

I am open to other formatters, I am just using black because it interfaces nice with flake8. My line length is set to 88.

标签: pythonnumpyvisual-studio-codeformatting

解决方案


黑色只有两个配置选项:

  1. 线长
  2. 是否将'引号替换为"

如果您想要这种级别的配置,您可能需要查看 autopep8 或 yapf。


推荐阅读