Table of contents
foreword
The Python standard library has over 200 modules that programmers can import and use in their programs. While the average programmer has some experience with many of these modules, there are likely some useful ones they still haven't noticed.
I've found that many of these modules contain functions that are useful in various fields. Comparing datasets, collaborating with other functions, and audio processing can all be automated using only Python.
1. difflib
difflib
is a Python module focused on comparing datasets , especially strings. To get a concrete idea of a few things you can do with this module, let's examine some of its most common functions.
SequenceMatcher
SequenceMatcher
is a function that compares two strings and returns data based on their similarity. By using ratio()
, we will be able to quantify this similarity in terms of ratios/percentages .
grammar:
SequenceMatcher(None, string1, string2)
The following simple example shows the function in action:
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
get_close_matches
Next get_close_matches
, the function returns the closest match to the string passed in as a parameter.
grammar:
get_close_matches(word, possibilities, result_limit, min_similarity)
Here's an explanation of these potentially confusing parameters:
-
word
is the target word the function will look at. -
possibilities
is an array containing the matches the function will look for and find the closest match. -
result_limit
is the limit on the number of returned results (optional). -
min_similarity
is the minimum similarity two words need to have to be considered a return value by the function (optional).
Here is an example of its use:
from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'Kairu', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities))
# Output: ['Andrew']
There are a few Difflib
other methods and classes that you can look at that belong to: unified_diff
, Differ
and diff_bytes
2. sched
sched
is a useful module centered around event scheduling that works across platforms, in stark contrast to tools like Task Scheduler on Windows. Most of the time when using this module, you will use schedular
classes.
The more common time
modules are often sched
used together with , since they both deal with the concepts of time and scheduling.
Create an schedular
instance:
schedular_name = sched.schedular(time.time, time.sleep)
Various methods can be called from this instance.
-
When called
run()
, the events/entries in the scheduler are called in order. This function usually appears at the end of the program, after scheduling events. In addition, search for the official account Linux and learn how to reply to "git books" in the background to get a surprise gift package. -
enterabs()
is a function that essentially adds the event to the scheduler's internal queue. It receives several parameters in the following order:-
time the event was executed
-
activity priority
-
the event itself (a function)
-
Parameters of the event function
-
A dictionary of keyword arguments for the event
-
Here's an example of how to use these two functions together:
import sched
import 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
There are also several sched
functions that extend the usage of the module: cancel()
, , enter()
and empty()
.
3. Binascii
binaascii
is a module for converting between binary and ASCII.
b2a_base64
is binaascii
a method in the module that converts base64 data to binary data. Here is an example of this method:
import base64
import binascii
msg = "Tandrew"
encoded = msg.encode('ascii')
base64_msg = base64.b64encode(encoded)
decode = binascii.a2b_base64(base64_msg)
print(decode)
# Output: b'Tandrew'
This code should be self-explanatory. Simply put, it involves encoding, converting to base64, and using b2a_base64
methods to convert it back to binary.
Here are binaascii
some other functions that belong to the module: a2b_qp()
, , b2a_qp()
and a2b_uu()
.
4. tty
tty
is a module containing several utility functions for working with tty
devices. Here are its two functions:
-
setraw()
Changes the mode of the file descriptor in its argument (fd) to raw. -
setcbreak()
Changes the mode of the file descriptor in its argument (fd) to cbreak.
This module is only available on Unix due to the need to use termios
modules, such as specifying the second parameter ( ) in the above two functions when=termios.TCSAFLUSH
.
5. weakref
weakref
is a module for creating weak references to objects in Python .
Weak references are references that do not protect a given object from being collected by garbage collection mechanisms.
The following are the two functions associated with this module:
-
getweakrefcount()
Takes an object as a parameter and returns the number of weak references that refer to that object. -
getweakrefs()
Takes an object and returns an array containing all weak references that refer to that object.
weakref
Example usage of its functions:
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 0x10b978a90; to 'type' at #0x7fb7755069f0 (Book)>]
# Weakrefs of 'num': []
Output From the output function return value we can see what it does. Since num
there are no weak references, getweakrefs()
the returned array is empty.
Here are weakref
some other functions related to the module: ref()
, , proxy()
and _remove_dead_weakref()
.
Summarize
Each of these functions has its own purpose, and each has varying degrees of usefulness. It's important to know as many Python functions and modules as possible in order to maintain a stable library of tools that you can use quickly as you write code.