Pythonic

Pythonic code is a philosophy or an approach to writing python code that utilizes the full features of the language. My down fall is writing c code in oython.

Basic Requirements for my python scripts:

  • Comment header:

    • cite original author and link when appropriate

    • create a To Do (???) List and a Completed List in the comments

    • add references as needed

    • include link to this page

  • if appropriate, all scripts should be able to run as a module or as a main script

  • add argparse module (see mylog.py for an example)

  • help should explain how to use from both command line and as a module

  • the arguments from multiple scripts/modules mereg into a larger list of arguments

  • store working code in github

  • run pydoc. pydoc has stricter syntax checking than either python3 or pylint

    • Print docstrings with command (must include ./):

    • $ pydoc ./mylog.py

  • run pylint as last step (see below)

The Zen of Python:

To see the Zen of Python run the following:

$ python3

>>> import this

<zen happens>

Python rating 9.5/10 or higher:

Without much effort, I can run and fix the pylint results striving for a rating 9.5 out of 10. So, I think this is a minimum.

There are several tools to check if code is pythonic.


My goal is to find code that is not pythonic and correct it. I am not concerned with anal formatting concerns, such as, lines that are too long, or lines that have trailing whitespace or have whitespace around keywords. This is a waste of time.


pycodestyle mostly catches formatting issues. So, for me, pycodestyle isn't worthwhile

pylint:


I use pylint on my MacBook as follows:


First install it:


$ pip3 install pylint


Run it using:


$ pylint <filename> --disable=C0301,C0303,C0103

Your code has been rated at 9.62/10


I suppress the following with my reasoning:


C0301 - suppress: line too long. It's not controversial,; it's stupid. It's not the 1980's and no one is limited to 79/80 characters in VT100 terminal or MS- DOS; If someone wants to argue about this walk away


C0303 - suppress: extra spaces at end of line. this is a don't care


C0103 - suppress: I prefer to use camelCase rather than snake_name

  • I prefer all global names to start with upper case, and all local to start with lower case.

  • So, following these rules;

    • CONSTANTS are all upper case

    • Global variables, Classes and Class variables are CapWord

    • Local variables, instance variables and methods within a class are camelCase

Python Anti-Patterns:

Basically, an anti-pattern is how python code should not be written. pylint won't catch everything in the link above.

Subect-Verb-Object:

IMO, which might be wrong for you, but is right for me, python naming conventions should follow a Subject-Verb-Object order,


where subject ~= Class

A method starts with a verb and may include an instance variable: As an example:

  • Sam is a class, orange is an instance variable and a method is Sam.ateOrange()


References:

Pythonic Code:

PEP8:

  • https://www.python.org/dev/peps/pep-0008/