t2-yada


Namet2-yada JSON
Version 1.2.2 PyPI version JSON
download
home_pagehttps://github.com/binh-vu/yada
SummaryYet another dataclass argparse
upload_time2023-12-22 07:16:57
maintainer
docs_urlNone
authorBinh Vu
requires_python>=3.8,<4.0
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Yada

![PyPI](https://img.shields.io/pypi/v/t2-yada)
![Python](https://img.shields.io/badge/python-v3.8+-blue.svg)
[![GitHub Issues](https://img.shields.io/github/issues/binh-vu/yada.svg)](https://github.com/binh-vu/yada/issues)
![Contributions welcome](https://img.shields.io/badge/contributions-welcome-orange.svg)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)

Yada (**Y**et **A**nother **D**ataclass **A**rgument Parser!) is a library to automatically generate `argparse.ArgumentParser` given data classes. Compared to some available options such as: [Huggingface's HfArgumentParser](https://huggingface.co/transformers/v4.2.2/_modules/transformers/hf_argparser.html), [argparse_dataclass](https://github.com/mivade/argparse_dataclass), and [tap](https://github.com/swansonk14/typed-argument-parser), it offers the following benefits:

1. Static Type Checking
2. Nested data classes and complex types
3. Easy to extend and customize the parser
4. Generate command line arguments given the data classes.

## Installation

Install via PyPI (requires Python 3.8+):

```bash
pip install t2-yada
```

## How to use

Yada's parser can be constructed from data classes. It relies on fieds' annotated types to construct correct argument parsers.

```python
import yada
from dataclasses import dataclass
from typing import *

@dataclass
class CityArgs:
    city: Literal["LA", "NY"]


@dataclass
class NestedArgs:
    name: str
    nested: CityArgs

parser = yada.YadaParser(NestedArgs)
args = parser.parse_args()  # or use parser.parse_known_args() -- the two functions are similar to argparse.parse_args or argparse.parse_known_args
```

Note: YadaParser is annotated as a generic type: `YadaParser[C, R]` where C denotes the classes, and R denotes the instance of the classes created from the arguments. Therefore, in the above example, C is inferred as NestedArgs, but R is unknown, hence the type of `args` variable is unknown. To overcome this typing limitation, Yada provides several options for up to 10 data classes (`yada.Parser1`, `yada.Parser2`, ...). Below is two examples:

```python
parser = yada.Parser1(NestedArgs)
args = parser.parse_args()  # <-- args now has type NestedArgs
```

```python
parser = yada.Parser2((NestedArgs, CityArgs))
args = parser.parse_args()  # <-- args now has type Tuple[NestedArgs, CityArgs]
```

Note: we recommend to use one of the specific parsers `yada.Parser<N>` instead of the generic `yada.YadaParser` if possible as they provide strong typing support.

### Configuring Yada

<details>
<summary>Add help message</summary>

Yada reads the help message from the `key` property of `dataclasses.Field.metadata`

```python
import yada
from dataclasses import dataclass, field
from typing import *

@dataclass
class CityArgs:
    city: Literal["LA", "NY"] = field(metadata={"help": "city's which you want to get the timezone"})

parser = yada.Parser1(CityArgs)
```

</details>


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/binh-vu/yada",
    "name": "t2-yada",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Binh Vu",
    "author_email": "binh@toan2.com",
    "download_url": "https://files.pythonhosted.org/packages/22/b2/645073e93f28f27dfff59f16a77117793c42da63ca3abd63626b0fc89cf7/t2_yada-1.2.2.tar.gz",
    "platform": null,
    "description": "# Yada\n\n![PyPI](https://img.shields.io/pypi/v/t2-yada)\n![Python](https://img.shields.io/badge/python-v3.8+-blue.svg)\n[![GitHub Issues](https://img.shields.io/github/issues/binh-vu/yada.svg)](https://github.com/binh-vu/yada/issues)\n![Contributions welcome](https://img.shields.io/badge/contributions-welcome-orange.svg)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n\nYada (**Y**et **A**nother **D**ataclass **A**rgument Parser!) is a library to automatically generate `argparse.ArgumentParser` given data classes. Compared to some available options such as: [Huggingface's HfArgumentParser](https://huggingface.co/transformers/v4.2.2/_modules/transformers/hf_argparser.html), [argparse_dataclass](https://github.com/mivade/argparse_dataclass), and [tap](https://github.com/swansonk14/typed-argument-parser), it offers the following benefits:\n\n1. Static Type Checking\n2. Nested data classes and complex types\n3. Easy to extend and customize the parser\n4. Generate command line arguments given the data classes.\n\n## Installation\n\nInstall via PyPI (requires Python 3.8+):\n\n```bash\npip install t2-yada\n```\n\n## How to use\n\nYada's parser can be constructed from data classes. It relies on fieds' annotated types to construct correct argument parsers.\n\n```python\nimport yada\nfrom dataclasses import dataclass\nfrom typing import *\n\n@dataclass\nclass CityArgs:\n    city: Literal[\"LA\", \"NY\"]\n\n\n@dataclass\nclass NestedArgs:\n    name: str\n    nested: CityArgs\n\nparser = yada.YadaParser(NestedArgs)\nargs = parser.parse_args()  # or use parser.parse_known_args() -- the two functions are similar to argparse.parse_args or argparse.parse_known_args\n```\n\nNote: YadaParser is annotated as a generic type: `YadaParser[C, R]` where C denotes the classes, and R denotes the instance of the classes created from the arguments. Therefore, in the above example, C is inferred as NestedArgs, but R is unknown, hence the type of `args` variable is unknown. To overcome this typing limitation, Yada provides several options for up to 10 data classes (`yada.Parser1`, `yada.Parser2`, ...). Below is two examples:\n\n```python\nparser = yada.Parser1(NestedArgs)\nargs = parser.parse_args()  # <-- args now has type NestedArgs\n```\n\n```python\nparser = yada.Parser2((NestedArgs, CityArgs))\nargs = parser.parse_args()  # <-- args now has type Tuple[NestedArgs, CityArgs]\n```\n\nNote: we recommend to use one of the specific parsers `yada.Parser<N>` instead of the generic `yada.YadaParser` if possible as they provide strong typing support.\n\n### Configuring Yada\n\n<details>\n<summary>Add help message</summary>\n\nYada reads the help message from the `key` property of `dataclasses.Field.metadata`\n\n```python\nimport yada\nfrom dataclasses import dataclass, field\nfrom typing import *\n\n@dataclass\nclass CityArgs:\n    city: Literal[\"LA\", \"NY\"] = field(metadata={\"help\": \"city's which you want to get the timezone\"})\n\nparser = yada.Parser1(CityArgs)\n```\n\n</details>\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Yet another dataclass argparse",
    "version": "1.2.2",
    "project_urls": {
        "Homepage": "https://github.com/binh-vu/yada",
        "Repository": "https://github.com/binh-vu/yada"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d2e0dbf63729a6499f6fc82b497764f28b40eea9c1edeaab4f94e0289f08b4da",
                "md5": "4437159018d775fc5c9807c5279ef4a1",
                "sha256": "b82752a094435aa33b202ef6491eefd5fc1152957163969d2c1df15a8d879d3f"
            },
            "downloads": -1,
            "filename": "t2_yada-1.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4437159018d775fc5c9807c5279ef4a1",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 10986,
            "upload_time": "2023-12-22T07:16:55",
            "upload_time_iso_8601": "2023-12-22T07:16:55.836205Z",
            "url": "https://files.pythonhosted.org/packages/d2/e0/dbf63729a6499f6fc82b497764f28b40eea9c1edeaab4f94e0289f08b4da/t2_yada-1.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "22b2645073e93f28f27dfff59f16a77117793c42da63ca3abd63626b0fc89cf7",
                "md5": "7ad467b5fa1fe6a1be1cf58030ae2db1",
                "sha256": "0ccded189d9c4940128bc40b8e964784197410dd8f8355ead38d64e3619ab000"
            },
            "downloads": -1,
            "filename": "t2_yada-1.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "7ad467b5fa1fe6a1be1cf58030ae2db1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 10320,
            "upload_time": "2023-12-22T07:16:57",
            "upload_time_iso_8601": "2023-12-22T07:16:57.385229Z",
            "url": "https://files.pythonhosted.org/packages/22/b2/645073e93f28f27dfff59f16a77117793c42da63ca3abd63626b0fc89cf7/t2_yada-1.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-22 07:16:57",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "binh-vu",
    "github_project": "yada",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "t2-yada"
}
        
Elapsed time: 0.95170s