Lesser‑Known Python Standard Library Modules and How to Use Them
This article introduces several lesser‑known Python standard‑library modules—including difflib, sched, binascii, tty, and weakref—explains their key functions, provides practical code examples, and highlights how they can simplify tasks such as string comparison, event scheduling, binary‑ASCII conversion, terminal handling, and weak referencing.
The Python standard library contains over 200 modules, many of which are under‑utilized despite their powerful capabilities for tasks ranging from data comparison to low‑level system interactions.
1. difflib
The difflib module focuses on comparing datasets, especially strings. Its most common class, SequenceMatcher , can quantify similarity between two strings using the ratio() method.
Example:
<code>SequenceMatcher(None, string1, string2)</code>Typical usage:
<code>from difflib import SequenceMatcher
phrase1 = "Tandrew loves Trees."
phrase2 = "Tandrew loves to mount Trees."
similarity = SequenceMatcher(None, phrase1, phrase2)
print(similarity.ratio()) # Output: 0.8163265306122449
</code>The module also provides get_close_matches , which returns the closest matches to a target word from a list of possibilities.
<code>from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'Kairu', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities)) # Output: ['Andrew']
</code>Additional useful functions include unified_diff , Differ , and diff_bytes .
2. sched
The sched module provides a platform‑independent event scheduler, typically used together with the time module. A scheduler instance can schedule functions to run at specific times.
<code>import sched, time
my_scheduler = sched.scheduler(time.time, time.sleep)
my_scheduler.enterabs(time.time(), 1, lambda: print('Event started'))
my_scheduler.run()
</code>Key methods are enterabs() (add an event with an absolute timestamp) and run() (execute scheduled events). The module also offers cancel() , enter() , and empty() .
3. binascii
The binascii module converts between binary data and ASCII representations. A common use is base64 encoding/decoding.
<code>import base64, binascii
msg = "Tandrew"
encoded = msg.encode('ascii')
base64_msg = base64.b64encode(encoded)
decode = binascii.a2b_base64(base64_msg)
print(decode) # Output: b'Tandrew'
</code>Other functions include a2b_qp() , b2a_qp() , and a2b_uu() .
4. tty
The tty module offers utilities for working with terminal devices, such as setraw() and setcbreak() . It requires the termios module and is only available on Unix‑like systems.
5. weakref
The weakref module enables creation of weak references to objects, which do not prevent garbage collection. Useful functions include getweakrefcount() (number of weak references) and getweakrefs() (list of weak references).
<code>import weakref
class Book:
def print_type(self):
print("Book")
lotr = Book
num = 1
rcount_lotr = weakref.getweakrefcount(lotr)
rcount_num = weakref.getweakrefcount(num)
print("number of weakrefs of 'lotr':", rcount_lotr)
print("number of weakrefs of 'num':", rcount_num)
</code>Additional functions are ref() , proxy() , and _remove_dead_weakref() .
Key Takeaways
Understanding and leveraging these Python standard‑library modules can greatly expand your toolkit, reduce reliance on third‑party packages, and improve code efficiency across a wide range of programming tasks.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.