A simple configuration manager for Python.
Project description
figga
A very simple configuration manager for Python.
Usage
figga currently supports three ways of specifying the configuration:
- standard Python dictionary
- environment variables with a common prefix
- one or more
INIfiles
Basic Usage
The default mechanism of instantiating a figga.Configuration instance is passing a simple Python dictionary:
from figga import Configuration
config = Configuration({'foo': 'bar', 'var1': 123, 'VAR2': 'buzz'})
This mechanism can be easily used to store configuration in any file format which can easily be converted to a dict, such as JSON or YAML:
import json
from figga import Configuration
with open('config.json') as json_config:
config = Configuration(json.load(json_config))
The instantiated object can be used from elsewhere in your code, e.g. if the above instantiation happened in module somewhere.py:
from somewhere import config
print(config.some_value)
Note that all variable names are normalised to lowercase, and access is case insensitive: config.foo, config.FOO or config.Foo all point to the same value.
In addition to direct access, values can be retrieved using the get method:
config.get('some_value')
Note that get also accepts the default argument, the value of which will override the default value specified at instantiation (see below).
Default Values
All constructor methods accept the argument default, which defines the behaviour when a non-existing value is accessed. This argument can handle three different types of default values:
- any object or scalar value, which will be returned as-is
- a callable, which will be called passing the accessed variable name as the only argument, returning its result
- a
BaseExceptionsubclass, which will be raised instead of returning a value
If no default value is specified, an unknown variable will have the value of None.
From Environment Variables
Another option to initialize the configuration manager is by taking the values of all the environment variables which begin with a common prefix:
import os
from figga import Configuration
os.environ['YOURAPP_VARIABLE'] = 'foo bar'
config = Configuration.from_environ(prefix='YOURAPP_')
print(config.yourapp_variable)
Optionally you can remove the prefix from the final configuration variables:
config = Configuration.from_environ(prefix='YOURAPP_', remove_prefix=True)
print(config.variable)
From INI Files
Alternatively, figga.Configuration can be instantiated using one or more configparser-compatible files:
from figga import Configuration
config = Configuration.from_files('config1.ini', '/vars/config2.ini')
If the file paths are not absolute, they are assumed to be relative to the current working directory. Paths can be either strings or os.PathLike instances.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file figga-0.3.0-py3-none-any.whl.
File metadata
- Download URL: figga-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.7.10 Linux/5.8.0-44-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69c9d4a4ca4fc049d84c45fc80c1e2b8181ebba440b373f542887068cc98705d
|
|
| MD5 |
654472f5bf5e4b86c53fab2969b67353
|
|
| BLAKE2b-256 |
21078a273b8991db96062a02480fe5e50285e5e6707a2a9bcaec88f45d6ada66
|