Skip to main content

Dataclass extension to use argparse

Project description

Parseable dataclasses

This library provides parse_args method to your dataclass.

Install

pip install git+https://github.com/m5ep12/parseable_dataclasses.git

Examples

Simple dataclass

@parseable_dataclass
@dataclass
class DC:
    a: int
    b: str
    opt: float = 3.141592

or

@parseable_dataclass
class DC:
    a: int
    b: str
    opt: float = 3.141592

and parse

assert, hasattr(DC, "parse_args")
dc = DC.parse_args("1 hello".split())
# dc(a=1, b="hello", opt=3.141592)
dc = DC.parse_args("1 hello 2.71828".split())
# dc(a=1, b="hello", opt=2.71828)

Generate help-text

dc = DC.parse_args(["-h"])
usage: DC [-h] [--opt OPT] a b

positional arguments:
  a           int
  b           str

options:
  -h, --help  show this help message and exit
  --opt OPT   float

Make CLI

from pathlib import Path
from pprint import pprint
from parseable_dataclasses import parseable_dataclass

@parseable_dataclass
class SuperTool:
    src: list[Path]
    verbose: bool = False
    timeout: float = 10.0
    retly: int = 3
    prefix: str = "Hello"

    def __call__(self):
        # implementation your tool
        pprint(self)

if __name__ == "__main__":
    tool = SuperTool.parse_args()
    tool()
$ python examples/make_cli.py -h
usage: SuperTool [-h] [--verbose | --no-verbose] [--timeout TIMEOUT] [--retly RETLY] [--prefix PREFIX] src

positional arguments:
  src                   Path

options:
  -h, --help            show this help message and exit
  --verbose, --no-verbose
                        bool (default: False)
  --timeout TIMEOUT     float (default: 10.0)
  --retly RETLY         int (default: 3)
  --prefix PREFIX       str (default: Hello)

$ python examples/make_cli.py --verbose aaa/bbb.py
SuperTool(src=WindowsPath('aaa/bbb.py'),
          verbose=True,
          timeout=10.0,
          retly=3,
          prefix='Hello')

$ 

Limitations

tuple

Due to my inability, I have not implemented to accept a complex tuple like following. (I'd like to convert this dataclass, but I can't find a nice implementation...)

@parseable_dataclass
@dataclass
class SomeClass:
    a: tuple[int, str, float]

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

parseable_dataclasses-1.0.0.tar.gz (34.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

parseable_dataclasses-1.0.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file parseable_dataclasses-1.0.0.tar.gz.

File metadata

  • Download URL: parseable_dataclasses-1.0.0.tar.gz
  • Upload date:
  • Size: 34.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for parseable_dataclasses-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a8ba90b03a6fdf70fd9469e9961b8122bebf10c38487e53c60ad5d29b4f1ddc9
MD5 2e0793ade6bfacebf6877508bfee2ddd
BLAKE2b-256 2ebd799b38a02a06e120d076dfad143ea1165ca844ecc3c251d7d2ddd1038e0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for parseable_dataclasses-1.0.0.tar.gz:

Publisher: python-publish.yml on m5ep12/parseable_dataclasses

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file parseable_dataclasses-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for parseable_dataclasses-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1276b66a6ff0fd7b6c5f19aba4ef5c67f637bf748f45019797eee01fda06f9de
MD5 065e2a1878a3d85a9f6a1bbe8b9e8fff
BLAKE2b-256 67ccaba5fd9e202f83e1197d5eff487ab9fb491dd513805c655db659e920927d

See more details on using hashes here.

Provenance

The following attestation bundles were made for parseable_dataclasses-1.0.0-py3-none-any.whl:

Publisher: python-publish.yml on m5ep12/parseable_dataclasses

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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