Comfortable python app configuration
Project description
Comfortable python configs from environment variables.
* Validated at **import time**. If you forgot a configuration variable you find out at startup.
* One source of configuration for all application code. No more `flask_conf.cfg`, `myconfig.ini`, `.env`, hardcoded constants and variables from `docker-compose.yml`.
* Type casting and validation at load time. Do it once and forget.
# Quickstart
```
>>> from confy import BaseEnvironConfig
>>> class Config(BaseEnvironConfig):
... DEBUG = ConfigField(processor=strtobool, default=True)
...
>>> Config.DEBUG
True
```
Loads variable `DEBUG` __at import time__ from environment, applies function `string_to_bool` to the value,
If variable is not provided uses the default of `True`.
# Config field options
* `default` - default value, can be anything
* `required` - boolean, default `False`. Throws `ConfigError` if value is required, but not provided.
* `processor` - callable. Can be used to validate the value
* `from_var` - specify environment variable to take value from. If none uses config class property name, __case is important__.
# Mocking in tests
Since its a global class that can't be instanced it's __very important__ to import it like this:
```
from package import module
print(module.ConfigClass.as_text())
```
and __not__ like this:
```
from package.module import ConfigClass
print(ConfigClass.as_text())
```
The first option allows you to monkey patch the config during testing.
Pytest example:
```
from package.module import ConfigClass
class TestConfig(ConfigClass):
pass
TestConfig.TEST = True
monkeypatch.setattr('package.module.ConfigClass', TestConfig)
```
All code accesing `module.ConfigClass` will get `TestConfig`.
* Validated at **import time**. If you forgot a configuration variable you find out at startup.
* One source of configuration for all application code. No more `flask_conf.cfg`, `myconfig.ini`, `.env`, hardcoded constants and variables from `docker-compose.yml`.
* Type casting and validation at load time. Do it once and forget.
# Quickstart
```
>>> from confy import BaseEnvironConfig
>>> class Config(BaseEnvironConfig):
... DEBUG = ConfigField(processor=strtobool, default=True)
...
>>> Config.DEBUG
True
```
Loads variable `DEBUG` __at import time__ from environment, applies function `string_to_bool` to the value,
If variable is not provided uses the default of `True`.
# Config field options
* `default` - default value, can be anything
* `required` - boolean, default `False`. Throws `ConfigError` if value is required, but not provided.
* `processor` - callable. Can be used to validate the value
* `from_var` - specify environment variable to take value from. If none uses config class property name, __case is important__.
# Mocking in tests
Since its a global class that can't be instanced it's __very important__ to import it like this:
```
from package import module
print(module.ConfigClass.as_text())
```
and __not__ like this:
```
from package.module import ConfigClass
print(ConfigClass.as_text())
```
The first option allows you to monkey patch the config during testing.
Pytest example:
```
from package.module import ConfigClass
class TestConfig(ConfigClass):
pass
TestConfig.TEST = True
monkeypatch.setattr('package.module.ConfigClass', TestConfig)
```
All code accesing `module.ConfigClass` will get `TestConfig`.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
confi-0.0.4.1.tar.gz
(3.2 kB
view details)
File details
Details for the file confi-0.0.4.1.tar.gz
.
File metadata
- Download URL: confi-0.0.4.1.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/38.4.0 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ffb8ebba51194f7be7b3b1bc0946216de42417e83fd487e83e8efb0450897c6 |
|
MD5 | d1b11f54eaeb3b9552b635b932b8e09a |
|
BLAKE2b-256 | 336cd550680225b086899109c41ecb0fa1a64351d7d18d6ec578cc6103c6549b |