From e438a80ec88bc7b0fec1d267d3032f74da74aecf Mon Sep 17 00:00:00 2001 From: Moritz Schillinger Date: Thu, 8 Oct 2020 23:56:52 +0200 Subject: [PATCH] Add auto-formatting section --- docs/writing/style.rst | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/docs/writing/style.rst b/docs/writing/style.rst index 91f089374..d40033afa 100644 --- a/docs/writing/style.rst +++ b/docs/writing/style.rst @@ -496,6 +496,14 @@ Then run it on a file or series of files to get a report of any violations. optparse.py:472:29: E221 multiple spaces before operator optparse.py:544:21: W601 .has_key() is deprecated, use 'in' +Auto-Formatting +~~~~~~~~~~~~~~~ + +There are several auto-formatting tools that can reformat your code, +in order to comply with PEP 8. + +**autopep8** + The program `autopep8 `_ can be used to automatically reformat code in the PEP 8 style. Install the program with: @@ -513,6 +521,49 @@ Excluding the ``--in-place`` flag will cause the program to output the modified code directly to the console for review. The ``--aggressive`` flag will perform more substantial changes and can be applied multiple times for greater effect. +**yapf** + +While autopep8 focuses on solving the PEP 8 violations, `yapf `_ +tries to improve the format of your code aside from complying with PEP 8. +This formatter aims at providing as good looking code as a programmer who +writes PEP 8 compliant code. +It gets installed with: + +.. code-block:: console + + $ pip install yapf + +Run the auto-formatting of a file with: + +.. code-block:: console + + $ yapf --in-place optparse.py + +Similar to autopep8, running the command without the ``--in-place`` flag will +output the diff for review before applying the changes. + +**black** + +The auto-formatter `black `_ offers an +opinionated and deterministic reformatting of your code base. +Its main focus lies in providing a uniform code style without the need of +configuration throughout its users. Hence, users of black are able to forget +about formatting altogether. Also, due to the deterministic approach minimal +git diffs with only the relevant changes are guaranteed. You can install the +tool as follows: + +.. code-block:: console + + $ pip install black + +A python file can be formatted with: + +.. code-block:: console + + $ black optparse.py + +Adding the ``--diff`` flag provides the code modification for review without +direct application. *********** Conventions