Skip to main content

A typed configparser

Project description

Description of the image

Test Package version Supported Python versions

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

MIT 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

typed_configparser-1.0.0.tar.gz (21.8 kB view hashes)

Uploaded Source

Built Distribution

typed_configparser-1.0.0-py3-none-any.whl (20.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page