Tool to convert Python code to be PEP8 compliant

后端 未结 6 1315
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 03:57

I know there are tools which validate whether your Python code is compliant with PEP8, for example there is both an online service and a python module.

However, I c

6条回答
  •  伪装坚强ぢ
    2020-12-02 04:45

    @Andy Hayden gave good overview of autopep8. In addition to that there is one more package called pep8ify which also does the same thing.

    However both packages can remove only lint errors but they cannot format code.

    little = more[3:   5]
    

    Above code remains same after pep8ifying also. But the code doesn't look good yet. You can use formatters like yapf, which will format the code even if the code is PEP8 compliant. Above code will be formatted to

    little = more[3:5]
    

    Some times this even destroys Your manual formatting. For example

    BAZ = {
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12]
    }
    

    will be converted to

    BAZ = {[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]}
    

    But You can tell it to ignore some parts.

    BAZ = {
       [1, 2, 3, 4],
       [5, 6, 7, 8],
       [9, 10, 11, 12]
    }  # yapf: disable
    

    Taken from my old blog post: Automatically PEP8 & Format Your Python Code!

提交回复
热议问题