首頁  >  文章  >  後端開發  >  常見的 Python 函式庫有哪些?

常見的 Python 函式庫有哪些?

王林
王林轉載
2023-04-27 16:40:071054瀏覽

1. difflib

五个常用的 Python 库

difflib 是一個專注於比較資料集(尤其是字串)的 Python 模組。為了具體了解您可以使用此模組完成的幾件事,讓我們檢查一下它的一些最常見的函數。

SequenceMatcher

SequenceMatcher 是一個比較兩個字串並根據它們的相似性傳回資料的函數。透過使用 ratio(),我們將能夠根據比率/百分比來量化這種相似性。

語法:

SequenceMatcher(None, string1, string2)

下面這個簡單的範例展示了該函數的作用:

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,該函數傳回與作為參數傳入的字串最接近的匹配項。

語法:

get_close_matches(word, possibilities, result_limit, min_similarity)

下面解釋一下這些可能有些混亂的參數:

word 是函數將要查看的目標單字。

possibilities 是一個數組,其中包含函數將要尋找的匹配項並找到最接近的匹配項。

result_limit 是傳回結果數量的限制(可選)。

min_similarity 是兩個單字需要具有的最小相似度才能被函數視為回傳值(可選)。

以下是它的一個使用範例:

from difflib import get_close_matches
word = 'Tandrew'
possibilities = ['Andrew', 'Teresa', 'Kairu', 'Janderson', 'Drew']
print(get_close_matches(word, possibilities))
# Output: ['Andrew']

除此之外還有幾個是您可以查看的其他屬於Difflib 的方法和類別:unified_diff、Differ和diff_bytes

2. sched

sched 是一個有用的模組,它以跨平台工作的事件調度為中心,與Windows 上的任務調度程序等工具形成鮮明對比。大多數情況下,使用此模組時,都會使用 schedular 類別。

更常見的 time 模組通常與 sched 一起使用,因為它們都處理時間和調度的概念。

建立一個 schedular 實例:

schedular_name = sched.schedular(time.time, time.sleep)

可以從這個實例中呼叫各種方法。

呼叫 run() 時,調度程式中的事件/條目會依照順序被呼叫。在安排完事件後,此函數通常會出現在程式的最後。另外,搜尋公眾號Linux就該這樣學後台回覆“git書籍”,獲取一份驚喜禮包。

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()。

3. binaascii

binaascii 是用於在二進位和 ASCII 之間轉換的模組。

b2a_base64 是 binaascii 模組中的一種方法,它將 base64 資料轉換為二進位資料。下面是這個方法的一個例子:

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'

該段程式碼應該是不言自明的。簡單地說,它涉及編碼、轉換為 base64,以及使用 b2a_base64 方法將其轉換回二進位。

以下是一些屬於 binaascii 模組的函數:a2b_qp()、b2a_qp() 和 a2b_uu()。

4. tty

tty 是一個包含多個實用函數的模組,可用來處理 tty 裝置。以下是它的兩個函數:

setraw() 將其參數 (fd) 中檔案描述子的模式變更為 raw。

setcbreak() 將其參數 (fd) 中的檔案描述子的模式變更為 cbreak。

由於需要使用 termios 模組,因此此模組僅適用於 Unix,例如在上述兩個函數中指定第二個參數(when=termios.TCSAFLUSH)。

5. 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': []
# Weakrefs of 'num': []

輸出從輸出的函數傳回值我們可以看到它的作用。由於 num 沒有弱引用,因此 getweakrefs() 傳回的陣列為空。擴充:接私活兒

以下是一些其他與 weakref 模組相關的函數:ref()、proxy() 和 _remove_dead_weakref()。

以上是常見的 Python 函式庫有哪些?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:51cto.com。如有侵權,請聯絡admin@php.cn刪除