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.

Source Distribution

arguable-1.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

arguable-1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file arguable-1.0.tar.gz.

File metadata

  • Download URL: arguable-1.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.8 CPython/3.8.3 Linux/5.3.0-1020-azure

File hashes

Hashes for arguable-1.0.tar.gz
Algorithm Hash digest
SHA256 a1f1d79c62ee05f60c7cbf8d67a8767fefa31758b1d7f43cb0353a262ccb4c83
MD5 2ef5243cadf8dba00d8997fbe36de769
BLAKE2b-256 f53ed72e24ea77ae5fef5bb0c04394f7569e2a013fc948182e697bb418bf7433

See more details on using hashes here.

File details

Details for the file arguable-1.0-py3-none-any.whl.

File metadata

  • Download URL: arguable-1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.8 CPython/3.8.3 Linux/5.3.0-1020-azure

File hashes

Hashes for arguable-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3bce116620ca3ab0db81dc416241871c81bcc30bdd5deaf10c9d33a0919fc043
MD5 5a0ca2bac2ab913227efaa7638595288
BLAKE2b-256 08803285a9d1e3c0b3fff3d52e6b69dab27b65a6ba700a11477bd084b4e0460d

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