Skip to main content

easy_flags

The goal of this nano-project is to provide simple alternative for argparse by adding some new features:

  1. easy definition

  2. type checking (with static type checking tools)

  3. reusability

Installation

pip install easy_flags

Basic example

foo.py

from easy_flags import SimpleConfig

class MyConfig(SimpleConfig):
    int_val = 4
    bool_val = True
    with_doc = 0.4, 'some docs'  # type: float
    without_default = None, int, 'another docs'  # type: bool

if __name__ == '__main__':
    # command line arguments will be parsed after ::define call
    c = MyConfig().define().print()
    print('bool_val:', c.bool_val)

Run:

$ python foo.py

+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|  bool_val        : True
|  int_val         : 4
|  with_doc        : 0.4
|  without_default : None
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool_val: True

Process finished with exit code 0


$ python foo.py -h

usage: foo.py [-h] [--bool_val | --no-bool_val] [--int_val INT_VAL]
                     [--with_doc WITH_DOC] [--without_default WITHOUT_DEFAULT]

optional arguments:
  -h, --help            show this help message and exit
  --bool_val            bool, default: True
  --no-bool_val
  --int_val INT_VAL     int, default: 4
  --with_doc WITH_DOC   float, default: 0.4 - some docs
  --without_default WITHOUT_DEFAULT
                        int, default: None - another docs

Alternative definition

from easy_flags import Config, IntField, BoolField, FloatField

class MyConfig(Config):
    int_val = IntField(4)
    bool_val = BoolField(default=True)
    with_doc = FloatField(0.4, 'some docs')
    without_default = IntField(doc='another docs')

Reusabillity

from easy_flags import Config

class ModelConfig(Config):
    layers = 4
    time_steps = 256
    cell_size = 256
    dropout = 1.0

# same as model config + additional parameters
class TrainingConfig(ModelConfig):
    lr = 0.001
    epochs = 10000
    dropout = 0.9  # change parent arg

Docstrings

If you want to add help message for field (which will be displayed if you run script with --help flag), then you need to add it after flags’ default value:

class ExampleConfig(BaseConfig):
    foo = 5.0, 'Some float field.'
    bar = 'field with only default docstring'
./script.py --help
usage: test_base.py [-h] [--bar BAR] [--foo FOO]

optional arguments:
  -h, --help  show this help message and exit
  --bar BAR   String field, default='field with default docstring'.
  --foo FOO   Float field, default=5.0. Some float field.

Booleans

Boolean flag with spefied in config name will set destination value to True, and the same flag prefixed with ‘no-’ will set value to False

class ExampleConfig(BaseConfig):
    cache = True
    f = False
./script --cache -f
# cache=True, f=True

./script --no-cache --no-f
# cache=False, f=False

Short flag names

If flag name consists only from one letter then it can be specified with one dash instead of two.

class ExampleConfig(BaseConfig):
    e = 100, 'number of epochs'
    b = True
./train.py -e 42 -b
# also valid with two dashes
./train.py --e 42 --b
./train.py --e 42 --no-b

Specify type for tuples

class ExampleConfig(BaseConfig):
    lr = 0.001, 'learning rate'
conf = ExampleConfig()
conf.define()

In example above pre-defined conf.lr is obviously not a float and some static checkers after typec hecking will make a warning that they expected a float as argument for some function but got tuple instead. Fortunately we can help IDE by adding special comment with proper after-define type:

class ExampleConfig(BaseConfig):
    lr = 0.001, 'learning rate'  # type: float

Metadata

Release files for easy-flags 0.2.1

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

Built distribution (wheel)

Table of built distributions (wheels) for easy-flags 0.2.1
File Interpreter ABI Platform Actions
easy_flags-0.2.1-py3-none-any.whl Python 3 none any Details for easy_flags-0.2.1-py3-none-any.whl

Release files / easy_flags-0.2.1-py3-none-any.whl

Download URL easy_flags-0.2.1-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9d8da38cb2ac426894abfdb254b3dce3417cff23bb10edf716c80a5358c499f
BLAKE2b-256 checksum
How to use checksums
443ba4c868d8412f11fbd4f7d0b5051d89ee26228fb1a427e561509c694042d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/38.2.4 requests-toolbelt/0.8.0 tqdm/4.20.0 CPython/3.6.3

Security for easy-flags 0.2.1

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.

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