=Parameters(first=1,second='A')
paramsassert (params.first==1) & (params.second=='A')
Parameters
Define a spctial class with is easy you use for config/parametrs
Parameters
Parameters (**kargs)
A splecial class whos atributes can be referenced as attributs or as dictionaty keys
Parameters.add_attr
Parameters.add_attr (**kargs)
Add attributes to the Parameters class, this will be done recursivly
Parameters.to_dict
Parameters.to_dict ()
Convert the parameters to dictionary recorsively
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
Attributes can be added later
= 'I am new'
params.added assert params['added'] == 'I am new'
And they can also be added recursively
='test.ini', paths = {'path1':'hello_world', 'path2':'http2'})
params.add_attr(file_nameassert 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']
'stam'] = 'no'
params[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
=Parameters().from_json('config_demo.json') params2
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'