Skip to main content

argparse wrapper to allow hierarchically nested class based parameters

Project description

Python argparse wrapper to allow hierarchically nested class based parameters suitable for automatic code analysis especially in Pycharm. It also provides a unified parameter specification protocol that can be used to provide parameter values through both text files and command line.

Please refer to the included parameter skeleton of a large multi object tracking project for which the functionality included in this package was originally developed. It provides an excellent example of a highly modular project with deeply nested and shared modules.

For example, this is one of the deeper instances of module nesting in this example:

Params->Trainer->Target->Templates->Siamese->SiamFC->DesignParams

Parameter for this configuration can be provided as:

trainer.target.templates.siamese.siamfc.design.exemplar_sz=150

Specifying multiple parameters for a deeply nested module can quickly become cumbersome especially from command line. The package thus provides a way to group parameters from the same module using the @ identifier. An example is provided in example/cfg/params.cfg. Note that the indentation used in this file is only for ease of human parsing and is not needed as this system of grouping also works from command line. Example commands are in example/commands.md.

The @ identifier specifies a prefix pf to be added to all subsequent arguments so that arg_name is treated as pf.arg_name. Assuming pf = name1.name2, following flavors are supported:

@name: pf = name
@: pf = <empty>

@@name: pf = name1.name2.name

@@@name: pf = name1.name
@@@: pf = name1

Usage of the package is very simple and involves calling paramparse.process as demonstrated in example/main.py.

It also provides three converter functions from_parser, from_dict and from_function that can create a parameter class compatible with this package from existing parameters in argparse.ArgumentParser and dict formats or using a function's keyword arguments. The generated class code is either writen to a python source file whose name can be specified as the second argument (defaults to Params.py) or copied to clipboard if to_clipboard=1 is provided (requires pyperclip).

The process function does type inference from the default value of each param but also supports extracting the type from restructuredText/pycharm type docstring (as generated by the converter functions) if the default is None.

Note : paramparse uses the reserved parameter cfg to specify paths to text files containing parameter values. If an existing argparse or dict object to be converted into paramparse class already has a cfg field used for some other purpose, it will conflict with the parser so please rename this field before or after converting but before running paramparse.process.

Usage of converter functions is demonstrated in example/utils_demo.py

Run python3 main.py --h from the example folder to see the hierarchical help documentation generated by argparse.

Apart from the hierarchical nesting and parameter grouping, an important utility of paramparse is in the class based representation that allows automated code analysis, navigation and refactoring in IDEs like Pycharm that is not possible when using vanilla argparse.ArgumentParser or dict.

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

paramparse-0.7.7.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

paramparse-0.7.7-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file paramparse-0.7.7.tar.gz.

File metadata

  • Download URL: paramparse-0.7.7.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for paramparse-0.7.7.tar.gz
Algorithm Hash digest
SHA256 e3c1bbcd9178c43ee431ba8e4eda606c2585a0061de794ac0c3987dae53f6b78
MD5 6063d0aca03cd7c739c92dc3c91c5157
BLAKE2b-256 2e50b61f1c22ea2d1e721df2a6e701ab0b7b5c6778e18f67fcfac204a092d348

See more details on using hashes here.

File details

Details for the file paramparse-0.7.7-py3-none-any.whl.

File metadata

  • Download URL: paramparse-0.7.7-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for paramparse-0.7.7-py3-none-any.whl
Algorithm Hash digest
SHA256 7add3a61fc47c5b10e193e4724e148d46c8aff093af69cc4163d1370590d8f11
MD5 a92c30d94756bea576e5d23e9ed4daf3
BLAKE2b-256 3a05df77914aa64fe1f94ecd2f6a78fd3273d86b5f9260beb52f748a6b70f318

See more details on using hashes here.

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