diff options
Diffstat (limited to 'docs/advanced.rst')
-rw-r--r-- | docs/advanced.rst | 55 |
1 files changed, 36 insertions, 19 deletions
diff --git a/docs/advanced.rst b/docs/advanced.rst index 2bce2e0..fd3cf3e 100644 --- a/docs/advanced.rst +++ b/docs/advanced.rst @@ -1,4 +1,4 @@ -.. currentmodule:: pep8 +.. currentmodule:: pycodestyle ============== Advanced usage @@ -8,64 +8,81 @@ Advanced usage Automated tests --------------- -You can also execute `pep8` tests from Python code. For example, this +You can also execute ``pycodestyle`` tests from Python code. For example, this can be highly useful for automated testing of coding style conformance in your project:: import unittest - import pep8 + import pycodestyle 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']) + def test_conformance(self): + """Test that we conform to PEP-8.""" + style = pycodestyle.StyleGuide(quiet=True) + result = style.check_files(['file1.py', 'file2.py']) self.assertEqual(result.total_errors, 0, "Found code style errors (and warnings).") -If you are using `nosetests` for running tests, remove `quiet=True` +If you are using ``nosetests`` for running tests, remove ``quiet=True`` since Nose suppresses stdout. There's also a shortcut for checking a single file:: - import pep8 + import pycodestyle - fchecker = pep8.Checker('testsuite/E27.py', show_source=True) + fchecker = pycodestyle.Checker('testsuite/E27.py', show_source=True) file_errors = fchecker.check_all() print("Found %s errors (and warnings)" % file_errors) +Configuring tests +----------------- + +You can configure automated ``pycodestyle`` tests in a variety of ways. + +For example, you can pass in a path to a configuration file that ``pycodestyle`` +should use:: + + import pycodestyle + + style = pycodestyle.StyleGuide(config_file='/path/to/tox.ini') + +You can also set specific options explicitly:: + + style = pycodestyle.StyleGuide(ignore=['E501']) + + Skip file header ---------------- Another example is related to the `feature request #143 -<https://github.com/jcrocholl/pep8/issues/143>`_: skip a number of lines +<https://github.com/pycqa/pycodestyle/issues/143>`_: skip a number of lines at the beginning and the end of a file. This use case is easy to implement through a custom wrapper for the PEP 8 library:: #!python - import pep8 + import pycodestyle LINES_SLICE = slice(14, -20) - class PEP8(pep8.StyleGuide): - """This subclass of pep8.StyleGuide will skip the first and last lines + class StyleGuide(pycodestyle.StyleGuide): + """This subclass of pycodestyle.StyleGuide will skip the first and last lines of each file.""" def input_file(self, filename, lines=None, expected=None, line_offset=0): if lines is None: assert line_offset == 0 line_offset = LINES_SLICE.start or 0 - lines = pep8.readlines(filename)[LINES_SLICE] - return super(PEP8, self).input_file( + lines = pycodestyle.readlines(filename)[LINES_SLICE] + return super(StyleGuide, self).input_file( filename, lines=lines, expected=expected, line_offset=line_offset) if __name__ == '__main__': - pep8style = PEP8(parse_argv=True, config_file=True) - report = pep8style.check_files() + style = StyleGuide(parse_argv=True, config_file=True) + report = style.check_files() if report.total_errors: raise SystemExit(1) @@ -74,4 +91,4 @@ and 20 lines at the end. If there's no line to skip at the end, it could be changed with ``LINES_SLICE = slice(14, None)`` for example. You can save it in a file and use it with the same options as the -original ``pep8``. +original ``pycodestyle``. |