Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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
from dataclasses import dataclass, field


@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 instead of an option
    # i.e., there will be no '--' added before the name when calling parser.add_argument
    pos: Annotated[str, dict(positional=True)] = ""

    # Dict is parsed using json.loads
    payload: dict = field(default_factory=dict)


if __name__ == "__main__":
    # Priority: dataclass default < annotated default < YAML default < manually specified value (sys.argv)
    test = HParamsTest()
    test.show(sort=True)
$ python example.py
usage: example.py [-h] [--x int] [--y upper] [--pair int int] [--ok] [--payload dict_parser] [--default Path] str
example.py: error: the following arguments are required: pos

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

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

$ python example.py --help
usage: example.py [-h] [--x int] [--y upper] [--pair int int] [--ok] [--payload dict_parser] [--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)
  --payload dict_parser
                        payload (default: {})
  --default Path        A YAML configuration file that overrides the defaults (default: None)

$ python example.py --payload '{"oops": "it is overwriten."}' --default default.yml 123
┌──────────────────────────────────────┐
│               HParams                │
├──────────────────────────────────────┤
│ok: False                             │
│pair: [4, 5]                          │
│payload: {'oops': 'it is overwriten.'}│
│pos: 123                              │
│x: 0                                  │
│y: 1                                  │
└──────────────────────────────────────┘

Metadata

Release files for argparse-hparams 0.0.1.dev20220103160002

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argparse-hparams 0.0.1.dev20220103160002
File Size Uploaded Provenance Actions
argparse-hparams-0.0.1.dev20220103160002.tar.gz 5.8 kB Without provenance Details for argparse-hparams-0.0.1.dev20220103160002.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for argparse-hparams 0.0.1.dev20220103160002
File Interpreter ABI Platform Actions
argparse_hparams-0.0.1.dev20220103160002-py3-none-any.whl Python 3 none any Details for argparse_hparams-0.0.1.dev20220103160002-py3-none-any.whl

Total release size: 12.4 kB

Release files / argparse-hparams-0.0.1.dev20220103160002.tar.gz

Download URL argparse-hparams-0.0.1.dev20220103160002.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
29aabaa1f55738baa795035024dd509ccf78c340d6f9e84137c913f3847521ba
BLAKE2b-256 checksum
How to use checksums
2f2394a9c6ba9bbc5513c015a9609e5edf07e88ba6b2da4351a3d6097a90073e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Release files / argparse_hparams-0.0.1.dev20220103160002-py3-none-any.whl

Download URL argparse_hparams-0.0.1.dev20220103160002-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6df35b4985638b02deb119a86774e66699adc9f440a3d1746d30c1ce7f0c3099
BLAKE2b-256 checksum
How to use checksums
f4e503c2b7bc5ad111d10d2e05fc4ed58167a9edfd17455aca11fb803123b006
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

Security for argparse-hparams 0.0.1.dev20220103160002

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page