typed-configparser


Nametyped-configparser JSON
Version 1.1.0 PyPI version JSON
download
home_page
SummaryA typed configparser
upload_time2024-03-09 05:37:10
maintainer
docs_urlNone
author
requires_python>=3.8
licenseMIT
keywords configparser typed
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <p align="center">
  <img src="https://raw.githubusercontent.com/ajatkj/typed_configparser/main/assets/logo.png" alt="Description of the image">
</p>
<p align="center">
  <a href="https://github.com/ajatkj/typed_configparser/actions?query=workflow%3ATest+event%3Apush+branch%3Amain" target="_blank">
      <img src="https://img.shields.io/github/actions/workflow/status/ajatkj/typed_configparser/tests.yml?branch=main&event=push&style=flat-square&label=test&color=%2334D058" alt="Test">
  </a>
  <a href="https://app.codecov.io/gh/ajatkj/typed_configparser/tree/main/" target="_blank">
      <img src="https://img.shields.io/codecov/c/github/ajatkj/typed_configparser?color=%2334D058&style=flat-square" alt="Coverage">
  </a>
  <a href="https://pypi.org/project/typed-configparser" target="_blank">
      <img src="https://img.shields.io/pypi/v/typed-configparser?color=%2334D058&label=pypi%20package&style=flat-square" alt="Package version">
  </a>
  <a href="https://pypi.org/project/typed-configparser" target="_blank">
      <img src="https://img.shields.io/pypi/pyversions/typed-configparser?color=%2334D058&style=flat-square" alt="Supported Python versions">
  </a>
</p>

# 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.<br />
✓ Use dataclasses to parse the configuration file.<br />
✓ Support for almost all python built-in data types - `int`, `float`, `str`, `list`, `tuple`, `dict` and complex data types using `Union` and `Optional`.<br />
✓ Supports almost all features of dataclasses including field level init flag, **post_init** method, InitVars and more.<br />
✓ Built on top of `configparser`, hence retains all functionalities of `configparser`.<br />
✓ Support for optional values (optional values are automatically set to `None` if not provided).<br />
✓ Smarter defaults (see below).

## Installation

You can install `typed_configparser` using `pip`:

```sh
pip install typed-configparser
```

## Usage

`examples/basic.py`

```py3
# 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)
```

```py3
BASIC(option1=10, option2=value2, option3=5.2, option4=['foo', 'bar'])
```

`examples/unions_and_optionals.py`

```py3
# 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)
```

```py3
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

[MIT License](./LICENSE)

# Contribution

If you are interested in contributing to typed_configparser, please take a look at the [contributing guidelines](./CONTRIBUTING.md).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "typed-configparser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "configparser,typed",
    "author": "",
    "author_email": "Ankit Jain <ajatkj.dev@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/0c/3a/bf80bb8cd2d97e5df46553fe03259c5ca1e8f75cab4682670a329f17554c/typed_configparser-1.1.0.tar.gz",
    "platform": null,
    "description": "<p align=\"center\">\n  <img src=\"https://raw.githubusercontent.com/ajatkj/typed_configparser/main/assets/logo.png\" alt=\"Description of the image\">\n</p>\n<p align=\"center\">\n  <a href=\"https://github.com/ajatkj/typed_configparser/actions?query=workflow%3ATest+event%3Apush+branch%3Amain\" target=\"_blank\">\n      <img src=\"https://img.shields.io/github/actions/workflow/status/ajatkj/typed_configparser/tests.yml?branch=main&event=push&style=flat-square&label=test&color=%2334D058\" alt=\"Test\">\n  </a>\n  <a href=\"https://app.codecov.io/gh/ajatkj/typed_configparser/tree/main/\" target=\"_blank\">\n      <img src=\"https://img.shields.io/codecov/c/github/ajatkj/typed_configparser?color=%2334D058&style=flat-square\" alt=\"Coverage\">\n  </a>\n  <a href=\"https://pypi.org/project/typed-configparser\" target=\"_blank\">\n      <img src=\"https://img.shields.io/pypi/v/typed-configparser?color=%2334D058&label=pypi%20package&style=flat-square\" alt=\"Package version\">\n  </a>\n  <a href=\"https://pypi.org/project/typed-configparser\" target=\"_blank\">\n      <img src=\"https://img.shields.io/pypi/pyversions/typed-configparser?color=%2334D058&style=flat-square\" alt=\"Supported Python versions\">\n  </a>\n</p>\n\n# typed-configparser\n\ntyped-configparser is an extension of the standard configparser module with support for typed configurations using dataclasses.\nIt leverages Python's type hints and dataclasses to provide a convenient way of parsing and validating configuration files.\n\n## Features\n\n\u2713 Fully typed.<br />\n\u2713 Use dataclasses to parse the configuration file.<br />\n\u2713 Support for almost all python built-in data types - `int`, `float`, `str`, `list`, `tuple`, `dict` and complex data types using `Union` and `Optional`.<br />\n\u2713 Supports almost all features of dataclasses including field level init flag, **post_init** method, InitVars and more.<br />\n\u2713 Built on top of `configparser`, hence retains all functionalities of `configparser`.<br />\n\u2713 Support for optional values (optional values are automatically set to `None` if not provided).<br />\n\u2713 Smarter defaults (see below).\n\n## Installation\n\nYou can install `typed_configparser` using `pip`:\n\n```sh\npip install typed-configparser\n```\n\n## Usage\n\n`examples/basic.py`\n\n```py3\n# This is a complete example and should work as is\n\nfrom typing import List\nfrom typed_configparser import ConfigParser\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass BASIC:\n    option1: int\n    option2: str\n    option3: float\n    option4: List[str]\n\n\nconfig = \"\"\"\n[BASIC]\noption1 = 10\noption2 = value2\noption3 = 5.2\noption4 = [foo,bar]\n\"\"\"\n\nparser = ConfigParser()\nparser.read_string(config)\nsection = parser.parse_section(using_dataclass=BASIC)\n\nprint(section)\n```\n\n```py3\nBASIC(option1=10, option2=value2, option3=5.2, option4=['foo', 'bar'])\n```\n\n`examples/unions_and_optionals.py`\n\n```py3\n# This is a complete example and should work as is\n\nfrom typing import List, Union, Optional, Dict, Tuple\nfrom typed_configparser import ConfigParser\nfrom dataclasses import dataclass, field\n\n\n@dataclass\nclass DEFAULT_EXAMPLE:\n    option1: int\n    option2: Union[List[Tuple[str, str]], List[int]]\n    option3: Dict[str, str] = field(default_factory=lambda: {\"default_key\": \"default_value\"})\n    option4: Optional[float] = None\n\n\nconfig = \"\"\"\n[DEFAULT]\noption1 = 20\noption2 = default_value2\n\n[MY_SECTION_1]\noption2 = [10,20]\noption4 = 5.2\n\n[MY_SECTION_2]\noption2 = [(value2a, value2b), (value2c, value2b), (value2c, value2d)]\noption3 = {key: value}\noption4 = none\n\"\"\"\n\nparser = ConfigParser()\nparser.read_string(config)\nmy_section_1 = parser.parse_section(using_dataclass=DEFAULT_EXAMPLE, section_name=\"MY_SECTION_1\")\nmy_section_2 = parser.parse_section(using_dataclass=DEFAULT_EXAMPLE, section_name=\"MY_SECTION_2\")\n\nprint(my_section_1)\nprint(my_section_2)\n```\n\n```py3\nDEFAULT_EXAMPLE(option1=20, option2=[10, 20], option3={'default_key': 'default_value'}, option4=5.2)\nDEFAULT_EXAMPLE(option1=20, option2=[('value2a', 'value2b'), ('value2c', 'value2b'), ('value2c', 'value2d')], option3={'key': 'value'}, option4=None)\n```\n\nCheck `example` directory for more examples.\n\n## Defaults\n\n- `configparser` includes sensible defaults options which allows you to declare a `[DEFAULT]` section in the config file for fallback values.\n- `typed_configparser` goes a step further and allows you to set a final (last) level of defaults at dataclass level.\n\n# License\n\n[MIT License](./LICENSE)\n\n# Contribution\n\nIf you are interested in contributing to typed_configparser, please take a look at the [contributing guidelines](./CONTRIBUTING.md).\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A typed configparser",
    "version": "1.1.0",
    "project_urls": {
        "Repository": "https://github.com/ajatkj/typed_configparser"
    },
    "split_keywords": [
        "configparser",
        "typed"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "07003cc3bc0c450ca30b8f85795129ee704db3231323c5880d7989b9a9c91744",
                "md5": "7cefabaf53630705a3947b0249e761b3",
                "sha256": "07ac120849267129c9228e3bbcb458113a5c3efddb771f7c930b8902ce185acc"
            },
            "downloads": -1,
            "filename": "typed_configparser-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "7cefabaf53630705a3947b0249e761b3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 22131,
            "upload_time": "2024-03-09T05:37:08",
            "upload_time_iso_8601": "2024-03-09T05:37:08.348838Z",
            "url": "https://files.pythonhosted.org/packages/07/00/3cc3bc0c450ca30b8f85795129ee704db3231323c5880d7989b9a9c91744/typed_configparser-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0c3abf80bb8cd2d97e5df46553fe03259c5ca1e8f75cab4682670a329f17554c",
                "md5": "a352cfa7bd9507ee807092176b06aba9",
                "sha256": "0a0ab47965151d095e0ad5f00aeb59a7ef95a5f9b6f045ac8d4417d91ba46d4d"
            },
            "downloads": -1,
            "filename": "typed_configparser-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a352cfa7bd9507ee807092176b06aba9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 27214,
            "upload_time": "2024-03-09T05:37:10",
            "upload_time_iso_8601": "2024-03-09T05:37:10.642506Z",
            "url": "https://files.pythonhosted.org/packages/0c/3a/bf80bb8cd2d97e5df46553fe03259c5ca1e8f75cab4682670a329f17554c/typed_configparser-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-09 05:37:10",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "ajatkj",
    "github_project": "typed_configparser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "typed-configparser"
}
        
Elapsed time: 0.20421s