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

Python動(dòng)態(tài)配置管理Dynaconf的實(shí)現(xiàn)示例詳解

 更新時(shí)間:2022年07月25日 16:30:54   作者:編程學(xué)習(xí)網(wǎng)  
這篇文章主要為大家介紹了Python動(dòng)態(tài)配置管理Dynaconf實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

正文

Dynaconf 是一個(gè) Python 的第三方模塊,旨在成為在 Python 中管理配置的最佳選擇。

它可以從各種來(lái)源讀取設(shè)置,包括環(huán)境變量、文件、服務(wù)器配置等

它適用于任何類(lèi)型的 Python 程序,包括 Flask 和 Django 擴(kuò)展

1.準(zhǔn)備

開(kāi)始之前,你要確保 Python 和 pip 已經(jīng)成功安裝在電腦上

然后,請(qǐng)選擇以下任一種方式輸入命令安裝依賴:

  • Windows 環(huán)境 打開(kāi) Cmd ( 開(kāi)始-運(yùn)行-CMD )
  • MacOS 環(huán)境 打開(kāi) Terminal ( command + 空格輸入 Terminal )
  • 如果你用的是 VSCode 編輯器 或 Pycharm,可以直接使用界面下方的 Terminal
pip install dynaconf

2.初步使用DynaConf

在你的項(xiàng)目的根目錄中運(yùn)行 dynaconf init 命令。

cd path/to/your/project/
dynaconf init -f toml

會(huì)有類(lèi)似如下的輸出,說(shuō)明初始化完成:

?? Configuring your Dynaconf environment
------------------------------------------
?? The file `config.py` was generated.
??? settings.toml created to hold your settings.
?? .secrets.toml created to hold your secrets.
?? the .secrets.* is also included in `.gitignore`
beware to not push your secrets to a public repo.
?? Dynaconf is configured! read more on https://dynaconf.com

剛剛初始化的時(shí)候我們選擇了 toml 格式。實(shí)際上你還可以選擇 toml|yaml|json|ini|py ,不過(guò) toml 是默認(rèn)的,也是最推薦的配置格式。

初始化完成后會(huì)創(chuàng)建以下文件:

.
├── config.py # 需要被導(dǎo)入的配置腳本
├── .secrets.toml # 像密碼等敏感信息配置
└── settings.toml # 應(yīng)用配置

初始化完成后你就可以編寫(xiě)你的配置,編輯settings.toml:

key = "value"
a_boolean = false
number = 1234
a_float = 56.8
a_list = [1, 2, 3, 4]
a_dict = {hello="world"}
[a_dict.nested]
other_level = "nested value"

然后就可以在你的代碼中導(dǎo)入并使用這些配置:

from config import settings
assert settings.key == "value"
assert settings.number == 789
assert settings.a_dict.nested.other_level == "nested value"
assert settings['a_boolean'] is False
assert settings.get("DONTEXIST", default=1) == 1

如果是密碼等敏感信息,你可以配置在 .secrets.toml 中:

password = "xxxxxxxxxxxxxx"
token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
message = "This file doesn't go to your pub repo"

.secrets.toml 文件會(huì)被自動(dòng)加入到 .gitignore 文件中,這些信息不會(huì)被上傳到Git倉(cāng)庫(kù)上。

同時(shí),DYNACONF還支持帶前綴的環(huán)境變量:

export DYNACONF_NUMBER=789
export DYNACONF_FOO=false
export DYNACONF_DATA__CAN__BE__NESTED=value
export DYNACONF_FORMATTED_KEY="@format {this.FOO}/BAR"
export DYNACONF_TEMPLATED_KEY="@jinja {{ env['HOME'] | abspath }}"

3.高級(jí)使用

你還可以在Flask或Django中使用DynaConf,以Django為例,第一步要先確保已經(jīng)設(shè)置 DJANGO_SETTINGS_MODULE 環(huán)境變量:

export DJANGO_SETTINGS_MODULE=yourproject.settings

然后在 manage.py 相同文件夾下運(yùn)行初始化命令:

dynaconf init -f yaml

然后按照終端上的說(shuō)明進(jìn)行操作:

Django app detected
?? Configuring your Dynaconf environment
------------------------------------------
??? settings.yaml created to hold your settings.
?? .secrets.yaml created to hold your secrets.
?? the .secrets.yaml is also included in `.gitignore`
beware to not push your secrets to a public repo
or use dynaconf builtin support for Vault Servers.
? path/to/yourproject/settings.py is found do you want to add dynaconf? [y/N]:

回答 y:

?? Now your Django settings are managed by Dynaconf
?? Dynaconf is configured! read more on https://dynaconf.com

在 Django 上,推薦的文件格式是yaml,因?yàn)樗梢愿p松地保存復(fù)雜的數(shù)據(jù)結(jié)構(gòu),但是你依然可以選擇使用 toml、json、ini 甚至將你的配置保存為 .py 格式。

初始化 dynaconf 后,在現(xiàn)有的settings.py底部包含以下內(nèi)容:

# HERE STARTS DYNACONF EXTENSION LOAD
import dynaconf # noqa
settings = dynaconf.DjangoDynaconf(__name__) # noqa
# HERE ENDS DYNACONF EXTENSION LOAD (No more code below this line)

現(xiàn)在,在你的 Django 視圖、模型和所有其他地方,你現(xiàn)在可以正常使用 django.conf.settings,因?yàn)樗驯?Dynaconf 設(shè)置對(duì)象替換。

from django.conf import settings
def index(request):
    assert settings.DEBUG is True
    assert settings.NAME == "Bruno"
    assert settings.DATABASES.default.name == "db"
    assert settings.get("NONEXISTENT", 2) == 2

現(xiàn)在,通過(guò)修改 manage.py 相同文件夾下的配置文件,就能讓配置全局生效了

以上就是Python動(dòng)態(tài)配置管理Dynaconf實(shí)現(xiàn)示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python動(dòng)態(tài)配置Dynaconf的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論