From 3be4dd957d67d1c38e0e720bc0193ec2ba7b6c0d Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Sat, 17 Aug 2019 17:55:18 +0100 Subject: [PATCH] added some code to play with grid class object and the argument parsing --- snippets/arg_test.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 snippets/arg_test.py diff --git a/snippets/arg_test.py b/snippets/arg_test.py new file mode 100644 index 000000000..78fced845 --- /dev/null +++ b/snippets/arg_test.py @@ -0,0 +1,63 @@ +import argparse + + +class grid(object): + def __init__(self, name): + self.name = name + self.grid_options = {} + + def load_grid_options(self): + self.grid_options = { + 'test1': 0, + 'test2': 1, + } + + def argparse(self): + """ + This function handles the arg parsing of the grid. + Make sure that every grid_option key/value is included in this, + preferably with an explanation on what that parameter will do + """ + + + parser = argparse.ArgumentParser(description='Arguments for the binary_c python wrapper grid') + + # add arguments here + parser.add_argument('--test1', type=int, help='input for test1') + parser.add_argument('--test2', type=int, help='input for test2') + + # Load the args from the cmdline + args = parser.parse_args() + + # Copy current grid_option set + new_grid_options = self.grid_options.copy() + + # loop over grid_options + for arg in vars(args): + # print ("arg: {arg} value: {value}".format(arg=arg, value=getattr(args, arg))) + + # If an input has been given in the cmdline: override the previous value of grid_options + if getattr(args, arg): + new_grid_options[arg] = getattr(args, arg) + + # Put the new grid options back + self.grid_options = new_grid_options.copy() + +newgrid = grid('test') +newgrid.load_grid_options() +print(newgrid.grid_options) + +newgrid.argparse() +print(newgrid.grid_options) + +# Custom set a single value: +newgrid.grid_options['test2'] = 2 +print(newgrid.grid_options) + +# Custom set multiple values: +newgrid.grid_options.update({ + 'test1':4, + 'test2':-2, + }) +print(newgrid.grid_options) + -- GitLab