Parameters

Define a spctial class with is easy you use for config/parametrs

source

Parameters

 Parameters (**kargs)

A splecial class whos atributes can be referenced as attributs or as dictionaty keys


source

Parameters.add_attr

 Parameters.add_attr (**kargs)

Add attributes to the Parameters class, this will be done recursivly


source

Parameters.to_dict

 Parameters.to_dict ()

Convert the parameters to dictionary recorsively


source

Parameters.from_json

 Parameters.from_json (json_file_name:str)

Read json file and add to parameters

Use cases

We can instantiate a Parameter class and immediately add attributes

params=Parameters(first=1,second='A')
assert (params.first==1) & (params.second=='A')

Attributes can be added later

params.added = 'I am new'
assert params['added'] == 'I am new'

And they can also be added recursively

params.add_attr(file_name='test.ini', paths = {'path1':'hello_world', 'path2':'http2'})
assert params.file_name == 'test.ini'
assert params.paths.path2 == 'http2'

You can see we can referance the attribute directly as in dict

assert params.paths.path1 == params['paths']['path1']
params['stam'] = 'no'
assert params.stam == 'no'
assert params['paths'].path2 == 'http2'

And can be deleted

del params['stam']
assert not hasattr(params,'stam')

The Parameters class can be printed and can be converted recursively to dict

print(params)
Parameters:
   first : 1
   second : A
   added : I am new
   file_name : test.ini
   paths : Parameters:
      path1 : hello_world
      path2 : http2
print(params.to_dict())
{'first': 1, 'second': 'A', 'added': 'I am new', 'file_name': 'test.ini', 'paths': {'path1': 'hello_world', 'path2': 'http2'}}

The parameters can also be populated using a json file

params2=Parameters().from_json('config_demo.json')
print(params2)
Parameters:
   path : Parameters:
      data : /workspace/hd/
      tmp : /workspace/hd/tmp/
      features : /workspace/nvme/features/
      train : /workspace/nvme/train/
      models : /workspace/hd/models/
      output : /workspace/hd/outputs/
      test : /workspace/nvme/test/
   platform : myserver
assert params2.platform == 'myserver'