Skip to main content

Upstride argument parser

License: MIT Build Status

This package provides a simple and efficient argument parser for every python projects. The idea is to define once all the arguments and then be able to parse them from the command line or configuration files

Example

Let's start with a simple example to demonstrate how it works :

let's create a file app.py containing :

import upstride_argparse as argparse

arguments = [
    [int, "batch_size", 128, 'The size of batch per gpu', lambda x: x > 0],
    [bool, "cpu", False, 'run on cpu'],
    [str, 'description', '', 'description of the experiment'],
    [float, "lr", 0.0001, 'learning rate', lambda x: x > 0],
    ['list[int]', "raw_size", [256, 256, 3], 'raw shape of one image'],
    ['list[str]', "yaml_config", [], "config files"]
]

config = argparse.parse_cmd(arguments)
print(config)

so as you can see, the arguments are defined using a standard python list containing lists of [type, name, default, help, condition]

  • type can be a python type (int, bool, str, float) or a string list[python_type] for processing lists
  • condition is a function called when the parameters will be parsed. if one parameter doesn't respect the condition, an exception will be raised.

Now let's try to call this program

  • python app.py prints {'batch_size': 128, 'cpu': False, 'description': '', 'lr': 0.0001, 'raw_size': [256, 256, 3], 'yaml_config': []}. This dictionary contains the default configuration
  • python app.py --cpu --lr 0.1 --description hello --raw_size 28 28 1 prints {'batch_size': 128, 'cpu': True, 'description': 'hello', 'lr': 0.1, 'raw_size': [28, 28, 1], 'yaml_config': []}

now lets create a yaml file config.yml containing :

batch_size: 16
cpu: true
  • python app.py --yaml_config config.yml prints {'batch_size': 16, 'cpu': True, 'description': '', 'lr': 0.0001, 'raw_size': [256, 256, 3], 'yaml_config': []}
  • python app.py --yaml_config config.yml --cpu false prints {'batch_size': 16, 'cpu': False, 'description': '', 'lr': 0.0001, 'raw_size': [256, 256, 3], 'yaml_config': []}

as you can see, the command line has the priority over the configuration file

It is also possible to split the configuration between as many configuration file as you want

Namespaces

For larger project, it can become useful to define namespaces to organized the configuration. This can be done like this :

import upstride_argparse as argparse

arguments = [
    [int, "batch_size", 128, 'The size of batch per gpu', lambda x: x > 0],
    ['list[str]', "yaml_config", [], "config files"],
    ['namespace', 'first_namespace', [
        [str, 'arg1', 'hello', 'first argument'],
        ['namespace', 'second_namespace', [
            [bool, "i_am_not_doing_anything", True, ''],
            [bool, "nether_do_i", False, '']
        ]],
    ]],
]

config = argparse.parse_cmd(arguments)
print(config)
  • calling python app.py will print {'batch_size': 128, 'yaml_config': [], 'first_namespace': {'arg1': 'hello', 'second_namespace': {'i_am_not_doing_anything': True, 'nether_do_i': False}}}

variable from namespace can be configure from yaml config file this way :

batch_size: 16
first_namespace:
  arg1: world
  second_namespace:
    i_am_not_doing_anything: false
    nether_do_i: true
  • calling python app.py --yaml_config config.yml will print {'batch_size': 16, 'yaml_config': [], 'first_namespace': {'arg1': 'world', 'second_namespace': {'i_am_not_doing_anything': False, 'nether_do_i': True}}}

and these variables can be setup from the command line like this : python app.py --yaml_config config.yml --first_namespace.arg1 bob --first_namespace.second_namespace.i_am_not_doing_anything false

it will print : {'batch_size': 16, 'yaml_config': [], 'first_namespace': {'arg1': 'bob', 'second_namespace': {'i_am_not_doing_anything': False, 'nether_do_i': True}}}

Metadata

Release files for upstride-argparse 1.0.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 upstride-argparse 1.0.0
File Size Uploaded Provenance Actions
upstride_argparse-1.0.0.tar.gz 5.8 kB Without provenance Details for upstride_argparse-1.0.0.tar.gz

Release files / upstride_argparse-1.0.0.tar.gz

Download URL upstride_argparse-1.0.0.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0ea55a0361e90620e161ac5857e7db507794a06220a3abfae9158c5d641f6430
BLAKE2b-256 checksum
How to use checksums
cc782a88de5d49467e91c54ec1fcd70449145a2242cdd241d6592a155c751c7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.3.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.0

Security for upstride-argparse 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

1 release file No provenance

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