This lesson is part of the Automating Python Code Quality course.
In this lesson, you will learn,
- what is Black
- how to install black
- Black's main configuration options.
What is Black?
black has become the de facto code formatter for Python which will automatically format you Python code based on Black’s code style guide, which aligns to the majority of the PEP8 rules. Or, in other words (as per the Black documentation):
Black is the uncompromising Python code formatter ... You will save time and mental energy for more important matters.
How to Install Black
Installation of black is performed by running the following:
poetry add -D black
Usage
Black can be run against a file or directory, like so:
$ black .
$ black dir/
$ black script.py
For example, let’s say we have the following Python script:
$ cat 001_formatters/001_black.py
vlans = [
{"vlan_name": "SALES", "vlan_id": 100},
{"vlan_name": "HR", "vlan_id": 101},{"vlan_name": "FINANCE", "vlan_id": 102}
]
def is_vlan_present( vlan_id ):
for vlan in vlans:
if vlan_id in vlan.values():
return True
return False