cfgparser


Namecfgparser JSON
Version 1.0.8 PyPI version JSON
download
home_pagehttps://github.com/CaRniFeXeR/PythonConfigParser
SummaryPython package that allows to parse typed configs defined by python dataclasses
upload_time2023-08-15 03:01:44
maintainer
docs_urlNone
authorFlorian Kowarsch
requires_python>=3.7, <4
license
keywords config parser yaml json typed-parsing dataclass lightweight
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # PythonConfigParser


truly fully-typed python configs - not more, not less.

[![PyPI version](https://badge.fury.io/py/cfgparser.svg)](https://badge.fury.io/py/cfgparser)
![Badge Name](https://github.com/CaRniFeXeR/PythonConfigParser/actions/workflows/unittests.yml/badge.svg?branch=main&event=push)
[![codecov](https://codecov.io/gh/CaRniFeXeR/PythonConfigParser/main/graph/badge.svg)](https://codecov.io/gh/CaRniFeXeR/PythonConfigParser)


## Usage

1. define your config structure in a dataclass like

```python
from dataclasses import dataclass
from typing import List

@dataclass
class DummyConfigElement:
    name: str
    list: List[int]
    another_list: List[float]

```

2. initialize the config parser with the path to the module with the config definitions.


```python

parser = ConfigParser(datastructure_module_name="mysrc.datastructures.configs")
```

3. parse a config from file or dict

#### from file 



```python

my_config = parser.parse_from_file("myconfig.json")

```

#### from dictionary

```python

my_config_dict = {"type_name" : "mysrc.datastructures.configs.a", "value" : 1}
my_config = parser.parse(my_config_dict)

```


### Type definition
There are two ways to define the configs type:
- specified in the config itself
    - store fully qualified name of the config class under the key "type_name" in the config
    - e.g in the code example above
- specified when parsing the config
    - if the config has no key "typed_config" set, the type can be specified when parsing the config

```python
from mysrc.datastructures.configs import a
my_config_dict = {"some_key" : "some_value"}
# config has no key "type_name" but type is specified when parsing
my_config = ConfigParser().parse_typed(my_config_dict,a)

```

### Optional & Union fields
By default every field can be explicitly set to None. If you don't what this behavior you can set the flag "allow_none" to False.

```python
    from cfgparser import settings
    settings.allow_none = False
```

#### Union fields

Whenever a Union type is encountered the parser tries to parse the config with the first type in the union. If this fails it tries the next type and so on. If all types fail the parser raises an exception. For instance, in the example below the parser tries to parse the config as a Bird. If this fails it tries to parse it as a Cat. If this fails too the parser raises an exception.

```python

from dataclasses import dataclass
from typing import Union

@dataclass
class Bird:
    name: str
    wingspan: float

@dataclass
class Cat:
    name: str
    paws: int

@dataclass
class AnimalOwnership:
    owner: str
    animal : Union[Bird, Cat]

```

## Features

- ✅ fully typed json and yaml configs
- ✅ nested configs
- ✅ complex union and optional types
- ✅ dict object into typed dataclass
- ✅ support for enums (value given as string or int)
- ✅ support for typed tuples (e.g. Tuple[int, str, float])
- ✅ override type_name if type is specified when parsing



## Installation

```bash
pip install cfgparser
```

## Feature Roadmap

- ⬜ specify config from cli
- ⬜ post hock
- ⬜ distributed configs
- ⬜ typed dicts (e.g. Dict[str, MyType])
- ⬜ typed functions (e.g. torch.functional.relu)
- ⬜ save config to file (json, yaml) with and without typeinfo



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/CaRniFeXeR/PythonConfigParser",
    "name": "cfgparser",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7, <4",
    "maintainer_email": "",
    "keywords": "config,parser,YAML,JSON,typed-parsing,dataclass,lightweight",
    "author": "Florian Kowarsch",
    "author_email": "flo.kowarsch@yahoo.de",
    "download_url": "https://files.pythonhosted.org/packages/71/1d/561fd9a57b11c7acacfbb2b878bf7a7cfa8897190c9914bd34f603921e7a/cfgparser-1.0.8.tar.gz",
    "platform": null,
    "description": "# PythonConfigParser\n\n\ntruly fully-typed python configs - not more, not less.\n\n[![PyPI version](https://badge.fury.io/py/cfgparser.svg)](https://badge.fury.io/py/cfgparser)\n![Badge Name](https://github.com/CaRniFeXeR/PythonConfigParser/actions/workflows/unittests.yml/badge.svg?branch=main&event=push)\n[![codecov](https://codecov.io/gh/CaRniFeXeR/PythonConfigParser/main/graph/badge.svg)](https://codecov.io/gh/CaRniFeXeR/PythonConfigParser)\n\n\n## Usage\n\n1. define your config structure in a dataclass like\n\n```python\nfrom dataclasses import dataclass\nfrom typing import List\n\n@dataclass\nclass DummyConfigElement:\n    name: str\n    list: List[int]\n    another_list: List[float]\n\n```\n\n2. initialize the config parser with the path to the module with the config definitions.\n\n\n```python\n\nparser = ConfigParser(datastructure_module_name=\"mysrc.datastructures.configs\")\n```\n\n3. parse a config from file or dict\n\n#### from file \n\n\n\n```python\n\nmy_config = parser.parse_from_file(\"myconfig.json\")\n\n```\n\n#### from dictionary\n\n```python\n\nmy_config_dict = {\"type_name\" : \"mysrc.datastructures.configs.a\", \"value\" : 1}\nmy_config = parser.parse(my_config_dict)\n\n```\n\n\n### Type definition\nThere are two ways to define the configs type:\n- specified in the config itself\n    - store fully qualified name of the config class under the key \"type_name\" in the config\n    - e.g in the code example above\n- specified when parsing the config\n    - if the config has no key \"typed_config\" set, the type can be specified when parsing the config\n\n```python\nfrom mysrc.datastructures.configs import a\nmy_config_dict = {\"some_key\" : \"some_value\"}\n# config has no key \"type_name\" but type is specified when parsing\nmy_config = ConfigParser().parse_typed(my_config_dict,a)\n\n```\n\n### Optional & Union fields\nBy default every field can be explicitly set to None. If you don't what this behavior you can set the flag \"allow_none\" to False.\n\n```python\n    from cfgparser import settings\n    settings.allow_none = False\n```\n\n#### Union fields\n\nWhenever a Union type is encountered the parser tries to parse the config with the first type in the union. If this fails it tries the next type and so on. If all types fail the parser raises an exception. For instance, in the example below the parser tries to parse the config as a Bird. If this fails it tries to parse it as a Cat. If this fails too the parser raises an exception.\n\n```python\n\nfrom dataclasses import dataclass\nfrom typing import Union\n\n@dataclass\nclass Bird:\n    name: str\n    wingspan: float\n\n@dataclass\nclass Cat:\n    name: str\n    paws: int\n\n@dataclass\nclass AnimalOwnership:\n    owner: str\n    animal : Union[Bird, Cat]\n\n```\n\n## Features\n\n- \u2705 fully typed json and yaml configs\n- \u2705 nested configs\n- \u2705 complex union and optional types\n- \u2705 dict object into typed dataclass\n- \u2705 support for enums (value given as string or int)\n- \u2705 support for typed tuples (e.g. Tuple[int, str, float])\n- \u2705 override type_name if type is specified when parsing\n\n\n\n## Installation\n\n```bash\npip install cfgparser\n```\n\n## Feature Roadmap\n\n- \u2b1c specify config from cli\n- \u2b1c post hock\n- \u2b1c distributed configs\n- \u2b1c typed dicts (e.g. Dict[str, MyType])\n- \u2b1c typed functions (e.g. torch.functional.relu)\n- \u2b1c save config to file (json, yaml) with and without typeinfo\n\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Python package that allows to parse typed configs defined by python dataclasses",
    "version": "1.0.8",
    "project_urls": {
        "Bug Reports": "https://github.com/CaRniFeXeR/PythonConfigParser/issues",
        "Homepage": "https://github.com/CaRniFeXeR/PythonConfigParser",
        "Source": "https://github.com/CaRniFeXeR/PythonConfigParser/"
    },
    "split_keywords": [
        "config",
        "parser",
        "yaml",
        "json",
        "typed-parsing",
        "dataclass",
        "lightweight"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2e5fc82b9ad338b8f285f0e9f1039723bd53dfffa401538e5a2276dacee19abb",
                "md5": "9ffa23b4dd5914577af17b55e5bdd064",
                "sha256": "ad60fc6aa05a12508bf58b72c189651d5f206bd1809f8296cf4211ea8cd0fc07"
            },
            "downloads": -1,
            "filename": "cfgparser-1.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "9ffa23b4dd5914577af17b55e5bdd064",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7, <4",
            "size": 8235,
            "upload_time": "2023-08-15T03:01:42",
            "upload_time_iso_8601": "2023-08-15T03:01:42.525753Z",
            "url": "https://files.pythonhosted.org/packages/2e/5f/c82b9ad338b8f285f0e9f1039723bd53dfffa401538e5a2276dacee19abb/cfgparser-1.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "711d561fd9a57b11c7acacfbb2b878bf7a7cfa8897190c9914bd34f603921e7a",
                "md5": "34d9f02121800b0c3d61e1bb9a83abb0",
                "sha256": "852c1dc1a15a8a04d711f383b1a069f4f2bb77a8aff5e075491243b1fb2e6c9d"
            },
            "downloads": -1,
            "filename": "cfgparser-1.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "34d9f02121800b0c3d61e1bb9a83abb0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7, <4",
            "size": 10271,
            "upload_time": "2023-08-15T03:01:44",
            "upload_time_iso_8601": "2023-08-15T03:01:44.005174Z",
            "url": "https://files.pythonhosted.org/packages/71/1d/561fd9a57b11c7acacfbb2b878bf7a7cfa8897190c9914bd34f603921e7a/cfgparser-1.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-15 03:01:44",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "CaRniFeXeR",
    "github_project": "PythonConfigParser",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "cfgparser"
}
        
Elapsed time: 0.09879s