Python 标准库有超过 200 个模块,程序员可以在他们的程序中导入和使用。虽然普通程序员对其中许多模块都有经验,但很可能有一些模块他们仍然没有注意到。
我发现其中许多模块包含在各个领域都非常有用的功能。比较数据集、使用其他函数以及使用音频只是 Python 可以自动帮助你做的一些事情。
因此,我编制了一份你可能不知道的 Python 模块的候选清单,并对它们进行了适度详细的解释,以便你将来能够理解和使用它们。
所有这些模块都有不同的功能和类。我包含了几个鲜为人知的函数和类,因此即使你听说过这些模块,也可能不知道它们的某些方面和用途
difflib模块
difflib是一个 Python 模块,专注于比较数据集,尤其是字符串。为了具体了解你可以使用此模块完成的几件事,让我们检查一下它的一些最常见的功能。
- SequenceMatcher是一个比较两个字符串并根据它们的相似性返回数据的函数。通过使用ratio(),我们将能够根据比率/百分比来量化这种相似性。
# SequenceMatcher(None, string1, string2)
from difflib import SequenceMatcher
phrase1 = "Hello World."
phrase2 = "Hello World."
similarity = SequenceMatcher(None, phrase1, phrase2)
print(similarity.ratio())
# Output: 1.0
2 .接下来是get_close_matches函数,它返回与作为参数传入的字符串最接近的匹配项
# get_close_matches(word, possibilities, result_limit, min_similarity)
from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'JackeyLove', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities))
# Output: ['Andrew']
word是函数将要查看的目标词。possibilities是一个包含函数将查找并找到最接近匹配项的匹配项的数组。result_limit是返回结果数量的限制(可选)。min_similarity是两个单词需要具有的最小相似度才能被函数视为返回值(可选)
以下是Difflib你可以查看的其他一些方法和类:
- unified_diff
- Differ
- diff_bytes
sched模块
sched是一个有用的模块,以跨平台工作的事件调度为中心,与 Windows 上的任务调度程序等工具形成鲜明对比。大多数情况下,在使用这个模块时,schedular都会使用该类。
创建schedular实例:
schedular_name = sched.schedular (time.time, time.sleep)
可以从schedular实例调用各种方法。
- 当run()被调用时,调度中的事件/条目被按顺序调用。此功能通常在活动安排好后出现在节目的最后。
- enterabs()是一个函数,它本质上将事件添加到调度到其内部队列中。它按以下顺序接收几个参数:
- 事件执行的时间
- 活动优先级
- 事件本身(一个函数)
- 事件函数的参数
- 事件的关键字参数字典
下面是一个示例,说明如何同时使用这两个函数:
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
扩展模块可能用途的其他功能sched包括:
- cancel()
- enter()
- empty()
binaascii模块
binaascii是一个用于在二进制和 ASCII 之间转换的模块。
b2a_base64是binaascii模块中的方法之一,它将base64数据转换为二进制。下面是这个方法的一个例子:
import base64
import binascii
msg = "Jack"
encoded = msg.encode('ascii')
base64_msg = base64.b64encode(encoded)
decode = binascii.a2b_base64(base64_msg)
print(decode)
# Output: b'Jack'
简单来说就是编码转base64,再转回binary的b2a_base64方法。
以下是属于该binaascii模块的一些其他功能:
- a2b_qp()
- b2a_qp()
- a2b_uu()
tty模块
tty是一个包含多个实用功能的模块,可用于处理 tty 设备。以下是它的两个功能:
- setraw()将其参数 ( fd ) 中文件描述符的模式更改为 raw。
- setcbreak()将其参数 ( fd ) 中文件描述符的模式更改为 cbreak。
由于需要使用该termios模块,该模块仅适用于 Unix,例如在上述两个函数 ( when=termios.TCSAFLUSH) 中指定第二个参数。
weakref模块
weakref是一个模块,用于在 Python 中创建对对象的弱引用。
弱引用是不保护给定对象不被垃圾收集的引用。
以下是与该模块相关的几个功能:
- getweakrefcount()接受一个对象作为参数并返回引用该对象的弱引用的数量。
- getweakrefs()接受一个对象并返回一个数组,其中包含引用该对象的所有弱引用。
用法weakref及其功能示例:
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': []
输出提供了这些函数的返回值是什么样子的想法。由于num没有弱引用,因此返回的数组getweakrefs()为空。
以下是与该weakref模块相关的一些其他功能:
- ref()
- proxy()
- _remove_dead_weakref()
总结
- Difflib是用于比较数据集,尤其是字符串的模块。例如,SequenceMatcher比较两个字符串并根据它们的相似性返回数据。
- sched是与模块一起使用的有用工具,用于使用实例time安排事件(以函数的形式) 。schedular例如,enterabs()将一个事件添加到调度程序的内部队列中,该队列将在run()调用函数时运行。
- binaascii在二进制和 ASCII 之间转换以编码和解码数据。b2a_base64是binaascii模块中的方法之一,它将base64数据转换为二进制。
- tty需要使用termios模块,并处理tty设备。它仅适用于 Unix。
- weakref适用于弱引用。它的函数可以返回对象的弱引用,查找对象的弱引用数量等。函数的一个例子是getweakrefs(),它接受一个对象并返回一个包含所有弱引用的数组。
这些功能中的每一个都有其各自的用途,每一个都有不同程度的有用性。了解尽可能多的 Python 函数和模块非常重要,以便保持稳定的工具库,你可以在编写代码时快速使用这些工具。
无论你的编程专业知识水平如何,你都应该不断学习。多投入一点时间可以为你带来更多价值,并为你节省更多未来时间。
如果你发现我的任何文章有帮助或有用,麻烦点赞或者转发。 谢谢!