-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparameters.py
More file actions
84 lines (64 loc) · 2.03 KB
/
parameters.py
File metadata and controls
84 lines (64 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import argparse
class Parameters(object):
""" class that handles parameters I/O
the main idea is that this class unifies the different ways a project can
be loaded. Current options are:
- a dict like object, passed to init
- from an input file
- from the command line
the main input format is from the command line
"""
def __init__(self, **kwargs):
Parameters.create_parser()
for k, v in kwargs.items():
setattr(self, k, v)
@staticmethod
def create_from_dict(d, defaults=True):
"""create_from_dict
allows creation of a set of parameters from a dictionary.
Parameters
----------
d : dict
Dictionary with format 'args' => val
defaults : bool
if True, then argparse is run once to get default arguments
Returns
-------
params : Parameters
the parameters object
"""
params = Parameters(**d)
if defaults:
defaults = Parameters.parser.parse_args("")
for k, v in defaults.iteritems():
if not hasattr(params, k):
params.k = v
return params
@staticmethod
def create_parser():
"""
generates ArgumentParser and reads options from CLI
use -h flag for details
"""
if hasattr(Parameters, "parser"):
return
args = argparse.ArgumentParser("default parser, override this")
return args
@staticmethod
def from_command_line():
"""loads arguments from command line
Returns
-------
p : Parameters
the parameters object read from the command line
"""
args = Parameters.parser
params = args.parse_args()
p = Parameters(**params.__dict__)
p.postprocess_args()
return p
def postprocess_args(p):
"""
processes some args in p
"""
pass