Package: upstride-argparse
Upstride argument parserThis 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 filesExampleLet's start with a simple example to demonstrate how it works :let's create a fileapp.pycontaining :importupstride_argparseasargparsearguments=[[int,"batch_size",128,'The size of batch per gpu',lambdax:x>0],[bool,"cpu",False,'run on cpu'],[str,'description','','description of the experiment'],[float,"lr",0.0001,'learning rate',lambdax: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 stringlist[python_type]for processing listscondition 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 programpython app.pyprints{'batch_size': 128, 'cpu': False, 'description': '', 'lr': 0.0001, 'raw_size': [256, 256, 3], 'yaml_config': []}. This dictionary contains the default configurationpython app.py --cpu --lr 0.1 --description hello --raw_size 28 28 1prints{'batch_size': 128, 'cpu': True, 'description': 'hello', 'lr': 0.1, 'raw_size': [28, 28, 1], 'yaml_config': []}now lets create a yaml fileconfig.ymlcontaining :batch_size:16cpu:truepython app.py --yaml_config config.ymlprints{'batch_size': 16, 'cpu': True, 'description': '', 'lr': 0.0001, 'raw_size': [256, 256, 3], 'yaml_config': []}python app.py --yaml_config config.yml --cpu falseprints{'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 fileIt is also possible to split the configuration between as many configuration file as you wantNamespacesFor larger project, it can become useful to define namespaces to organized the configuration. This can be done like this :importupstride_argparseasargparsearguments=[[int,"batch_size",128,'The size of batch per gpu',lambdax: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)callingpython app.pywill 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:16first_namespace:arg1:worldsecond_namespace:i_am_not_doing_anything:falsenether_do_i:truecallingpython app.py --yaml_config config.ymlwill 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 falseit will print :{'batch_size': 16, 'yaml_config': [], 'first_namespace': {'arg1': 'bob', 'second_namespace': {'i_am_not_doing_anything': False, 'nether_do_i': True}}}