config_manager
Configuration manager for parsing from different sources
Installation
python -m pip install config-manager-evjeny
Usage
Create class with config variables and inherit it from config_manager.config.Config.
You can define variables and their types or even define values.
Then we can parse variables from different sources.
For example, this is example_parser.py:
from config_manager.config import Config
from config_manager.variable_parsers import ListType
class TestConfig(Config):
"""Config example with some variables of different types"""
name: str
age: int
is_useful: bool = False
parts: ListType[float]
my_config = TestConfig() \
.parse_env(prefix="test_config_") \
.parse_json(json_path="test_config.json") \
.parse_arguments("TestConfig parser")
print(my_config)
If we run it:
test_config_age=33 python example_parser.py \
--name hello \
--is_useful false \
--parts "[0.25, 0.5, 0.75]"
It will output something like and all the primitive types will be parsed correctly:
age = 33
is_useful = False
name = hello
parts = [0.25, 0.5, 0.75]
Type details
str, int and float
For str, int and float casting is the same as builtin functions.
bool
Generally all not empty containers (even string "false")
cast to bool would return True.
Thus, there is a BoolParser that casts value to True in one of these cases:
- variable is subclass of
strand it's value one of (in any case):"yes""true""1""on"
- in other cases default
boolcall used
from config_manager.variable_parsers import BoolParser
parser = BoolParser()
assert parser("yes") == True
assert parser("yES") == True
assert parser("tRuE") == True
assert parser("1") == True
assert parser("on") == True
assert parser("no") == False
assert parser("FaLsE") == False
assert parser(1.0) == True
assert parser(0.0) == False
assert parser(1) == True
assert parser(0) == False
assert parser(True) == True
assert parser(False) == False
ListType
As it is impossible to get type from typing.List[T]
(plus this annotation removed in Python 3.9) there is a ListType.
It can be used with any parseable type, so every element will be cast
to target type. For example:
from config_manager.variable_parsers import ListType
# shallow lists
assert ListType[int](["1", "2", "3"]) == [1, 2, 3]
assert ListType[str]([1.0, -213.5122, 52.123]) == ["1.0", "-213.5122", "52.123"]
assert ListType[int]("[1, 2, 3]") == [1, 2, 3]
All the parse sources provide different ways to define list, so there they are:
- in
predefineandjsoncase simply assign any python list to variable - in
environmentandargumentcases every list is parsed from string[item_1, item_2, ...]
Release files for config-manager-evjeny 0.3.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 | |
|---|---|---|---|
| config_manager_evjeny-0.3.0.tar.gz | 4.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| config_manager_evjeny-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / config_manager_evjeny-0.3.0.tar.gz
| Download URL | config_manager_evjeny-0.3.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9ecef1041773c01cf9d1d5c3a8335a5c2abd762c27e340db454c27a3816978b1
|
|
BLAKE2b-256 checksum How to use checksums |
4b246be29afc711ff63550637770ee58092dea1002f1f2bb6eed7df80ab971a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|
Release files / config_manager_evjeny-0.3.0-py3-none-any.whl
| Download URL | config_manager_evjeny-0.3.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f35cfef01607f8d06ab1a6276419dc6eb79c0dc57ce8f46721d684a75c50cd0c
|
|
BLAKE2b-256 checksum How to use checksums |
d76cdf1cd62736e6911610b5e18624ffd0cb974bbd21e9c0312f0e8310195084
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
|