Magic-settings
Installation
pip install magic-settings
Using settings from yaml file
pip install magic-settings[yaml]
Initialization
Project settings class declaration
from magic_settings import BaseSettings, Property
from functools import partial
class MySettings(BaseSettings):
VERSION = Property(types=str)
PROJECT_DIR = Property(types=str)
LOGGING_LEVEL = Property(default='INFO', choices=['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])
RETRIES_NUMBER = Property(types=int, converts=[int])
COEFFICIENT = Property(types=float, converts=[float])
DEBUG = Property(types=bool, converts=[int, bool], default=False)
DISTRIBUTED_SERVICE_HOST_NAMES = Property(types=list, converts=[partial(str.split, sep=',')])
Class Property is a descriptor with following parameters:
- types - Type of
valueor a tuple of possibletypes. It is aValueErrorifvalueis not one of thetypes. - validators - List of
callableobjects each of which is successively applied tovalue. RaisesValueErrorifvaluedoes not pass at least one of the validations (if any validation function returnsFalse). - choices - List of any objects. If
valueis not inchoices- raisesValueError. When using this parameter, parameterstypesandvalidatorsare ignored. - default - Sets the default value of
Property. - converts - List of
callableobjects. It is a chain of transformations that are successively applied to thevalueand overwrite it each time. It applies tovalueonly ifvalueis a string. RaisesValueErrorifvalueat least one of the transformations failed to apply.
Property classes
Besides Property following classes may be used for standard types:
BoolProperty: accepts boolean values, converts case-insensitivetrueorfalseto appropriate python boolean value. Also this property accepts numbers (0is False,1is True).FloatProperty: accepts float number values.IntProperty: accepts integer number values.StringProperty: accepts string values.StringListProperty: accepts list of strings. You can specify delimiter in constructor of this class (,is default value).HostListProperty: accepts list of hosts. Each host is a tuple containing astringhostname and anintport. Pairs should be divided by comma, hostname and port should be divided by colon. For example,192.168.20.1:80,www.yandex.ru:1234,localhost:8888will be converted into[('192.168.20.1', 80), ('www.yandex.ru', 1234), ('localhost', 8888)].
Above example may be simplified using these properties:
from magic_settings import (BaseSettings, Property,
BoolProperty, FloatProperty, IntProperty, StringListProperty, StringProperty)
class MySettings(BaseSettings):
VERSION = StringProperty()
PROJECT_DIR = StringProperty()
LOGGING_LEVEL = Property(default='INFO', choices=['NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'])
RETRIES_NUMBER = IntProperty()
COEFFICIENT = FloatProperty()
DEBUG = BoolProperty(default=False)
DISTRIBUTED_SERVICE_HOST_NAMES = StringListProperty()
Settings configuration
Settings configuration occurs at the stage of creating a Settings object.
from my_project import my_module, my_awesome_module
from my_config import MySettings
settings = MySettings(
modules=[my_module, my_awesome_module],
prefix='MY_PROJECT_ENV_SETTINGS',
dotenv_path='/path/to/my/env',
override_env=True,
yaml_settings_path='/path/to/my/yaml/settings.yaml',
use_env=True
)
Parameters
-
modules: List of Python modules with variables to import. Default
None. -
prefix: The prefix with which the environment variables are taken. Default -
None.settings.py
class MySettings(BaseSettings): PSYDUCK = Property(types=str)
.env
MYPROJECT_PSYDUCK=Owowowow
some_other_place.py
settings = MySettings(prefix='MYPROJECT')
or
settings = MySettings(prefix='MYPROJECT_')
-
dotenv_path: Path to env-file. Default -
None. Using for exporting variables from env-file to environment. Ifdotenv_pathisNone- walking up the directory tree looking for the specified file - called.envby default. -
override_env:
True- override existing system environment variables with variables from.env- file,False- do not override. Default -False. -
yaml_settings_path: Path to yaml config file. Default -
None. -
use_env:
True- use environment variables. Default -True.
Exceptions
ValueError: If modules type is not list or NoneType and if type of element in modules is not ModuleType.
Settings loading
Loading settings can be initiated anywhere in the project.
from where_your_settings import settings
settings.init()
If called again, it goes through the configuration files and update properties.
Settings priority
In case of intersection of settings the following priority will be applied: my_module -> my_awesome_module -> .env -> settings.yaml
class MySettings(BaseSettings):
PSYDUCK = Property(types=str)
my_module.py
PSYDUCK = 'one'
my_awesome_module.py
PSYDUCK = 'two'
.env
MYPROJECTPREFIX_PSYDUCK=env
setting.yaml
PSYDUCK: yaml
Examples
>>> settings = MySettings(modules=[my_module])
>>> settings.PSYDUCK
'one'
>>> settings = MySettings(modules=[my_module, my_awesome_module])
>>> settings.PSYDUCK
'two'
>>> settings = MySettings(modules=[my_awesome_module, my_module])
>>> settings.PSYDUCK
'one'
>>> settings = MySettings(modules=[my_module, my_awesome_module], dotenv_path='/path/to/dotenv')
>>> settings.PSYDUCK
'env'
>>> settings = MySettings(modules=[my_module, my_awesome_module], dotenv_path='/path/to/dotenv', use_env=False)
>>> settings.PSYDUCK
'two'
>>> settings = MySettings(modules=[my_module, my_awesome_module], dotenv_path='/path/to/dotenv', yaml_settings_path='/path/to/yaml/settings.yaml')
>>> settings.PSYDUCK
'yaml'
Temporary Property override
my_module.py
PIKACHU = 'Psyduck_is_not_fine'
PSYDUCK = 'Owowowow'
from my_project import my_module
from my_config import MySettings
class MySettings(BaseSettings):
PSYDUCK = Property(types=str)
PIKACHU = Property(types=str)
settings = MySettings(modules=[my_module])
settings.init()
with settings.temp_set_attributes(PSYDUCK='I_am_ok', PIKACHU='Psyduck_is_ok'):
print(settings.PSYDUCK) # 'I_am_ok'
print(settings.PIKACHU) # 'Psyduck_is_ok'
print(settings.PSYDUCK) # 'Owowowow'
print(settings.PIKACHU) # 'Psyduck_is_not_fine'
Method temp_set_attributes is not thread-safe.
Settings list
You can use methods to_dict(), to_json() to get current settings:
from magic_settings import BaseSettings, Property
class MySettings(BaseSettings):
PSYDUCK = Property(types=str)
PIKACHU = Property(types=str)
settings = MySettings(dotenv_path='12345.env')
settings.PIKACHU = '3'
settings.PSYDUCK = '12345'
pprint(settings.to_dict())
{
'properties': {
'PIKACHU': '3',
'PSYDUCK': '12345'
},
'sources': [{
'source_type': 'dotenv',
'address': {
'dotenv_path': '12345.env',
'override': False
}
}]
}
Validation
It is recommended to use following BaseSettings class methods during redefinition update_settings_from_source method:
pre_validate- check that types are configured correctly; check that the values fromchoicesand the default pass the type check.post_validate- check if eachPropertyis assigned a value.
Dynamic settings
Implementing a custom dynamic settings source
Example with storing settings in dict source:
from magic_settings import BaseDynamicSettings, Property
source = {
'JIGGLYPUFF': 'pink'
}
class BaseDynamicSettingsDict(BaseDynamicSettings):
async def update_settings_from_source(self):
super().update_config(**source)
async def update_config(self, **kwargs):
source.update(kwargs)
return super().update_config(**kwargs)
Definition of project`s dynamic settings class
class MyDynamicSettings(BaseDynamicSettingsDict):
JIGGLYPUFF = Property(types=str)
Dynamic Settings Initialization
loop = asyncio.get_event_loop()
dynamic_settings = MyDynamicSettings(loop=loop, update_period=5, task_retries_number=5)
- update_period: time between updating settings from source, in seconds.
- task_retries_number: the number of attempts to update the settings when an exception occurred before stopping the task.
Dynamic settings update
Updating settings only once
await dynamic_settings.update_settings_from_source()
Starting the update loop
await dynamic_settings.start_update()
Stopping the update loop
await dynamic_settings.stop_update()
Writing settings into the source
await dynamic_settings.update_config(JIGGLYPUFF='magenta')
Exceptions
- magic_settings.DynamicSettingsSourceError - this exception should be selected if the settings source in the class inherited from
BaseDynamicSettingsis unavailable.
Metadata
Release files for magic-settings 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| magic-settings-1.2.0.tar.gz | 12.5 kB | Without provenance | Details for magic-settings-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| magic_settings-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for magic_settings-1.2.0-py3-none-any.whl |
Total release size: 23.6 kB
Release files / magic-settings-1.2.0.tar.gz
| Download URL | magic-settings-1.2.0.tar.gz |
|---|---|
| Size | 12.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
be24793617d9faef25a8b540c3bf1920e2a2587327d1ae84c42ca368405a9363
|
|
BLAKE2b-256 checksum How to use checksums |
51b95f570c03cee61c12ae097c1df2a7e3de22070af24674f633a88008c79d2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
|
Release files / magic_settings-1.2.0-py3-none-any.whl
| Download URL | magic_settings-1.2.0-py3-none-any.whl |
|---|---|
| Size | 11.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c5928b6a51f41b62d8287898b271904ac15115314a0afbdd6a16ca6b7b5e3b7
|
|
BLAKE2b-256 checksum How to use checksums |
44d01c2364baf665a737f5d68ae9e7e05df6021caaed4337d0c9ece6b7e5f2b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
|
Security for magic-settings 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.