Skip to main content

Simplified flags definition.

Project description

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

  1. typechecking
  2. configuration reusability
  3. config print out

Installation

pip install easy_flags

Basic example

configs.py

from easy_flags import BaseConfig

# main config with model parameters
class ModelConfig(BaseConfig):
    layers = 4
    dropout = 0.4, 'this is docstring for this field'  # type: float
    leaky = True, 'use leaky relu'  # type: bool

# extend model config with training flags
class TrainingConfig(ModelConfig):
    lr = 0.001, 'learning rate'  # type: float
    e = 100, 'number of epochs'  # type: int

class PredictConfig(ModelConfig):
    load_path = 'path/to/model/checkpoint'

train.py

from configs import TrainingConfig

conf = TrainingConfig()  # instantiate config object

def train(epochs: int, lr: float, layers: int, dropout: float, leaky_relu: bool):
    # or use `conf` directly inside function
    ...

def main():
    train(conf.e, conf.lr, conf.layers, conf.dropout, conf.leaky)

if __name__ == "__main__":
    conf.define()  # read/parse flags from sys.argv and fill up `conf` with values
    conf.print()  # pretty-print all flags and their values
    main()
python train.py --layers 2 --lr 0.1 -e 42 --no-leaky

predict.py

from configs import PredictConfig

conf = PredictConfig()

def predict(load_path: str, layers: int):
    ...

def main():
    predict(conf.load_path, conf.layers)

if __name__ == "__main__":
    conf.define()
    conf.print()
    main()
python predict.py --layers 2 --load_path "not/default/path/to/model" --no-leaky

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 IDE/linters after typechecking 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

Global FLAGS

Globally available easy_flags.FLAGS is pointing to the latest defined config or to None if no config was defined. You can specify type after import:

from easy_flags import FLAGS
from configs import ExampleConfig

FLAGS: ExampleConfig = FLAGS
# or just import config object from script that defines it
from train import conf

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
easy_flags-0.2.0-py3-none-any.whl (8.8 kB) Copy SHA256 hash SHA256 Wheel py3 Aug 14, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page