Skip to main content

class-level configuration via argparse

Project description

https://github.com/Dominik1123/arguable/workflows/Tests/badge.svg https://codecov.io/gh/Dominik1123/arguable/branch/master/graph/badge.svg https://img.shields.io/pypi/v/arguable.svg

Arguable

“arguable” stands for “argparse configurable”. This project enables class-level configuration via argparse and the command line interface.

Installation

pip install arguable

Usage

Classes inheriting from arguable.Arguable define a class-level argparse.Namespace object config which holds relevant parameters and their defaults. The corresponding argument parser is created automatically from these parameters and is stored as the config_parser class attribute.

For example:

from argparse import Namespace
from arguable import Arguable


class Example(Arguable):
    config = Namespace(language='en')


print(Example().config.language)

When the class is created, the Example.config_parser attribute holds an argument parser with an argument --example-language with type=str and default='en'.

When the class is instantiated it automatically configures its instances by assigning an instance-level attribute config which picks up the provided command line arguments and shadows the class-level config defaults.

So invoking the above script via python example.py --example-language=de prints de while omitting the parameter uses the default en.

Parameter types

Other supported parameter types are sequences (e.g. tuples) which will use the following argument definition:

# `par` is the tuple parameter default.
parser.add_argument(name, type=type(par[0]), nargs=len(par), default=par)

bool:

# `par` is the bool parameter default.
parser.add_argument(name, action=f'store_{"false" if par else "true"}')

and of course all the numeric types.

Customization

By default the class name in lowercase will be used as a prefix for parameter names. This can be configured by two class variables:

  • config_prefix – If provided then this is used directly as the prefix.
  • config_removesuffix – If config_prefix is not provided, the class name in lowercase is used and reduced by this suffix (if provided).

These variables can also be provided during class creation without the config_ prefix, e.g.:

class ExampleA(Arguable, prefix='test'):
    config_prefix = 'text'  # similar


class ExampleB(Arguable, removesuffix='b'):
    config_removesuffix = 'b'  # similar

Showing the full help text

In order to make the application aware of all registered parameters, the Arguable class has a super_parser attribute which is an instance of argparse.ArgumentParser. Whenever a class registers new parameters, not only the class-level parser config_parser gets updated, but also the application-level super_parser. This parser can be used to show the help text involving all parameters.

This package provides a helper parser class, arguable.ArgumentParser, which automatically falls back on super_parser when --help is requested.

This super_parser can also be used used to verify the correctness of all given parameters. Since every class is only concerned with their own parameters, excess arguments are simply ignored (using parse_known_args). Using the super_parser one can verify that all provided arguments are supported by the application:

from arguable import Arguable


if __name__ == '__main__':
    # The following will report any excess arguments.
    Arguable.super_parser.parse_args()

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Files for arguable, version 1.0
Filename, size File type Python version Upload date Hashes
Filename, size arguable-1.0-py3-none-any.whl (5.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size arguable-1.0.tar.gz (5.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page