搜尋
首頁後端開發Python教學邁向輕鬆的 Python 設定檔版本 2

介紹

在上一篇文章中,我們成功建立了一個模擬屬性資料類別來管理應用程式的配置值。 然而,它基本上只是一個模板,我們必須為每次使用重新實作。 在我的工作的第二版中,我成功地實作了一個可重複使用的類別。

  • 它需要屬性定義上的一對裝飾器。
  • 另外,實作需要一個Sections 資料類型,其中包含每個配置節的一個Section 條目。
  • 每個部分都需要一個 ConfigurationNameValue 條目清單

班級代表

下面的類別圖顯示了基本的可重用類別以及開發人員使用此功能所需的資料結構。

Towards Effortless Python Configuration Files Version 2

子類別配置屬性

開發人員透過以下方式子類化配置屬性來開始這個過程。

BASE_FILE_NAME: str = 'config.ini'
MODULE_NAME:    str = 'version2properties'

class ConfigurationPropertiesVersion2(ConfigurationProperties, metaclass=SingletonV3):

    def __init__(self):
        self.logger: Logger = getLogger(LOGGER_NAME)

        super().__init__(baseFileName=BASE_FILE_NAME, moduleName=MODULE_NAME, sections=CONFIGURATION_SECTIONS)

        self._configParser.optionxform = self._toStr    # type: ignore

        self._loadConfiguration()

對 super 的呼叫會建立設定檔的完全限定路徑。 程式碼遵循 XDG 基本目錄規範的子集。 程式碼首先嘗試 XDG_CONFIG_HOME,然後嘗試 HOME,如果兩者都失敗,最後使用目前目錄。 在第 13 行中,開發人員呼叫受保護的方法來準備配置解析器。 此外,該呼叫將

  • 確保設定檔存在,如果不存在,則建立一個空檔案
  • 建立缺失的部分
  • 在部分建立缺失的鍵

先前的功能會產生大量可重複使用的程式碼和引導設定檔。 此外,我們將該類別設為單例,以降低在我們的應用程式程式碼庫中實例化該類別時的創建成本。

定義部分

注意 super 呼叫中的參數部分。 部分的定義如下:

from codeallybasic.ConfigurationProperties import Sections

CONFIGURATION_SECTIONS: Sections = Sections(
    {
        SectionName('General'):  SECTION_GENERAL,
        SectionName('Database'): SECTION_DATABASE,
    }
)

上面是一個字典,其中鍵是節名稱,值是節。

定義部分

Section 只是 ConfigurationNameValue 條目的清單。 ConfigurationNameValue 是一個具有 2 個值的資料類,即 PropertyName 與其預設值。這是我們的部分。

from codeallybasic.ConfigurationProperties import Section
from codeallybasic.ConfigurationProperties import ConfigurationNameValue
from codeallybasic.ConfigurationProperties import PropertyName

SECTION_GENERAL: Section = Section(
    [
        ConfigurationNameValue(name=PropertyName('debug'),                           defaultValue='False'),
        ConfigurationNameValue(name=PropertyName('logLevel'),                     defaultValue='Info'),
        ConfigurationNameValue(name=PropertyName('phoneyEnumByValue'),  defaultValue=DEFAULT_PHONEY_ENUM_BY_VALUE.value),
        ConfigurationNameValue(name=PropertyName('impostorEnumByName'), defaultValue=DEFAULT_IMPOSTOR_ENUM_BY_NAME.name),
    ]
)

SECTION_DATABASE: Section = Section(
    [
        ConfigurationNameValue(name=PropertyName('dbName'), defaultValue='example_db'),
        ConfigurationNameValue(name=PropertyName('dbHost'), defaultValue='localhost'),
        ConfigurationNameValue(name=PropertyName('dbPort'), defaultValue='5432'),
    ]
)

請注意,我指定了兩個新的枚舉屬性。 我們希望堅持的一個是價值,另一個是名字。

這裡有枚舉定義。

class PhoneyEnumByValue(Enum):
    TheWanderer = 'The Wanderer'
    Mentiroso   = 'Mentiroso'
    FakeBrenda  = 'Faker Extraordinaire'
    NotSet          = 'Not Set'

    @classmethod
    def deSerialize(cls, value: str) -> 'PhoneyEnumByValue':

    @classmethod
    def deSerialize(cls, value: str) -> 'PhoneyEnumByValue':

        match value:
            case PhoneyEnumByValue.TheWanderer.value:
                phoneyEnum: PhoneyEnumByValue = PhoneyEnumByValue.TheWanderer
            case PhoneyEnumByValue.Mentiroso.value:
                phoneyEnum = PhoneyEnumByValue.Mentiroso
            case PhoneyEnumByValue.FakeBrenda.value:
                phoneyEnum = PhoneyEnumByValue.FakeBrenda
            case _:
                raise Exception('Unknown PhoneyEnumByValue')

        return phoneyEnum


class ImpostorEnumByName(Enum):
    Low       = 0.1
    Medium = 0.5
    High     = 1.0
    NotSet = -1.0

我們將了解這些如何影響開發者類別中的屬性定義

屬性定義

字串屬性定義如下。

BASE_FILE_NAME: str = 'config.ini'
MODULE_NAME:    str = 'version2properties'

class ConfigurationPropertiesVersion2(ConfigurationProperties, metaclass=SingletonV3):

    def __init__(self):
        self.logger: Logger = getLogger(LOGGER_NAME)

        super().__init__(baseFileName=BASE_FILE_NAME, moduleName=MODULE_NAME, sections=CONFIGURATION_SECTIONS)

        self._configParser.optionxform = self._toStr    # type: ignore

        self._loadConfiguration()

我們擺脫的是存取 configParser 來取得和設定值的樣板。 我們加入的是configurationGetter 和configurationSetter 裝飾器。 我不會介紹裝飾器的實作細節,並將其作為讀者的練習。 這些裝飾器負責與配置解析器互動以取得和設定值。 設定值時,configurationSetter 裝飾器執行直寫.

開發者定義整數屬性如下。

from codeallybasic.ConfigurationProperties import Sections

CONFIGURATION_SECTIONS: Sections = Sections(
    {
        SectionName('General'):  SECTION_GENERAL,
        SectionName('Database'): SECTION_DATABASE,
    }
)

請注意,configurationGetter 裝飾器有一個可選參數。 它是一個函數,它會取得字串屬性值並將其轉換為適當的類型值,然後傳回呼叫者屬性。 這可以應用於浮動屬性。

我們想要保留枚舉名稱的枚舉屬性定義如下:

from codeallybasic.ConfigurationProperties import Section
from codeallybasic.ConfigurationProperties import ConfigurationNameValue
from codeallybasic.ConfigurationProperties import PropertyName

SECTION_GENERAL: Section = Section(
    [
        ConfigurationNameValue(name=PropertyName('debug'),                           defaultValue='False'),
        ConfigurationNameValue(name=PropertyName('logLevel'),                     defaultValue='Info'),
        ConfigurationNameValue(name=PropertyName('phoneyEnumByValue'),  defaultValue=DEFAULT_PHONEY_ENUM_BY_VALUE.value),
        ConfigurationNameValue(name=PropertyName('impostorEnumByName'), defaultValue=DEFAULT_IMPOSTOR_ENUM_BY_NAME.name),
    ]
)

SECTION_DATABASE: Section = Section(
    [
        ConfigurationNameValue(name=PropertyName('dbName'), defaultValue='example_db'),
        ConfigurationNameValue(name=PropertyName('dbHost'), defaultValue='localhost'),
        ConfigurationNameValue(name=PropertyName('dbPort'), defaultValue='5432'),
    ]
)

除了使用適當的裝飾器之外,請注意,要保留枚舉名稱,請使用 enumUseName 參數並將其設為 True.

以下是開發人員希望保留其值的枚舉屬性。 請注意,setter 裝飾器表明它是一個枚舉。 另外請注意,開發者必須提供反序列化方法,將值轉換為特定的枚舉值。

class PhoneyEnumByValue(Enum):
    TheWanderer = 'The Wanderer'
    Mentiroso   = 'Mentiroso'
    FakeBrenda  = 'Faker Extraordinaire'
    NotSet          = 'Not Set'

    @classmethod
    def deSerialize(cls, value: str) -> 'PhoneyEnumByValue':

    @classmethod
    def deSerialize(cls, value: str) -> 'PhoneyEnumByValue':

        match value:
            case PhoneyEnumByValue.TheWanderer.value:
                phoneyEnum: PhoneyEnumByValue = PhoneyEnumByValue.TheWanderer
            case PhoneyEnumByValue.Mentiroso.value:
                phoneyEnum = PhoneyEnumByValue.Mentiroso
            case PhoneyEnumByValue.FakeBrenda.value:
                phoneyEnum = PhoneyEnumByValue.FakeBrenda
            case _:
                raise Exception('Unknown PhoneyEnumByValue')

        return phoneyEnum


class ImpostorEnumByName(Enum):
    Low       = 0.1
    Medium = 0.5
    High     = 1.0
    NotSet = -1.0

訪問和修改屬性

存取和修改屬性與版本 1 完全相同。

    @property
    @configurationGetter(sectionName='General')
    def debug(self) -> str:
        return ''       # never executed

    @debug.setter
    @configurationSetter(sectionName='General')
    def debug(self, newValue: str):
        pass

上面的程式碼片段產生以下輸出。

    @property
    @configurationGetter(sectionName='Database', deserializeFunction=int)
    def dbPort(self) -> int:
        return -1

    @dbPort.setter
    @configurationSetter(sectionName='Database',)
    def dbPort(self, newValue: int):
        pass

結論

本文的原始碼在這裡。 請參閱支援類 SingletonV3。 查看ConfigurationProperties的實作

實現的結果讓我作為程式碼的使用者感到滿意。 我能夠取得和設定類型屬性。 它並沒有像我最初希望的那樣刪除那麼多程式碼。 然而,它確實為我提供了可重複使用的程式碼。 然而,它確實提示我在 PyCharm 中編寫一個實時模板來幫助我產生單獨的屬性。

優點

  • 輕鬆型別安全地存取應用程式屬性
  • 用於不同實作的可重複使用父類別
  • 資料結構驅動的程式碼,用於新增部分和配置鍵

缺點

  • 還有很多樣板 代碼
  • 我覺得使用裝飾器是對它們的誤用

我的下一篇文章我實作了所謂的動態屬性。 它完全刪除了所有樣板程式碼,並且仍然保留了上述優點。

以上是邁向輕鬆的 Python 設定檔版本 2的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何將元素附加到Python列表中?您如何將元素附加到Python列表中?May 04, 2025 am 12:17 AM

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

您如何創建Python列表?舉一個例子。您如何創建Python列表?舉一個例子。May 04, 2025 am 12:16 AM

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

討論有效存儲和數值數據的處理至關重要的實際用例。討論有效存儲和數值數據的處理至關重要的實際用例。May 04, 2025 am 12:11 AM

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。

您如何創建Python數組?舉一個例子。您如何創建Python數組?舉一個例子。May 04, 2025 am 12:10 AM

pythonarraysarecreatedusiseThearrayModule,notbuilt-Inlikelists.1)importThearrayModule.2)指定tefifythetypecode,例如,'i'forineizewithvalues.arreaysofferbettermemoremorefferbettermemoryfforhomogeNogeNogeNogeNogeNogeNogeNATATABUTESFELLESSFRESSIFERSTEMIFICETISTHANANLISTS。

使用Shebang系列指定Python解釋器有哪些替代方法?使用Shebang系列指定Python解釋器有哪些替代方法?May 04, 2025 am 12:07 AM

除了shebang線,還有多種方法可以指定Python解釋器:1.直接使用命令行中的python命令;2.使用批處理文件或shell腳本;3.使用構建工具如Make或CMake;4.使用任務運行器如Invoke。每個方法都有其優缺點,選擇適合項目需求的方法很重要。

列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?列表和陣列之間的選擇如何影響涉及大型數據集的Python應用程序的整體性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

說明如何將內存分配給Python中的列表與數組。說明如何將內存分配給Python中的列表與數組。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python數組中指定元素的數據類型?您如何在Python數組中指定元素的數據類型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版