欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中可復(fù)用函數(shù)的6種實(shí)踐

 更新時(shí)間:2023年08月24日 09:17:48   作者:Python數(shù)據(jù)開(kāi)發(fā)  
為了實(shí)現(xiàn)可維護(hù)性,我們的Python函數(shù)應(yīng)該:小型、只做一項(xiàng)任務(wù);沒(méi)有重復(fù);有一個(gè)層次的抽象性;有一個(gè)描述性的名字和有少于四個(gè)參數(shù),下面我們就來(lái)看看這6個(gè)特性的實(shí)踐吧

前言

對(duì)于在一個(gè)有各種角色的團(tuán)隊(duì)中工作的數(shù)據(jù)科學(xué)家來(lái)說(shuō),編寫(xiě)干凈的代碼是一項(xiàng)必備的技能,因?yàn)椋?/p>

  • 清晰的代碼增強(qiáng)了可讀性,使團(tuán)隊(duì)成員更容易理解和貢獻(xiàn)于代碼庫(kù)。
  • 清晰的代碼提高了可維護(hù)性,簡(jiǎn)化了調(diào)試、修改和擴(kuò)展現(xiàn)有代碼等任務(wù)。

為了實(shí)現(xiàn)可維護(hù)性,我們的Python函數(shù)應(yīng)該:

  • 小型
  • 只做一項(xiàng)任務(wù)
  • 沒(méi)有重復(fù)
  • 有一個(gè)層次的抽象性
  • 有一個(gè)描述性的名字
  • 有少于四個(gè)參數(shù)

我們先來(lái)看看下面的 get_data 函數(shù)。

import xml.etree.ElementTree as ET
import zipfile
from pathlib import Path
import gdown
def get_data(
    url: str,
    zip_path: str,
    raw_train_path: str,
    raw_test_path: str,
    processed_train_path: str,
    processed_test_path: str,
):
    # Download data from Google Drive
    zip_path = "Twitter.zip"
    gdown.download(url, zip_path, quiet=False)
    # Unzip data
    with zipfile.ZipFile(zip_path, "r") as zip_ref:
        zip_ref.extractall(".")
    # Extract texts from files in the train directory
    t_train = []
    for file_path in Path(raw_train_path).glob("*.xml"):
        list_train_doc_1 = [r.text for r in ET.parse(file_path).getroot()[0]]
        train_doc_1 = " ".join(t for t in list_train_doc_1)
        t_train.append(train_doc_1)
    t_train_docs = " ".join(t_train)
    # Extract texts from files in the test directory
    t_test = []
    for file_path in Path(raw_test_path).glob("*.xml"):
        list_test_doc_1 = [r.text for r in ET.parse(file_path).getroot()[0]]
        test_doc_1 = " ".join(t for t in list_test_doc_1)
        t_test.append(test_doc_1)
    t_test_docs = " ".join(t_test)
    # Write processed data to a train file
    with open(processed_train_path, "w") as f:
        f.write(t_train_docs)
    # Write processed data to a test file
    with open(processed_test_path, "w") as f:
        f.write(t_test_docs)
if __name__ == "__main__":
    get_data(
        url="https://drive.google.com/uc?id=1jI1cmxqnwsmC-vbl8dNY6b4aNBtBbKy3",
        zip_path="Twitter.zip",
        raw_train_path="Data/train/en",
        raw_test_path="Data/test/en",
        processed_train_path="Data/train/en.txt",
        processed_test_path="Data/test/en.txt",
    )

盡管在這個(gè)函數(shù)中有許多注釋?zhuān)茈y理解這個(gè)函數(shù)的作用,因?yàn)椋?/p>

  • 該函數(shù)很長(zhǎng)。
  • 該函數(shù)試圖完成多項(xiàng)任務(wù)。
  • 函數(shù)內(nèi)的代碼處于不同的抽象層次。
  • 該函數(shù)有許多參數(shù)。
  • 有多個(gè)代碼重復(fù)。
  • 該函數(shù)缺少一個(gè)描述性的名稱(chēng)。

我們將通過(guò)使用文章開(kāi)頭提到的六種做法來(lái)重構(gòu)這段代碼。

小型

一個(gè)函數(shù)應(yīng)該保持很小,以提高其可讀性。理想情況下,一個(gè)函數(shù)的代碼不應(yīng)超過(guò)20行。此外,一個(gè)函數(shù)的縮進(jìn)程度不應(yīng)超過(guò)1或2。

import zipfile
import gdown
def get_raw_data(url: str, zip_path: str) -> None:
    gdown.download(url, zip_path, quiet=False)
    with zipfile.ZipFile(zip_path, "r") as zip_ref:
        zip_ref.extractall(".")

只做一個(gè)任務(wù)

函數(shù)應(yīng)該有一個(gè)單一的重點(diǎn),并執(zhí)行單一的任務(wù)。函數(shù)get_data試圖完成多項(xiàng)任務(wù),包括從Google Drive檢索數(shù)據(jù),執(zhí)行文本提取,并保存提取的文本。

因此,這個(gè)函數(shù)應(yīng)該被分成幾個(gè)小的函數(shù),如下圖所示:

def main(
    url: str,
    zip_path: str,
    raw_train_path: str,
    raw_test_path: str,
    processed_train_path: str,
    processed_test_path: str,
) -> None:
    get_raw_data(url, zip_path)
    t_train, t_test = get_train_test_docs(raw_train_path, raw_test_path)
    save_train_test_docs(processed_train_path, processed_test_path, t_train, t_test)

這些功能中的每一個(gè)都應(yīng)該有一個(gè)單一的目的:

def get_raw_data(url: str, zip_path: str) -> None:
    gdown.download(url, zip_path, quiet=False)
    with zipfile.ZipFile(zip_path, "r") as zip_ref:
        zip_ref.extractall(".")

函數(shù)get_raw_data只執(zhí)行一個(gè)動(dòng)作,那就是獲取原始數(shù)據(jù)。

重復(fù)性

我們應(yīng)該避免重復(fù),因?yàn)椋?/p>

  • 重復(fù)的代碼削弱了代碼的可讀性。
  • 重復(fù)的代碼使代碼修改更加復(fù)雜。如果需要修改,需要在多個(gè)地方進(jìn)行修改,增加了出錯(cuò)的可能性。

下面的代碼包含重復(fù)的內(nèi)容,用于檢索訓(xùn)練和測(cè)試數(shù)據(jù)的代碼幾乎是相同的。

from pathlib import Path  
 # 從train目錄下的文件中提取文本
t_train = []
for file_path in Path(raw_train_path).glob("*.xml"):
    list_train_doc_1 = [r.text for r in ET.parse(file_path).getroot()[0]]
    train_doc_1 = " ".join(t for t in list_train_doc_1)
    t_train.append(train_doc_1)
t_train_docs = " ".join(t_train)
# 從測(cè)試目錄的文件中提取文本
t_test = []
for file_path in Path(raw_test_path).glob("*.xml"):
    list_test_doc_1 = [r.text for r in ET.parse(file_path).getroot()[0]]
    test_doc_1 = " ".join(t for t in list_test_doc_1)
    t_test.append(test_doc_1)
t_test_docs = " ".join(t_test)

我們可以通過(guò)將重復(fù)的代碼合并到一個(gè)名為extract_texts_from_multiple_files的單一函數(shù)中來(lái)消除重復(fù),該函數(shù)從指定位置的多個(gè)文件中提取文本。

def extract_texts_from_multiple_files(folder_path) -> str:
    all_docs = []
    for file_path in Path(folder_path).glob("*.xml"):
        list_of_text_in_one_file = [r.text for r in ET.parse(file_path).getroot()[0]]
        text_in_one_file = " ".join(list_of_text_in_one_file)
        all_docs.append(text_in_one_file)
    return " ".join(all_docs)

現(xiàn)在你可以使用這個(gè)功能從不同的地方提取文本,而不需要重復(fù)編碼。

t_train = extract_texts_from_multiple_files(raw_train_path)
t_test  = extract_texts_from_multiple_files(raw_test_path)

一個(gè)層次的抽象

抽象水平是指一個(gè)系統(tǒng)的復(fù)雜程度。高層次指的是對(duì)系統(tǒng)更概括的看法,而低層次指的是系統(tǒng)更具體的方面。

在一個(gè)代碼段內(nèi)保持相同的抽象水平是一個(gè)很好的做法,使代碼更容易理解。

以下函數(shù)證明了這一點(diǎn):

def extract_texts_from_multiple_files(folder_path) -> str:
    all_docs = []
    for file_path in Path(folder_path).glob("*.xml"):
        list_of_text_in_one_file = [r.text for r in ET.parse(file_path).getroot()[0]]
        text_in_one_file = " ".join(list_of_text_in_one_file)
        all_docs.append(text_in_one_file)
    return " ".join(all_docs)

該函數(shù)本身處于較高層次,但 for 循環(huán)內(nèi)的代碼涉及與XML解析、文本提取和字符串操作有關(guān)的較低層次的操作。

為了解決這種抽象層次的混合,我們可以將低層次的操作封裝在extract_texts_from_each_file函數(shù)中:

def extract_texts_from_multiple_files(folder_path: str) -> str:
    all_docs = []
    for file_path in Path(folder_path).glob("*.xml"):
        text_in_one_file = extract_texts_from_each_file(file_path)
        all_docs.append(text_in_one_file)
    return " ".join(all_docs)
def extract_texts_from_each_file(file_path: str) -> str:
    list_of_text_in_one_file = [r.text for r in ET.parse(file_path).getroot()[0]]
    return " ".join(list_of_text_in_one_file)

這為文本提取過(guò)程引入了更高層次的抽象,使代碼更具可讀性。

描述性的名稱(chēng)

一個(gè)函數(shù)的名字應(yīng)該有足夠的描述性,使用戶(hù)不用閱讀代碼就能理解其目的。長(zhǎng)一點(diǎn)的、描述性的名字比模糊的名字要好。例如,命名一個(gè)函數(shù)get_texts就不如命名為extract_texts_from_multiple_files來(lái)得清楚。

然而,如果一個(gè)函數(shù)的名字變得太長(zhǎng),比如retrieve_data_extract_text_and_save_data,這說(shuō)明這個(gè)函數(shù)可能做了太多的事情,應(yīng)該拆分成更小的函數(shù)。

少于四個(gè)參數(shù)

隨著函數(shù)參數(shù)數(shù)量的增加,跟蹤眾多參數(shù)之間的順序、目的和關(guān)系變得更加復(fù)雜。這使得開(kāi)發(fā)人員難以理解和使用該函數(shù)。

def main(
    url: str,
    zip_path: str,
    raw_train_path: str,
    raw_test_path: str,
    processed_train_path: str,
    processed_test_path: str,
) -> None:
    get_raw_data(url, zip_path)
    t_train, t_test = get_train_test_docs(raw_train_path, raw_test_path)
    save_train_test_docs(processed_train_path, processed_test_path, t_train, t_test)

為了提高代碼的可讀性,你可以用數(shù)據(jù)類(lèi)或Pydantic模型將多個(gè)相關(guān)參數(shù)封裝在一個(gè)數(shù)據(jù)結(jié)構(gòu)中。

from pydantic import BaseModel
class RawLocation(BaseModel):
    url: str
    zip_path: str
    path_train: str
    path_test: str
class ProcessedLocation(BaseModel):
    path_train: str
    path_test: str
def main(raw_location: RawLocation, processed_location: ProcessedLocation) -> None:
    get_raw_data(raw_location)
    t_train, t_test = get_train_test_docs(raw_location)
    save_train_test_docs(processed_location, t_train, t_test)

如何寫(xiě)這樣的函數(shù)

在編寫(xiě)Python函數(shù)時(shí),你不需要記住所有這些最佳實(shí)踐。衡量一個(gè)Python函數(shù)質(zhì)量的一個(gè)很好的指標(biāo)是它的可測(cè)試性。如果一個(gè)函數(shù)可以很容易地被測(cè)試,這表明該函數(shù)是模塊化的,執(zhí)行單一的任務(wù),并且沒(méi)有重復(fù)的代碼。

def save_data(processed_path: str, processed_data: str) -> None:
    with open(processed_path, "w") as f:
        f.write(processed_data)
def test_save_data(tmp_path):
    processed_path = tmp_path / "processed_data.txt"
    processed_data = "Sample processed data"
    save_data(processed_path, processed_data)
    assert processed_path.exists()
    assert processed_path.read_text() == processed_data

到此這篇關(guān)于Python中可復(fù)用函數(shù)的6種實(shí)踐的文章就介紹到這了,更多相關(guān)Python可復(fù)用函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python裝飾器簡(jiǎn)單用法實(shí)例小結(jié)

    Python裝飾器簡(jiǎn)單用法實(shí)例小結(jié)

    這篇文章主要介紹了Python裝飾器簡(jiǎn)單用法,結(jié)合實(shí)例形式總結(jié)分析了Python裝飾器的基本功能、簡(jiǎn)單用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2018-12-12
  • Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式

    Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式

    今天小編就為大家分享一篇Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • django框架單表操作之增刪改實(shí)例分析

    django框架單表操作之增刪改實(shí)例分析

    這篇文章主要介紹了django框架單表操作之增刪改,結(jié)合實(shí)例形式分析了Django框架前臺(tái)數(shù)據(jù)操作提交與后臺(tái)處理相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-12-12
  • python裝飾器常見(jiàn)使用方法分析

    python裝飾器常見(jiàn)使用方法分析

    這篇文章主要介紹了python裝飾器常見(jiàn)使用方法,結(jié)合實(shí)例形式分析了Python裝飾器的功能及三種常見(jiàn)的裝飾模式使用技巧,需要的朋友可以參考下
    2019-06-06
  • python中內(nèi)置庫(kù)os與sys模塊的詳細(xì)介紹

    python中內(nèi)置庫(kù)os與sys模塊的詳細(xì)介紹

    這篇文章主要介紹了python中內(nèi)置庫(kù)os與sys模塊的詳細(xì)介紹,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下
    2022-08-08
  • numpy多項(xiàng)式擬合函數(shù)polyfit的使用方法代碼

    numpy多項(xiàng)式擬合函數(shù)polyfit的使用方法代碼

    這篇文章主要給大家介紹了關(guān)于numpy多項(xiàng)式擬合函數(shù)polyfit的使用方法,np.polyfit是Numpy庫(kù)中的一個(gè)函數(shù),用于在最小二乘意義下擬合多項(xiàng)式曲線(xiàn)到數(shù)據(jù)點(diǎn)集,需要的朋友可以參考下
    2024-01-01
  • python中join()方法介紹

    python中join()方法介紹

    Python join() 方法用于將序列中的元素以指定的字符連接生成一個(gè)新的字符串。這篇文章主要介紹了python中join()方法,需要的朋友可以參考下
    2018-10-10
  • Pytorch 實(shí)現(xiàn)自定義參數(shù)層的例子

    Pytorch 實(shí)現(xiàn)自定義參數(shù)層的例子

    今天小編就為大家發(fā)信息一篇Pytorch 實(shí)現(xiàn)自定義參數(shù)層的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-08-08
  • 刪除pandas中產(chǎn)生Unnamed:0列的操作

    刪除pandas中產(chǎn)生Unnamed:0列的操作

    這篇文章主要介紹了刪除pandas中產(chǎn)生Unnamed:0列的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • Python中使用PyMySQL模塊的方法詳解

    Python中使用PyMySQL模塊的方法詳解

    Python中的pymysql模塊是用于連接MySQL數(shù)據(jù)庫(kù)的一個(gè)第三方庫(kù),它提供了一套API,使得Python程序員能夠方便地執(zhí)行SQL語(yǔ)句、操作數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于Python中使用PyMySQL模塊的相關(guān)資料,需要的朋友可以參考下
    2024-08-08

最新評(píng)論