Less known bits of the Python Standard Library

Vitor Pereira
Nov 2 · 3 min read

It’s not unusual for me to search the web for how to do something in Python and end up using a third party library only to find out later that the standard library included a module that did more or less what I wanted. Here are a couple of less known modules that might come in handy.

textwrap

This module has some functions for easily wrapping and indenting plain text. Its useful when you’re one of those weirdos that likes to wrap everything you print to the terminal at 80 characters. E.g.

>>> import textwrap
>>> text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.'
>>> for line in textwrap.wrap(text, 50):
... print(line)
...
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur.

pprint

I’m surprised a lot of people don’t know this one. While developing in Python, you always end up doing a lot of print debugging. When dealing with more complicated data structures, like nested dictionaries, print’s output becomes unruly and that’s when pprint comes in:

>>> from pprint import pprint
>>> data = {
... 'name': 'Michael Audrey Meyers',
... 'birth_date': 'October 19, 1957',
... 'relatives' : [
... 'Donald Meyers',
... 'Edith Meyers',
... 'Judith Meyers',
... ],
... }
>>> print(data)
{'name': 'Michael Audrey Meyers', 'birth_date': 'October 19, 1957', 'relatives': ['Donald Meyers', 'Edith Meyers', 'Judith Meyers']}
>>> pprint(data)
{'birth_date': 'October 19, 1957',
'name': 'Michael Audrey Meyers',
'relatives': ['Donald Meyers', 'Edith Meyers', 'Judith Meyers']}

enum

Python has had type hinting for a while now — you knew that, right? Big companies are type hinting their code and so should you because the power of types can even make shitty languages less shitty. Before types, Python had already started moving in this direction with the enum module. It allows you to define a type as a set of predefined constants much like in other languages. Here’s an example from the documentation:

>>> from enum import Enum
>>> class Color(Enum):
... RED = 1
... GREEN = 2
... BLUE = 3
...
>>> print(Color.RED)
Color.RED
>>> print(Color.RED.name)
RED
>>> print(Color.RED.value)
1

shelve

When I want persist some data, without much fuss, I pickle whichever objects I want and write them to storage. If portability is an issue, I serialize the objects using json instead. An even simple (and less portable) alternative is to use shelve. You just instantiate a Shelf and use it like a dictionary. Dbm takes care of writing and reading your data to and from the disk. E.g.

>>> import shelve
>>> with shelve.open('default.db') as shelf:
... shelf['first_name'] = 'Vitor'
... shelf['last_name'] = 'Pereira'
...
>>> with shelve.open('default.db') as shelf:
... print(shelf['first_name'], shelf['last_name'])
...
Vitor Pereira

(Read the bit on writebackin the documentation if you plan on making heavier use of this module)

email and smtplib

That’s right, Python includes an RFC compliant parser and generator of email messages. I found out about this module while reading Gmail’s documentation. It also includes an SMTP client. Combining these two means you have a full fledged email client at your disposal. Here’s an example script from the documentation:

import smtplib
from email.message import EmailMessage
textfile = 'stored_email.txt'# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())

msg['Subject'] = f'The contents of {textfile}'
msg['From'] = 'roger@hmail.com'
msg['To'] = 'tobias@imail.com'

# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

winreg

(Windows exclusive!) Microsoft’s operating system includes an all purpose global key-value trash bag that software can use to store data it hates called the Windows Registry. This module’s API is pretty much a light wrapper around the native APIs so it’s not particularly pleasant to use, but hey, at least it’s an alternative to those ill-defined .REG files.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade