You are on page 1of 8

9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

How I Write Industry-Standard Python Code


The tools and best practices I use when I write my Python code

Whether you’re a newbie to Python or a seasoned veteran, the concept of code that is ‘industry
standard’ is elusive and often tedious to achieve — that being said, it is definitely possible! In this
post, I will detail the tools and best practices I use when I write my Python code — let’s dive right
in!

Full list and references at the bottom!

Photo by Chris Ried on Unsplash

Tool #1: The Python Debugger


Queue the age-old joke about debugging code with print statements…

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 1/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

Whenever I ever get into a pickle with one of my projects and I just can’t seem to shake a series of
errors, The Python Debugger module from the Python standard libraries is my go-to tool — it offers
an interactive source code debugger that can be embedded into scripts similar to print or logging
statements.

Example:

The tool can be used to test new functionalities before adding them into scripts in the exact
environments that they will be added to — pretty powerful, huh?

Link: https://docs.python.org/3/library/pdb.html

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 2/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

Best practice #1: Function strings and Docstrings


More information from my past post here!

A docstring is most conventionally defined under a function or class definition using triple double
quotation marks and is broken down into 3 clearly defined sections:

Functionality description.

Breakdown of any parameters.

Breakdown of the return, if any.

The aim of any docstring is to explain the part of the code it is referring to so that anyone new
viewing it will be able to understand, in a general sense, what the section does.

See the following example, but be aware that a later practice I will introduce will slightly alter this
format!

def filter_systems(data, threshold, keep_duplicate=False)


"""

A simple filter that removes systems that are beneath the user-defined x-
value threshold.
Parameters

----------

`data` : Pandas DataFrame


A pandas dataframe containing the systems from the xyz dataset. Must
include columns 'x' and 'y'. All timestamp columns must be timezone
aware.

`threshold` : float

for the x-value to be applied to the filter. Must
A float threshold
be between 1 and 10.
`keep_duplicates` :

Boolean

Defaults to False. A boolean value determining whether duplicate


systems should be removed in the filter.

Returns
-------

`filtered_data` : Pandas DataFrame


A pandas dataframe containing the

filtered systems from the xyz dataset.


Must include columns 'x' and 'y'. All timestamp columns must be timezone

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 3/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

aware.
"""

Tool #2: Notepad++


Just hear me out on this one…

Now everyone will have their favourite IDE or text editor to use, but I opt for Notepad++… why you
ask? Because, it forces me to actually remember functions and their parameters when using pip-
installable packages instead of relying on something like VS Code’s ‘auto-correct’ aide.

Of course, this can probably be achieved in most if not all text editors and IDEs but this is just my
personal preference! I do have some settings configured on my Notepad++ in order to uphold my
coding standards, these are as follows:

A vertical line down the files at exactly 100 characters — this will ensure that my lines of code
won’t be too long.

Display line endings — this is fairly self explanatory, but it is a great help with file formatting.

Below you can see my Notepad++ layout:

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 4/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

Best practice #2: Type hints


More information from my past post here!

Type hints are one of those features that doesn’t feels very Pythonic, however they can add so much
value to your code — especially when creating public packages and libraries!

Using the library typing, type hints allow programmers to add type annotations to parameters of
functions. These are especially powerful when coupled with substantial function strings as it ensures
the user of your code can understand the functions even if the source code is abstracted from them.

Remember that function string from earlier? Let’s revisit it and add the type hints:

def filter_systems(data : pd.Dataframe, threshold : float, keep_duplicate


: bool = False)
"""

A simple filter that removes systems that are beneath the user-defined x-

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 5/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

value threshold.
Parameters
----------

`data` : Pandas DataFrame


A pandas dataframe containing the systems from the xyz dataset. Must
include columns 'x' and 'y'. All timestamp columns must be timezone
aware.

`threshold` : float

for the x-value to be applied to the
A float threshold filter. Must
be between 1 and 10.
`keep_duplicates` :

Boolean

Defaults to False. A boolean value determining whether duplicate


systems should be removed in the filter.

Returns
-------

`filtered_data` : Pandas DataFrame


A pandas dataframe containing the

filtered systems from the xyz dataset.


Must include columns 'x' and 'y'. All timestamp columns must be timezone
aware.
"""

Et viola, just a few extra characters can go a long way to level up your function definitions and code
overall!

Tool #3: Pylint


Or a human embodiment of Pylint!

When it comes to re-viewing a script I have recently written, my go-to tool to help me uphold best
practices is Pylint. Pylint is a static code analyser and is one of, if not the most powerful tool in my
arsenal when I write my Python code.

Pylint goes as deep as pointing out necessary imports, lines of code that are too long as well as poor
variable names. To invoke Pylint on a script, the following command should be ran:

pylint <path_to_script.py>

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 6/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

Here’s an example of what it could yield; this is taken from an old project:

My favourite thing about Pylint is the rating /10 given at the end of the output — this almost
gamifies the process of perfecting code and allows users to set the standard of their code on a
numeric scale!

Best practice #3: Banish those print() statements


Not just for debugging purposes either…

We talked about not using print() statements to debug, but I believe they shouldn’t be used at all —
instead I opt to use logging statements. Again, from the standard Python library, the logging module
offers a more lightweight and customisable way to output to the terminal.

Benefits of logging:

You can differentiate your logging based on severity i.e., errors, warnings etc.

If your code is used alongside other Python modules, any print statements won’t clearly indicate
where they are coming from.

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 7/8
9/28/22, 9:09 AM How I Write Industry-Standard Python Code | by Ethan Jones | Sep, 2022 | Python in Plain English

A simple example could look like so:

>>> import logging


>>> logging.warning('Watch out!')


WARNING:root:Watch out!

Conclusion
And there we have it, 3 tools and 3 best practices that I use when writing my industry-standard
Python code!

There are many more that can be used to level up your code, but these are what I consider the best.

As usual thanks for reading, take care

~ Ethan

References:

Python Debugger

Function strings blog post

Notepad++ download

Type hints LinkedIn post

Pylint

Logging vs print

https://python.plainenglish.io/how-i-write-industry-standard-python-code-2dbc9435e48 8/8

You might also like