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.1.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

typed_configparser-1.0.1-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

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

Hashes for typed_configparser-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b3c09099a10d1487ca5ecf7033155d95015ffffaf9da5f8c0019d76b3cc4fa21
MD5 ebbec3dfd7a72011fdb418c12a3ab1f8
BLAKE2b-256 70eab430a2005482547f66e26e063554f0c7a5b0b1376fcd1c2922761f30cad1

See more details on using hashes here.

File details

Details for the file typed_configparser-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for typed_configparser-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8e719a2c25db5d36283c28e43931ef26184ea569678d708439e6e24f52d8c20c
MD5 ce3e5e24bdeef071c885d6f541fafa48
BLAKE2b-256 7f7486a7a10dc559a4b7469c5b8d2f8f5b1151fc692398d8fde91872cf0052cc

See more details on using hashes here.

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