Python news 2020

python-code-quality

What exactly is code quality <https://en.wikipedia.org/wiki/Software_quality> ? How do we measure it? How do we improve code quality and clean up our Python code?

Code quality generally refers to how functional and maintainable your code is. Code is considered to be of high quality when:

  • It serves its purpose

  • It’s behavior can be tested

  • It follows a consistent style

  • It’s understandable

  • It doesn’t contain security vulnerabilities

  • It’s documented well

  • It’s easy to maintain

Since we already addressed the first two points in the Testing in Python and Modern Test-Driven Development in Python posts, the focus of this post is on points three through six.

This post looks how to improve the quality of your Python code with linters, code formatters, and security vulnerability scanners.

Python 3.9.0 (2020-10-05) is out !

Stop using await in a Python forloop

Forcefully sequentialise

Take a look at the code below. How long do you think it takes to finish ?

async def ioheavyfn(idx):
    await asyncio.sleep(5)
    return "bar"

for i in range(0, 5):
    await ioheavyfn(i)
1
2
3
4
5
6

If you guessed 25 seconds you were right. But where is the benefit of asyncio then? Where is the parallelism? You might ask.

The problem with await in a forloop is that it makes the parallelism (described above) impossible and executes the code essentially sequentially. It does have some benefit if you’re running a highly concurrent web application with Tornado/Twisted or something similar because it can switch to another request processing but there is a much better way to do this.

Asyncio.gather

And here comes asyncio.gather to save the day.

The above example is very easy to rewrite like:

g = asyncio.gather(*ioheavyfn(i) for i in range(0, 5)])
await g
1
2

It takes a little over 5 seconds to finish (5.0056 to be precise). So we’re back to properly executing the IO heavy operations “parallel” and we can indicate somewhere in the code that we need this information moving forward.

pycln : A formatter for finding and removing unused import statements)

pyinspect 2020-10-04 find functions when you can’t remember their name