Commit 3be4dd95 authored by David Hendriks's avatar David Hendriks
Browse files

added some code to play with grid class object and the argument parsing

parent e29b6ecb
Loading
Loading
Loading
Loading

snippets/arg_test.py

0 → 100644
+63 −0
Original line number Diff line number Diff line
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)