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
configparserincludes sensible defaults options which allows you to declare a[DEFAULT]section in the config file for fallback values.typed_configparsergoes 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typed_configparser-1.0.1.tar.gz.
File metadata
- Download URL: typed_configparser-1.0.1.tar.gz
- Upload date:
- Size: 22.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3c09099a10d1487ca5ecf7033155d95015ffffaf9da5f8c0019d76b3cc4fa21
|
|
| MD5 |
ebbec3dfd7a72011fdb418c12a3ab1f8
|
|
| BLAKE2b-256 |
70eab430a2005482547f66e26e063554f0c7a5b0b1376fcd1c2922761f30cad1
|
File details
Details for the file typed_configparser-1.0.1-py3-none-any.whl.
File metadata
- Download URL: typed_configparser-1.0.1-py3-none-any.whl
- Upload date:
- Size: 20.1 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 |
8e719a2c25db5d36283c28e43931ef26184ea569678d708439e6e24f52d8c20c
|
|
| MD5 |
ce3e5e24bdeef071c885d6f541fafa48
|
|
| BLAKE2b-256 |
7f7486a7a10dc559a4b7469c5b8d2f8f5b1151fc692398d8fde91872cf0052cc
|