A typed configparser
Project description
typed-configparser
typed-configparser is an extension of the standard configparser module with support for typed configurations using dataclasses. It leverages Python's type hints and dataclasses to provide a convenient way of parsing and validating configuration files.
Features
✓ Fully typed.
✓ Use dataclasses to parse the configuration file.
✓ Support for almost all python built-in data types - int
, float
, str
, list
, tuple
, dict
and complex data types using Union
and Optional
.
✓ Built on top of configparser
, hence retains all functionalities of configparser
.
✓ Support for optional values (optional values are automatically set to None
if not provided).
✓ Smarter defaults (see below).
Installation
You can install typed_configparser
using pip
:
pip install typed-configparser
Usage
examples/basic.py
# This is a complete example and should work as is
from typing import List
from typed_configparser import ConfigParser
from dataclasses import dataclass
@dataclass
class BASIC:
option1: int
option2: str
option3: float
option4: List[str]
config = """
[BASIC]
option1 = 10
option2 = value2
option3 = 5.2
option4 = [foo,bar]
"""
parser = ConfigParser()
parser.read_string(config)
section = parser.parse_section(using_dataclass=BASIC)
print(section)
BASIC(option1=10, option2=value2, option3=5.2, option4=['foo', 'bar'])
examples/unions_and_optionals.py
# This is a complete example and should work as is
from typing import List, Union, Optional, Dict, Tuple
from typed_configparser import ConfigParser
from dataclasses import dataclass, field
@dataclass
class DEFAULT_EXAMPLE:
option1: int
option2: Union[List[Tuple[str, str]], List[int]]
option3: Dict[str, str] = field(default_factory=lambda: {"default_key": "default_value"})
option4: Optional[float] = None
config = """
[DEFAULT]
option1 = 20
option2 = default_value2
[MY_SECTION_1]
option2 = [10,20]
option4 = 5.2
[MY_SECTION_2]
option2 = [(value2a, value2b), (value2c, value2b), (value2c, value2d)]
option3 = {key: value}
option4 = none
"""
parser = ConfigParser()
parser.read_string(config)
my_section_1 = parser.parse_section(using_dataclass=DEFAULT_EXAMPLE, section_name="MY_SECTION_1")
my_section_2 = parser.parse_section(using_dataclass=DEFAULT_EXAMPLE, section_name="MY_SECTION_2")
print(my_section_1)
print(my_section_2)
DEFAULT_EXAMPLE(option1=20, option2=[10, 20], option3={'default_key': 'default_value'}, option4=5.2)
DEFAULT_EXAMPLE(option1=20, option2=[('value2a', 'value2b'), ('value2c', 'value2b'), ('value2c', 'value2d')], option3={'key': 'value'}, option4=None)
Check example
directory for more examples.
Defaults
configparser
includes sensible defaults options which allows you to declare a[DEFAULT]
section in the config file for fallback values.typed_configparser
goes a step further and allows you to set a final (last) level of defaults at dataclass level.
License
Project details
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
File details
Details for the file typed_configparser-1.0.2.tar.gz
.
File metadata
- Download URL: typed_configparser-1.0.2.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f845d9d5b134ca1fa90060e73c4ac79503bc99a406333a1198eb05bee79fff8e |
|
MD5 | 76cffc06a895e58bb4a968d017b69737 |
|
BLAKE2b-256 | 438cb50b8a63bfea707100c37140baf051cfb7e9e934a808963568f451c3bb15 |
File details
Details for the file typed_configparser-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: typed_configparser-1.0.2-py3-none-any.whl
- Upload date:
- Size: 20.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6de6b948b058bc82feaba747d60d19f688efe4bd9bd35f2641f9651bf4d41f60 |
|
MD5 | 41e6eb5344746ccbbc05d49da56b3f64 |
|
BLAKE2b-256 | c14036f0ac2737dfa317c5f8658df17646a48d11864f2f670c2c265bf91d4f7d |