1 .. currentmodule:: pep8
11 You can also execute `pep8` tests from Python code. For example, this
12 can be highly useful for automated testing of coding style conformance
19 class TestCodeFormat(unittest.TestCase):
21 def test_pep8_conformance(self):
22 """Test that we conform to PEP8."""
23 pep8style = pep8.StyleGuide(quiet=True)
24 result = pep8style.check_files(['file1.py', 'file2.py'])
25 self.assertEqual(result.total_errors, 0,
26 "Found code style errors (and warnings).")
28 If you are using `nosetests` for running tests, remove `quiet=True`
29 since Nose suppresses stdout.
31 There's also a shortcut for checking a single file::
35 fchecker = pep8.Checker('testsuite/E27.py', show_source=True)
36 file_errors = fchecker.check_all()
38 print("Found %s errors (and warnings)" % file_errors)
44 Another example is related to the `feature request #143
45 <https://github.com/jcrocholl/pep8/issues/143>`_: skip a number of lines
46 at the beginning and the end of a file. This use case is easy to implement
47 through a custom wrapper for the PEP 8 library::
52 LINES_SLICE = slice(14, -20)
54 class PEP8(pep8.StyleGuide):
55 """This subclass of pep8.StyleGuide will skip the first and last lines
58 def input_file(self, filename, lines=None, expected=None, line_offset=0):
60 assert line_offset == 0
61 line_offset = LINES_SLICE.start or 0
62 lines = pep8.readlines(filename)[LINES_SLICE]
63 return super(PEP8, self).input_file(
64 filename, lines=lines, expected=expected, line_offset=line_offset)
66 if __name__ == '__main__':
67 pep8style = PEP8(parse_argv=True, config_file=True)
68 report = pep8style.check_files()
69 if report.total_errors:
72 This module declares a lines' window which skips 14 lines at the beginning
73 and 20 lines at the end. If there's no line to skip at the end, it could be
74 changed with ``LINES_SLICE = slice(14, None)`` for example.
76 You can save it in a file and use it with the same options as the