Python package that allows to parse typed configs defined by python dataclasses
Project description
PythonConfigParser
easy, light-weight fully-typed python configs. not more, not less.
Usage
define your config stucture in a dataclass like
from dataclasses import dataclass
from typing import List
@dataclass
class DummyConfigElement:
name: str
list: List[int]
another_list: List[float]
from file
First initialize the config parser with the path to the module with the config definitions.
parser = ConfigParser(datastructure_module_name="mysrc.datastructures.configs")
# Then parse the config from file.
my_config = parser.parse_from_file("myconfig.json")
from dictionary
my_config_dict = {"type_name" : "mysrc.datastructures.configs.a"}
my_config = ConfigParser().parse(my_config_dict)
type definition
There are two ways to define the configs type:
- specified in the config itself
- e.g in the code example above
- path to the config class must be specified as str in the config as key "type_name"
- specified when parsing the config
- if the config has no key "typed_config" set, the type can be specified when parsing the config
from mysrc.datastructures.configs import a
my_config_dict = {"some_key" : "some_value"}
my_config = ConfigParser().parse_typed(my_config_dict,a)
By default every field can be explicitly set to None. If you don't what this behaviour you can set the flag "allow_none" to False.
from cfgparser import settings
settings.allow_none = False
Features
- fully typed json and yaml configs
- nested configs
- complex union and optional types
- dict object into typed dataclass
- support for enums (string and int)
Whenever a Union type is encountered the parser tries to parse the config with the first type in the union. If this fails it tries the next type and so on. If all types fail the parser raises an exception.
Installation
pip install cfgparser
Features Roadmap
- specify config from cli
- post hock
- distributed configs
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 cfgparser-1.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c908572377a48fb1562f99990861affb98ab7fdb3dbce180c5378ce7b9e633b7 |
|
MD5 | 39e76b76d84f4834cc91f4a0b31686ed |
|
BLAKE2b-256 | b2dd3e729171e96d22e356c61f81f3a79ba8661dfa03f0fd326d89438cb99d30 |