Skip to main content

A command line argument parsing utility using python class-based namespace for better IDE static auto-completion

Project description

Todo

Done

  • [x] publish

  • [x] add test

Installation

pip install params_proto

Usage

To use a python namespace to declare commandline argments

Note that this is python >= 3.6 only. The reason being the annotation syntax is only supported by python 3.6 an above. If you feel unhappy about this, just use javascript.

For python <= 3.5, you can declare each default value as a tuple. param_proto recognizes the python version that is being ran, and changes its behavior.

Below is a simple example how to use python namespace to declare command line arguments:

from .params_proto import cli_parse, is_hidden, ParamsProto, proto_signature


def test_cli_proto():
    @cli_parse
    class G(ParamsProto):
        """Supervised MAML in tensorflow"""
        npts: "number of points to sample from distribution" = 100
        num_epochs: "number of epochs to train" = 70000
        num_tasks: "number of tasks in the inner loop" = 10
        num_grad_steps: "number of gradient descent steps in the inner loop" = 1
        num_points_sampled: "effectively the k-shot" = 10
        fix_amp: "controls the sampling, fix the amplitude of the sample distribution if True" = False

    assert G.npts == 100
    G.npts = 10
    assert G.npts == 10
    assert vars(G) == {'npts': 10, 'num_epochs': 70000, 'num_tasks': 10, 'num_grad_steps': 1,
                       'num_points_sampled': 10, 'fix_amp': False}
    assert G._proto is not None, '_proto should exist'

Setting Function Signatures using Python Namespace

sometimes, you have a function with wildcard keyword argument signature. It is annoying to work with such functions because the static type analysis of the IDE doesn’t tell you what needs to go in.

I originally wrote this decorator to help with that case, however the dynamically set function signature won’t show up in the IDE in general. Use this for inspection purposes if you like.

Below si the usage example and the test case:

def test_proto_signature():
    @cli_parse
    class G(ParamsProto):
        """some parameter proto"""
        npts: "number of points to sample from distribution" = 100

    @proto_signature(G._proto)
    def main_demo(**kwargs):
        print('npts = ', kwargs['npts'])
        return kwargs['npts']

    # First way is to use proto_signature decorator. The dynamically generated signature
    # however does not show up in pyCharm. It does however, show during run time.
    import inspect

    assert main_demo(npts=10) == 10
    print("main_demo<Function> signature:", inspect.signature(main_demo))
    assert str(inspect.signature(main_demo)) == "(npts=100)"

To Develop

git clone https://github.com/episodeyang/params_proto.git
cd params_proto
make dev

To test, run

make test

This make dev command should build the wheel and install it in your current python environment. Take a look at the https://github.com/episodeyang/params_proto/blob/master/Makefile for details.

To publish, first update the version number, then do:

make publish

Project details


Release history Release notifications | RSS feed

This version

1.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

params_proto-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file params_proto-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for params_proto-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 996599832a414506029e1afbe57f8e814389f3a4b8432c610743414f6b93ea84
MD5 454a0e0231264d5dd56348014b894b3f
BLAKE2b-256 43c86f339e34530d0a89872e58aa7cb30b686e1d5ad3efb5e43c44a85a0dfa87

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