【问题标题】:Running PEP8 checks from Python从 Python 运行 PEP8 检查
【发布时间】:2015-03-20 09:18:55
【问题描述】:

我无法从 Python 脚本运行 PEP8 检查。

我不想显式运行 pep8.exe,因为我想自动执行此检查,而 pep8 可执行文件可以位于不同平台上的不同位置。

【问题讨论】:

    标签: python pep8


    【解决方案1】:

    PEP8 advanced usage 涵盖在 Python 脚本中使用 pep8。

    举个例子:

    import unittest
    import pep8
    
    
    class TestCodeFormat(unittest.TestCase):
    
        def test_pep8_conformance(self):
            """Test that we conform to PEP8."""
            pep8style = pep8.StyleGuide(quiet=True)
            result = pep8style.check_files(['file1.py', 'file2.py'])
            self.assertEqual(result.total_errors, 0,
                             "Found code style errors (and warnings).")
    

    【讨论】:

    • 我看过了。但我仍有疑问。 1) 如何打印所有发现的违规行为? 2) 如何查看一个目录下的所有Python文件?
    • 1) 移除 quiet=True。 2) 使用 os.walk 建立一个目录中所有 python 文件的列表并将其传递给 check_files()。
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 2011-02-26
    • 1970-01-01
    • 2018-10-15
    • 2018-11-15
    • 1970-01-01
    • 2012-05-21
    • 2011-10-16
    相关资源
    最近更新 更多