Small config manager for parsing configs from different sources
Project description
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 import config
class TestConfig(config.Config):
name: str
age: int
is_useful: bool = False
my_config = TestConfig()
config.parse_env(my_config, prefix="test_config_")
config.parse_json(my_config, json_path="test_config.json")
config.parse_arguments(my_config, "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.
BoolType
Generally all not empty containers (even string "false"
)
cast to bool
would return True
.
Thus, there is custom type BoolType
.
Variable cast to BoolType
will get True
value in one of these cases:
- variable is subclass of
str
and it's value one of (in any case):"yes"
"true"
"1"
"on"
- variable is numeric, and it's value is
1
(or1.0
)
from config_manager.config_types import BoolType
assert BoolType("yes") == True
assert BoolType("yES") == True
assert BoolType("tRuE") == True
assert BoolType("1") == True
assert BoolType("on") == True
assert BoolType("no") == False
assert BoolType("FaLsE") == False
assert BoolType(1.0) == True
assert BoolType(0.0) == False
assert BoolType(1) == True
assert BoolType(0) == False
assert BoolType(True) == True
assert BoolType(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 of primitives, so every element will be cast
to primitive's type. For example:
from config_manager.config_types import ListType
assert ListType[int](["1", "2", "3"]) == [1, 2, 3]
assert ListType[str]([1.0, -213.5122, 52.123]) == ["1.0", "-213.5122", "52.123"]
All the parse sources provide different ways to define list, so there they are:
- in
predefine
andjson
case simply assign any python list to variable - in
environment
case provide list separated by comma without spaces, likemy_cool_list=hello,world,ough
- in
argument
case put list items separated by spaces after variable name, like--cool_list 1 2 3 1123
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for config_manager_evjeny-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 256ed0b71aebd64e27b0de5e37473acb151242f5d14b9e5ace9ffe74067e573c |
|
MD5 | b51ae5e3f7da3d6b294ce6f421adcd94 |
|
BLAKE2b-256 | 2570737bb2d762289a97479cd58e4602cf674f4fe2aa68e09f7a1cfb5ccd039a |
Hashes for config_manager_evjeny-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5477b7de4289cb7693066fc87854a072727d635942a120970de7dd232e2f0bc |
|
MD5 | de14342612afe2e29ead8b43dd85bcef |
|
BLAKE2b-256 | 788496300f6b393ccd03b7cd2a3a8b1d1cf596c00f78c5eee4b22dfc27c99e04 |