Created
November 26, 2021 21:24
-
-
Save lclpedro/87c32370577d7cb8f1cdd1c5bb440daf to your computer and use it in GitHub Desktop.
Dynaconf examples of use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TOKEN = 123123 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from dynaconf import Dynaconf | |
# Init a config dynaconf. | |
# dynaconf init -f <toml> <json> <yaml,yml> <ini> <.env> | |
settings = Dynaconf( | |
settings_files=['settings.toml', '.secrets.toml'], | |
) | |
# For enable with POC. | |
os.environ['VAULT_ENABLED_FOR_DYNACONF']='false' | |
# No is case-sensitive | |
print(settings.ENV_STR) | |
print(settings.env_str) | |
''' | |
Uma string | |
Uma string | |
''' | |
#Type hints | |
print(settings.env_str) | |
print(type(settings.env_str)) | |
print(settings.env_int) | |
print(type(settings.env_int)) | |
print(settings.env_bool) | |
print(type(settings.env_bool)) | |
print(settings.env_list) | |
print(type(settings.env_list)) | |
print(settings.env_dict) | |
print(type(settings.env_dict)) | |
print(settings.env_float) | |
print(type(settings.env_float)) | |
''' | |
Uma string | |
<class 'str'> | |
100 | |
<class 'int'> | |
True | |
<class 'bool'> | |
['uma', 'lista'] | |
<class 'dynaconf.vendor.box.box_list.BoxList'> | |
{'firstName': 'Pedro', 'email': '[email protected]'} | |
<class 'dynaconf.utils.boxing.DynaBox'> | |
1.1 | |
<class 'float'> | |
''' | |
# Using data list and dict | |
a_list = settings.env_list | |
print(a_list[0]) | |
a_dict = settings.env_dict | |
print(a_dict) | |
print(a_dict['firstName']) | |
''' | |
uma | |
{'firstName': 'Pedro', 'email': '[email protected]'} | |
Pedro | |
''' | |
# Using Vault | |
# $ docker run -d -e 'VAULT_DEV_ROOT_TOKEN_ID=myroot' -p 8200:8200 vault | |
# $ pip install 'dynaconf[vault]' | |
settings = Dynaconf( | |
environment=True, | |
vault_enabled=True, | |
vault={'url': 'http://localhost:8200', 'token': 'myroot'} # recommended to keep as env var. | |
) | |
os.environ['VAULT_ENABLED_FOR_DYNACONF']='true' | |
# $ dynaconf -i config.settings write vault -s password=password_default | |
# $ dynaconf -i config.settings write vault -s password=password_dev -e development | |
# $ dynaconf -i config.settings write vault -s password=password_prod -e production | |
print(settings.password) | |
''' | |
$ password_default | |
''' | |
# Using variables by environments. | |
print(settings.from_env('development').password) | |
''' | |
$ password_dev | |
''' | |
print(settings.from_env('production').password) | |
''' | |
$ password_prod | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV_STR = 'Uma string' | |
ENV_INT = 100 | |
ENV_BOOL = true | |
ENV_LIST = ['uma', 'lista'] | |
ENV_DICT = {firstName='Pedro', email='[email protected]'} | |
ENV_FLOAT = 1.10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment