你可能不知道的五個(gè)實(shí)用的 Python 模塊

Python 標(biāo)準(zhǔn)庫有超過 200 個(gè)模塊,程序員可以在他們的程序中導(dǎo)入和使用。雖然普通程序員對(duì)其中許多模塊都有一些經(jīng)驗(yàn),但很可能有一些好用的模塊他們?nèi)匀粵]有注意到。
我發(fā)現(xiàn)其中許多模塊都包含了在各個(gè)領(lǐng)域都非常有用的函數(shù)。比較數(shù)據(jù)集、協(xié)作其他函數(shù)以及音頻處理等都可以僅使用 Python 就可以自動(dòng)完成。
因此,我編制了一份您可能不知道的 Python 模塊的候選清單,并對(duì)這幾個(gè)模塊進(jìn)行了適當(dāng)?shù)慕忉專员隳趯砝斫夂褪褂盟鼈儭?/p>
所有這些模塊都有不同的函數(shù)和類。我包含了幾個(gè)鮮為人知的函數(shù)和類,因此即使您聽說過這些模塊,也可能不知道它們的某些方面和用途。
1. difflib
??difflib?? 是一個(gè)專注于比較數(shù)據(jù)集(尤其是字符串)的 Python 模塊。為了具體了解您可以使用此模塊完成的幾件事,讓我們檢查一下它的一些最常見的函數(shù)。
SequenceMatcher
??SequenceMatcher?? 是一個(gè)比較兩個(gè)字符串并根據(jù)它們的相似性返回?cái)?shù)據(jù)的函數(shù)。通過使用 ??ratio()??,我們將能夠根據(jù)比率/百分比來量化這種相似性。
語法:
SequenceMatcher(None, string1, string2)
下面這個(gè)個(gè)簡(jiǎn)單的例子展示了該函數(shù)的作用:
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
接下來是 ??get_close_matches??,該函數(shù)返回與作為參數(shù)傳入的字符串最接近的匹配項(xiàng)。
語法:
get_close_matches(word, possibilities, result_limit, min_similarity)
下面解釋一下這些可能有些混亂的參數(shù):
- ?
?word?? 是函數(shù)將要查看的目標(biāo)單詞。 - ?
?possibilities?? 是一個(gè)數(shù)組,其中包含函數(shù)將要查找的匹配項(xiàng)并找到最接近的匹配項(xiàng)。 - ?
?result_limit?? 是返回結(jié)果數(shù)量的限制(可選)。 - ?
?min_similarity?? 是兩個(gè)單詞需要具有的最小相似度才能被函數(shù)視為返回值(可選)。 
下面是它的一個(gè)使用示例:
from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'Kairu', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities))
# Output: ['Andrew']
除此之外還有幾個(gè)是您可以查看的屬于 ??Difflib?? 的其他一些方法和類:??unified_diff??、??Differ?? 和 ??diff_bytes??
2. sched
??sched?? 是一個(gè)有用的模塊,它以跨平臺(tái)工作的事件調(diào)度為中心,與 Windows 上的任務(wù)調(diào)度程序等工具形成鮮明對(duì)比。大多數(shù)情況下,使用此模塊時(shí),都會(huì)使用 ??schedular?? 類。
更常見的 ??time?? 模塊通常與 ??sched?? 一起使用,因?yàn)樗鼈兌继幚頃r(shí)間和調(diào)度的概念。
創(chuàng)建一個(gè) ??schedular?? 實(shí)例:
schedular_name = sched.schedular(time.time, time.sleep)
可以從這個(gè)實(shí)例中調(diào)用各種方法。
- 事件執(zhí)行的時(shí)間
 - 活動(dòng)優(yōu)先級(jí)
 - 事件本身(一個(gè)函數(shù))
 - 事件函數(shù)的參數(shù)
 - 事件的關(guān)鍵字參數(shù)字典
 
- 調(diào)用 ?
?run()?? 時(shí),調(diào)度程序中的事件/條目會(huì)按照順序被調(diào)用。在安排完事件后,此函數(shù)通常出現(xiàn)在程序的最后。 - ?
?enterabs()?? 是一個(gè)函數(shù),它本質(zhì)上將事件添加到調(diào)度程序的內(nèi)部隊(duì)列中。它按以下順序接收幾個(gè)參數(shù): 
下面是一個(gè)示例,說明如何一起使用這兩個(gè)函數(shù):
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
還有幾個(gè)擴(kuò)展 ??sched?? 模塊用途的函數(shù):??cancel()??、??enter()?? 和 ??empty()??。
3. binaascii
??binaascii?? 是一個(gè)用于在二進(jìn)制和 ASCII 之間轉(zhuǎn)換的模塊。
??b2a_base64?? 是 ??binaascii?? 模塊中的一種方法,它將 base64 數(shù)據(jù)轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)。下面是這個(gè)方法的一個(gè)例子:
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'
該段代碼應(yīng)該是不言自明的。簡(jiǎn)單地說,它涉及編碼、轉(zhuǎn)換為 base64,以及使用 ??b2a_base64?? 方法將其轉(zhuǎn)換回二進(jìn)制。
以下是屬于 ??binaascii?? 模塊的其他一些函數(shù):??a2b_qp()??、??b2a_qp()?? 和 ??a2b_uu()??。
4. tty
??tty?? 是一個(gè)包含多個(gè)實(shí)用函數(shù)的模塊,可用于處理 ??tty?? 設(shè)備。以下是它的兩個(gè)函數(shù):
setraw()將其參數(shù) (fd) 中文件描述符的模式更改為 raw。setcbreak()將其參數(shù) (fd) 中的文件描述符的模式更改為 cbreak。
由于需要使用 ??termios?? 模塊,該模塊僅適用于 Unix,例如在上述兩個(gè)函數(shù)中指定第二個(gè)參數(shù)(??when=termios.TCSAFLUSH??)。
5. weakref
??weakref?? 是一個(gè)用于在 Python 中創(chuàng)建對(duì)對(duì)象的弱引用的模塊。
弱引用是不保護(hù)給定對(duì)象不被垃圾回收機(jī)制收集的引用。
以下是與該模塊相關(guān)的兩個(gè)函數(shù):
getweakrefcount()接受一個(gè)對(duì)象作為參數(shù),并返回引用該對(duì)象的弱引用的數(shù)量。getweakrefs()接受一個(gè)對(duì)象并返回一個(gè)數(shù)組,其中包含引用該對(duì)象的所有弱引用。
??weakref?? 及其函數(shù)的使用示例:
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': []
輸出從輸出的函數(shù)返回值我們可以看到它的作用。由于 ??num?? 沒有弱引用,因此 ??getweakrefs()?? 返回的數(shù)組為空。
以下是與 ??weakref?? 模塊相關(guān)的一些其他函數(shù):??ref()??、??proxy()?? 和 ??_remove_dead_weakref()??。
回顧
Difflib是一個(gè)用于比較數(shù)據(jù)集,尤其是字符串的模塊。例如,SequenceMatcher可以比較兩個(gè)字符串并根據(jù)它們的相似性返回?cái)?shù)據(jù)。sched是與time模塊一起使用的有用工具,用于使用schedular實(shí)例安排事件(以函數(shù)的形式)。例如,enterabs()將一個(gè)事件添加到調(diào)度程序的內(nèi)部隊(duì)列中,該隊(duì)列將在調(diào)用run()函數(shù)時(shí)運(yùn)行。
??binaascii?? 可在二進(jìn)制和 ASCII 之間轉(zhuǎn)換以編碼和解碼數(shù)據(jù)。??b2a_base64?? 是 ??binaascii?? 模塊中的一種方法,它將 base64 數(shù)據(jù)轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)。
??tty?? 模塊需要配合使用 ??termios?? 模塊,并處理 tty 設(shè)備。它僅適用于 Unix。
??weakref?? 用于弱引用。它的函數(shù)可以返回對(duì)象的弱引用,查找對(duì)象的弱引用數(shù)量等。其中非常使用的函數(shù)之一是 ??getweakrefs()??,它接受一個(gè)對(duì)象并返回一個(gè)該對(duì)象包含的所有弱引用的數(shù)組。
要點(diǎn)
這些函數(shù)中的每一個(gè)都有其各自的用途,每一個(gè)都有不同程度的有用性。了解盡可能多的 Python 函數(shù)和模塊非常重要,以便保持穩(wěn)定的工具庫,您可以在編寫代碼時(shí)快速使用。
無論您的編程專業(yè)知識(shí)水平如何,您都應(yīng)該不斷學(xué)習(xí)。多投入一點(diǎn)時(shí)間可以為您帶來更多價(jià)值,并為您節(jié)省更多未來時(shí)間。
 
2019-11-25 14:05:47
 
 
 














 
 







 