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
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 │
└──────────────────────────────────────┘
Project details
Release history Release notifications | RSS feed
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.dev20211204142354.tar.gz
.
File metadata
- Download URL: argparse-hparams-0.0.1.dev20211204142354.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6289704cf1096c5f703cf1b85fd357389a82d3dd431f361223972a3fe11b7353 |
|
MD5 | c98431f5e2d29c809f2417fa323d9973 |
|
BLAKE2b-256 | 179617b773eb44e875b8fda62c9416af50b068a9fc1e1eefa2bafffbe2a7811f |
File details
Details for the file argparse_hparams-0.0.1.dev20211204142354-py3-none-any.whl
.
File metadata
- Download URL: argparse_hparams-0.0.1.dev20211204142354-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bccc7ada38b7401c9a2c1791339253482f1a1542d3b6e26d72154fc5344234f0 |
|
MD5 | fb5c3d394e3d68ab8643d3d9dd18aa07 |
|
BLAKE2b-256 | 66e31266c084bb8186434b21d4493fc92ed08015879ec30a6ea3dd03d3f5448c |