📜 ⬆️ ⬇️

Formatting Python Code

Introduction


Python, more precisely its most famous representative of CPython, is not very designed for any quick calculations. In other words, his performance is not so good . But the speed of development and readability is excellent.

On readability and will be discussed, but rather how to increase it.

Formatting problems


There is no perfect formatting code. For each language, it is worth adjusting to the generally accepted code design rules. But what to say, if among the newbies of C ++ there are still wars about putting brackets on the next line or not.
For python, the basic formatting problem is “C style”. It is not uncommon for the language in question to come from C-like languages, but it is common for them to write with the characters ") (;".
Symbols are not the only problem, there is also the problem of the redundancy of writing constructions. Python, unlike Java, is less verbose, and it takes a lot of time to get used to beginners.
These are the two main problems that occur most often.

Standards and guidelines for registration


If you can use different approaches to increase the speed of code execution, although these approaches are very individual, then there is a straight slyle guide for text formatting - this is pep8. Further I will call it “standard”.
You can read about the standard here , in Russian here.
Pep8 is very extensive and allows the programmer to write REALLY readable code.
')
It includes:

In general, covers a variety of code formatting rules. However, it is worth noting that not all python programmers follow these rules.
Large companies, such as Google, have their own recommendations for writing python-code, you can read them here and here .
Very interesting reading, I recommend.

Automating Formatting


If you look at how many rules in pep8, then you can sit down for refactoring for a long time. That's just it is lazy, and when writing a new code, there will be some errors of the rules. To do this, consider how you can simplify life.

pep8


In order to have an idea of ​​how many design errors in the code, you should use the pep8 utility.
She has a sufficient list of parameters that allows recursively browse all files in folders for compliance with the pep8 standard.
The utility output is something like this:

$ pep8 --first optparse.py optparse.py:69:11: E401 multiple imports on one line optparse.py:77:1: E302 expected 2 blank lines, found 1 optparse.py:88:5: E301 expected 1 blank line, found 0 optparse.py:222:34: W602 deprecated form of raising exception optparse.py:347:31: E211 whitespace before '(' optparse.py:357:17: E201 whitespace after '{' optparse.py:472:29: E221 multiple spaces before operator optparse.py:544:21: W601 .has_key() is deprecated, use 'in' 

According to it, you can clearly understand: where is the error and what happened.

autopep8


Standard errors often repeat from file to file. And there is a strong desire to automate the fix. In this case, autopep8 enters the arena .
Like pep8, it can identify errors on its own and correct them. A list of correctable formatting errors can be found here.
The use of autopep8 itself is extremely simple and may look like this:

 $ autopep8 ./ --recursive --in-place -a 

After executing this command, the utility will recursively go through the subfolders and begin to correct errors in the files themselves.

autoflake


You can go further and take autoflake as a weapon. This utility helps to remove unused imports and variables.
Used like this:

 $ autoflake --in-place --remove-all-unused-imports --remove-unused-variables -r ./ 

This will recursively clean the files in the directory.

unify


The final, final point in code editing is strings. Someone likes to write them in single apostrophes, someone in double ones. That's just for this, there are recommendations, as well as a utility that allows you to automatically align - unify
Using:

 $ unify --in-place -r ./src/ 

As elsewhere, the utility will do its dirty work recursively for the files in the folder.

docformatter


All the time we are talking about the code itself, but the comments have never been spoken about. The time has come - docformatter . This utility helps to bring your docstring under the agreement of PEP 257 . The agreement prescribes proper documentation.
Using the utility is no more difficult than the previous ones:

 $ docformatter --in-place example.py 


And all together you can?


The utilities are described above, they can be launched by adding some bash script under the magic name clean.bash and running. And you can go the other way and use a wrapper over these utilities - pyformat

findings


Python code is easy to read, however, there are ways to make noodles from readable code.
This article has been voiced some problems of code design, as well as ways to find these problems. Several utilities that allow you to automatically remove some flaws in code design were considered.
It is necessary to voice out loud the following recommendation when writing code that is universal for any language: a more important design rule than similar pep8 documents is the constancy of style. Chose what style you will write the program in, in the same and write all the code.

If readers are interested, in the next article I will describe how to automatically look for errors in the code.

Source: https://habr.com/ru/post/251531/


All Articles