Skip to main content

Powerful parameter processing.

Project description

pyparam

pypi pypi codacy quality codacy quality docs github action pyver

Powerful parameter processing

Features

  • Command line argument parser (with subcommand support)
  • Rich type support, including py, json, namespace, etc.
  • Type overwriting for parameters from command line
  • Arbitrary parsing arguments from command line
  • Automatic help page assembling
  • Help page customization
  • Callbacks for option values
  • Parameter loading from configuration files

Installation

pip install -U pyparam

Documentation

https://pwwang.github.io/pyparam/

Basic usage

example.py

from rich import print
from pyparam import Params
# program name, otherwise sys.argv[0]
params = Params(prog='pyparam', desc="An example for {prog}")
# adding parameters
params.add_param('i, int', type=int,
                 desc="An integer argument.")
params.add_param('float', default=0.1, # type float implied
                 desc="A float argument.")
params.add_param('str', type=str,
                 desc="A str argument.")
params.add_param('flag', type=bool,
                 desc="A flag argument.")
params.add_param('c,count', type='count',
                 desc="A count argument.")
params.add_param('a', type='auto', type_frozen=False,
                 desc="Value will be automatically casted.")
params.add_param('py', type='py',
                 desc="Value will be evaluated by `ast.literal_eval`.")
params.add_param('json', type='json',
                 desc="Value will be converted using `json.loads`.")
params.add_param('list', type='list',
                 desc="Values will be accumulated.")
params.add_param('path', type='path', required=True,
                 desc="Value will be casted into `pathlib.Path`.",
                 callback=( # check if path exists
                     lambda path: ValueError('File does not exist.')
                     if not path.exists() else path
                 ))
params.add_param('choice', type='choice', default='medium',
                 choices=['small', 'medium', 'large'],
                 desc="One of {choices}.")
params.add_param('config.ncores', default=1, # namespace config implied
                 argname_shorten=False,
                 desc='Number of cores to use.')

print(vars(params.parse()))

Try it out:

$ python example.py

help

$ python example.py \
    -i2 \
    --float 0.5 \
    --str abc \
    -ccc \
    -a:int 1 \
    --py "{1,2,3}" \
    --json "{\"a\": 1}" \
    --list 1 2 3 \
    --choice large \
    --path . \
    --config.ncores 4
{
    'i': 2,
    'int': 2,
    'float': 0.5,
    'str': 'abc',
    'flag': False,
    'c': 3,
    'count': 3,
    'a': 1,
    'py': {1, 2, 3},
    'json': {'a': 1},
    'list': [1, 2, 3],
    'path': PosixPath('.'),
    'choice': 'large',
    'config': Namespace(ncores=4)
}

Try more features with:

$ python -m pyparam

TODO

  • Shell completion.

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

pyparam-0.3.2.tar.gz (28.1 kB view details)

Uploaded Source

Built Distribution

pyparam-0.3.2-py3-none-any.whl (29.5 kB view details)

Uploaded Python 3

File details

Details for the file pyparam-0.3.2.tar.gz.

File metadata

  • Download URL: pyparam-0.3.2.tar.gz
  • Upload date:
  • Size: 28.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.3.0-1035-azure

File hashes

Hashes for pyparam-0.3.2.tar.gz
Algorithm Hash digest
SHA256 22830cda02ec49efd2c80d9a545190b8662d74da713b0a99be1c9e1c1587d9f5
MD5 e547dd84f34c076bfc98037d97021f2c
BLAKE2b-256 e447e31028b55d159b799c43b0784b3ce9249cbd6a49a788d461360570a708ed

See more details on using hashes here.

File details

Details for the file pyparam-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: pyparam-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 29.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.10 CPython/3.8.5 Linux/5.3.0-1035-azure

File hashes

Hashes for pyparam-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ab1ca69988543d0db91b210a2cf659b779fd6d3b3beb043b1abbef3f312565bf
MD5 f026c4556c9164dc24c8e5e069cffa75
BLAKE2b-256 cc477dc71b513266c6c1c801250e1dbb5d6c1a3facfaf8c658ebe9b8d54887f6

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