Skip to main content

An HParams implementation with argparse.

Project description

Argparse HParams

A simple argument parser for hyper-parameters.

Installation

From PyPI:

pip install argparse-hparams

From Github:

pip install git+https://github.com/enhuiz/argparse-hparams.git

Example

from argparse_hparams import dataclass, HParams, Annotated, Flag

@dataclass
class HParamsTest(HParams):
    # This will call: parser.add_argument(type=int, default=0)
    x: int = 0

    # You may annotate custom data for parser.add_argument(...).
    # The annotated default will overwrite dataclass default
    # This will call: parser.add_argument(type=str.upper, default='bad')
    y: Annotated[str, dict(type=str.upper, default="bad")] = "good"

    # This will call: parser.add_argument(type=int, nargs=2, default=(2, 3))
    pair: Annotated[tuple[int, int], dict(type=int, nargs=2)] = (2, 3)

    # This will call: parser.add_argument(action="store_true", default=False)
    ok: Flag = False

    # when positional is set, the argument is a positional argument
    pos: Annotated[str, dict(positional=True)] = ""


if __name__ == "__main__":
    test = HParamsTest()
    test.show()
$ python example.py
usage: example.py [-h] [--x int] [--y upper] [--pair int int] [--ok] [--default Path] str
example.py: error: the following arguments are required: pos

$ python example.py 123
┌────────────┐
│  HParams   │
├────────────┤
│x: 0        │
│y: BAD      │
│pair: (2, 3)│
│ok: False   │
│pos: 123    │
└────────────┘

$ python example.py --default default.yml 123
┌────────────┐
│  HParams   │
├────────────┤
│x: 0        │
│y: 1        │
│pair: [4, 5]│
│ok: False   │
│pos: 123    │
└────────────┘

$ python example.py --help
usage: example.py [-h] [--x int] [--y upper] [--pair int int] [--ok] [--default Path] str

positional arguments:
  str             pos

optional arguments:
  -h, --help      show this help message and exit
  --x int         x (default: 0)
  --y upper       y (default: bad)
  --pair int int  pair (default: (2, 3))
  --ok            ok (default: False)
  --default Path  A YAML configuration file that overrides the defaults (default: None)

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

Built Distribution

File details

Details for the file argparse-hparams-0.0.1.dev20211128164602.tar.gz.

File metadata

  • Download URL: argparse-hparams-0.0.1.dev20211128164602.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for argparse-hparams-0.0.1.dev20211128164602.tar.gz
Algorithm Hash digest
SHA256 aa9edf2cf823315091388bab965c9d64939d6870e5b786d4bc4aa9b2597f6670
MD5 5a61fbc76fd75a20a7d5a3053c63d089
BLAKE2b-256 c9e2bf7dba13e309b2a31a47123ddfb6397669d16f37456e75af980a861a6d5d

See more details on using hashes here.

File details

Details for the file argparse_hparams-0.0.1.dev20211128164602-py3-none-any.whl.

File metadata

  • Download URL: argparse_hparams-0.0.1.dev20211128164602-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for argparse_hparams-0.0.1.dev20211128164602-py3-none-any.whl
Algorithm Hash digest
SHA256 38d0e52eef10d211dcd5104b003966042f37f90d83be43aa5219c7b25d0c24c4
MD5 5f067a6b45818d49d8046129050c61cc
BLAKE2b-256 f4139164f51f85fb6cba2f5e776ee03d602b1a77b450bfcf847851258eeb0d92

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