In this lesson, you will learn,

  • what is Black
  • how to install black
  • Black's main configuration options.
You can find the scripts and code for this course within the Automating Python Code Quality repo.

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


Black can be run against a file or directory, like so:

$ black .
$ black dir/
$ black 

For example, let’s say we have the following Python script:

$ cat 001_formatters/
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
Members Only Content
Want to Learn Network Automation?
Join our membership to access:
  • Full deep-dive course library (inc. Batfish, pyATS, Netmiko)
  • Code repositories inc. full course code, scripts and examples
  • 24x7 multi-vendor labs (Arista, Cisco, Juniper)
  • Private online community
  • Tech demos and sessions

Join Now ➜
Close You've successfully subscribed to Packet Coders.
Close Success! Your account is fully activated, you now have access to all content.
Close Welcome back! You've successfully signed in.
Close Nearly there! To activate your account, please click the link in the email we just sent you.