Lesser‑Known Python Standard Library Modules and Their Usage
This article introduces several under‑used Python standard‑library modules—difflib, sched, binascii, tty, and weakref—explaining their key functions, typical scenarios, and providing concrete code examples to help developers expand their toolkit.
Python's standard library includes more than 200 modules, many of which are rarely explored despite offering useful functionality across various domains such as data comparison, scheduling, binary‑ASCII conversion, terminal handling, and weak references.
1. difflib
The difflib module focuses on comparing data sets, especially strings. The most common class is SequenceMatcher , which can compare two strings and return a similarity ratio via its ratio() method.
<code>SequenceMatcher(None, string1, string2)</code>Example:
<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 matching strings from a list of possibilities.
<code>get_close_matches(word, possibilities, result_limit, min_similarity)</code>Example:
<code>from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'Kairu', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities)) # Output: ['Andrew']
</code>Other useful functions include unified_diff , Differ , and diff_bytes .
2. sched
The sched module provides a platform‑independent event scheduler. It is typically used together with the time module.
Creating a scheduler instance:
<code>schedular_name = sched.scheduler(time.time, time.sleep)</code>Key methods:
run() – Executes scheduled events in order.
enterabs() – Adds an event to the internal queue with an absolute execution time.
Example:
<code>import sched, time
def event_notification(event_name):
print(event_name + " has started")
my_schedular = sched.scheduler(time.time, time.sleep)
closing_ceremony = my_schedular.enterabs(time.time(), 1, event_notification, ("The Closing Ceremony",))
my_schedular.run() # Output: The Closing Ceremony has started
</code>Additional functions include cancel() , enter() , and empty() .
3. binascii
The binascii module converts between binary data and various ASCII‑encoded representations. One common method is b2a_base64 , which transforms base64‑encoded data back to binary.
<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. It is Unix‑only and often used together with the termios module.
setraw(fd) – Puts the file descriptor into raw mode.
setcbreak(fd) – Puts the file descriptor into cbreak mode.
5. weakref
The weakref module enables creation of weak references to objects, allowing the objects to be garbage‑collected when no strong references exist.
getweakrefcount(obj) – Returns the number of weak references to obj .
getweakrefs(obj) – Returns a list of all weak references to obj .
<code>import weakref
class Book:
def print_type(self):
print("Book")
lotr = Book
num = 1
rcount_lotr = str(weakref.getweakrefcount(lotr))
rcount_num = str(weakref.getweakrefcount(num))
rlist_lotr = str(weakref.getweakrefs(lotr))
rlist_num = str(weakref.getweakrefs(num))
print("number of weakrefs of 'lotr': " + rcount_lotr)
print("number of weakrefs of 'num': " + rcount_num)
print("Weakrefs of 'lotr': " + rlist_lotr)
print("Weakrefs of 'num': " + rlist_num)
# Output:
# number of weakrefs of 'lotr': 1
# number of weakrefs of 'num': 0
# Weakrefs of 'lotr': [<weakref at 0x...; to 'type' at ... (Book)>]
# Weakrefs of 'num': []
</code>Additional utilities include ref() , proxy() , and _remove_dead_weakref() .
Conclusion
Understanding and leveraging these lesser‑known standard‑library modules can greatly expand a Python developer's toolbox, enabling more concise, efficient, and maintainable code across a wide range of 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.