Skip to main content

PythonConfigParser

truly fully-typed python configs - not more, not less.

PyPI version Badge Name codecov

Usage

  1. define your config structure in a dataclass like
from dataclasses import dataclass
from typing import List

@dataclass
class DummyConfigElement:
    name: str
    list: List[int]
    another_list: List[float]
  1. initialize the config parser with the path to the module with the config definitions.
parser = ConfigParser(datastructure_module_name="mysrc.datastructures.configs")
  1. parse a config from file or dict

from file

my_config = parser.parse_from_file("myconfig.json")

from dictionary

my_config_dict = {"type_name" : "mysrc.datastructures.configs.a", "value" : 1}
my_config = parser.parse(my_config_dict)

Type definition

There are two ways to define the configs type:

  • specified in the config itself
    • store fully qualified name of the config class under the key "type_name" in the config
    • e.g in the code example above
  • 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"}
# config has no key "type_name" but type is specified when parsing
my_config = ConfigParser().parse_typed(my_config_dict,a)

Optional & Union fields

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

Union fields

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]

Features

  • ✅ fully typed json and yaml configs
  • ✅ nested configs
  • ✅ complex union and optional types
  • ✅ dict object into typed dataclass
  • ✅ support for enums (value given as string or int)
  • ✅ support for typed tuples (e.g. Tuple[int, str, float])
  • ✅ override type_name if type is specified when parsing

Installation

pip install cfgparser

Feature Roadmap

  • ⬜ specify config from cli
  • ⬜ post hock
  • ⬜ distributed configs
  • ⬜ typed dicts (e.g. Dict[str, MyType])
  • ⬜ typed functions (e.g. torch.functional.relu)
  • ⬜ save config to file (json, yaml) with and without typeinfo

Release files for cfgparser 1.0.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cfgparser 1.0.8
File Size Uploaded
cfgparser-1.0.8.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cfgparser 1.0.8
File Interpreter ABI Platform
cfgparser-1.0.8-py3-none-any.whl Python 3 none any Details

Total release size: 18.5 kB

Release files / cfgparser-1.0.8.tar.gz

Download URL cfgparser-1.0.8.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
852c1dc1a15a8a04d711f383b1a069f4f2bb77a8aff5e075491243b1fb2e6c9d
BLAKE2b-256 checksum
How to use checksums
711d561fd9a57b11c7acacfbb2b878bf7a7cfa8897190c9914bd34f603921e7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release files / cfgparser-1.0.8-py3-none-any.whl

Download URL cfgparser-1.0.8-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ad60fc6aa05a12508bf58b72c189651d5f206bd1809f8296cf4211ea8cd0fc07
BLAKE2b-256 checksum
How to use checksums
2e5fc82b9ad338b8f285f0e9f1039723bd53dfffa401538e5a2276dacee19abb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.17

Release history Release notifications | RSS feed

This release

1.0.8 This release

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

1 release file

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page