Skip to main content

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`.

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 hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page