Consistency and style

All of the examples in the book were prepared using the black tool to provide consistent formatting. Some additional manual adjustments were made to keep code within the narrow sizes of printed material.

A common alternative to using black is to use pylint to identify formatting problems. These can then be corrected. In addition to detailed analysis of code quality, the pylint tool offers a numeric quality score. For this book, some pylint rules needed to be disabled. For example, the modules often have imports that are not in the preferred order; some modules also have imports that are relevant to doctest examples, and appear to be unused; some examples use global variables; and some class definitions are mere skeletons without appropriate method definitions. 

Using pylint to locate potential problems is essential. It's often helpful to silence pylint warnings. In the following example, we need to silence a pylint warning about the test_list variable name being invalid as a global variable:

# pylint: disable=invalid-name
test_list = """
>>> f = [1, 1, 2, 3]
>>> f += [f[-1] + f[-2]]
>>> f
[1, 1, 2, 3, 5]
"""

if __name__ == "__main__":
import doctest
__test__ = {name: value
for name, value in locals().items()
if name.startswith("test_")}
doctest.testmod(verbose=False)

Besides helping enforce a consistent style, the pylint warnings are helpful for identifying spelling mistakes and a list of common errors. For example, the instance variable is commonly self. An accidental spelling error of sefl will be found by pylint

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset