Skip to main content
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()

Metadata

Release files for arguable 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for arguable 1.0
File Size Uploaded
arguable-1.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for arguable 1.0
File Interpreter ABI Platform
arguable-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.1 kB

Release files / arguable-1.0.tar.gz

Download URL arguable-1.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a1f1d79c62ee05f60c7cbf8d67a8767fefa31758b1d7f43cb0353a262ccb4c83
BLAKE2b-256 checksum
How to use checksums
f53ed72e24ea77ae5fef5bb0c04394f7569e2a013fc948182e697bb418bf7433
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.8 CPython/3.8.3 Linux/5.3.0-1020-azure

Release files / arguable-1.0-py3-none-any.whl

Download URL arguable-1.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3bce116620ca3ab0db81dc416241871c81bcc30bdd5deaf10c9d33a0919fc043
BLAKE2b-256 checksum
How to use checksums
08803285a9d1e3c0b3fff3d52e6b69dab27b65a6ba700a11477bd084b4e0460d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.8 CPython/3.8.3 Linux/5.3.0-1020-azure

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page