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
basic_example.py
import dataclasses
from typed_configparser import ConfigParser
@dataclasses.dataclass
class AppConfig:
host: str
port: int
debug: bool
# Create an instance of Typed ConfigParser
config_parser = ConfigParser()
config_parser.read("conf.ini")
app_config = config_parser.parse_section(AppConfig, section_name='AppSection')
print(f"Host: {app_config.host}")
print(f"Port: {app_config.port}")
print(f"Debug Mode: {app_config.debug}")
conf.ini
[AppSection]
host = localhost
port = 8080
debug = True
optional_example.py
import typing
import dataclasses
from typed_configparser import ConfigParser
@dataclasses.dataclass
class AppConfig:
host: str
port: int
debug: typing.Optional[bool]
config_parser = ConfigParser()
app_config = config_parser.parse_section(AppConfig, section_name='AppSection')
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 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.0.tar.gz
.
File metadata
- Download URL: typed_configparser-1.0.0.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f2b641b7ab788530956f9ea5b7b5bb7b2b741e7591484c94bcb81d5707011b7 |
|
MD5 | 98c68fde96bf2841c91c0c0a00d2a111 |
|
BLAKE2b-256 | a144743ecd4dbca41cbf8f7085bc127576221cd8833042bfe96a1a0a8dcfb8af |
File details
Details for the file typed_configparser-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: typed_configparser-1.0.0-py3-none-any.whl
- Upload date:
- Size: 20.0 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 | 29dd1d2e40f76ecb78fb71cadf9e843745eb9830e51f6ccc7357f915b1a8c4b9 |
|
MD5 | 1448931793621ec16cf58a555aff4229 |
|
BLAKE2b-256 | a707a28468858e787a7de768fce3f6fb839c938700695e37fcc986b6250b4f88 |