Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Python package that allows to parse typed configs defined by python dataclasses
truly fully-typed python configs - not more, not less.
from dataclasses import dataclass
from typing import List
@dataclass
class DummyConfigElement:
name: str
list: List[int]
another_list: List[float]
parser = ConfigParser(datastructure_module_name="mysrc.datastructures.configs")
my_config = parser.parse_from_file("myconfig.json")
my_config_dict = {"type_name" : "mysrc.datastructures.configs.a", "value" : 1}
my_config = parser.parse(my_config_dict)
There are two ways to define the configs type:
from mysrc.datastructures.configs import a
my_config_dict = {"some_key" : "some_value"}
# config has no key "type_name" but type is specified when parsing
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 behavior you can set the flag "allow_none" to False.
from cfgparser import settings
settings.allow_none = False
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. For instance, in the example below the parser tries to parse the config as a Bird. If this fails it tries to parse it as a Cat. If this fails too the parser raises an exception.
from dataclasses import dataclass
from typing import Union
@dataclass
class Bird:
name: str
wingspan: float
@dataclass
class Cat:
name: str
paws: int
@dataclass
class AnimalOwnership:
owner: str
animal : Union[Bird, Cat]
pip install cfgparser
FAQs
Python package that allows to parse typed configs defined by python dataclasses
We found that cfgparser demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.