设置文件的实用程序
custom-settings的Python项目详细描述
在用python描述配置文件时,需要在每个环境中对其进行更改。例如django的settings.py。 这个包提供了帮助它的实用程序。
安装
$ pip install custom_settings
如何使用
设置\自定义.py
AUTH_CREDENTIAL = 'MY_CREDENTIAL' INTEGER_VALUE = '1'
执行以下操作以使用此配置文件。
>>> import custom_settings >>> custom = custom_settings.load('settings_custom') >>> custom.get('AUTH_CREDENTIAL') 'MY_CREDENTIAL'
如果指定了类型参数,请将类型转换为。
>>> custom.get('INTEGER_VALUE', type_=int, default=10) 1
如果您在
>>> custom.get('PS1', use_environ=True) '$ '
如果您指定
>>> custom.get('NO_SET_VALUE', default=10) 10
如果在
>>> custom.get('NO_SET_VALUE', use_environ=True, raise_exception=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/envs/3.5/lib/python3.5/site-packages/custom_settings/adapters.py", line 40, in get raise exc.NoCustomSettingError('Not been set: {}'.format(name)) custom_settings.exc.NoCustomSettingError: Not been set: NO_SET_VALUE