blob: 476e0d2d8a7cba835810077b3be87e6dcfebf9ae (
plain)
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
85
86
87
88
89
90
91
|
"""base class for all params"""
import pprint
import simplejson as json
import sys
import os
from ricky.config import PROBABILITIES_DIR
class Params(object):
def __init__(self, *args):
self._api = None
self._params = args
def __getitem__(self, name):
"""getter for the param by name"""
for param in self._params:
if param.name == name:
return param
raise ValueError("No param with name %s\n" % name)
def __str__(self):
"""string representation"""
return pprint.pformat(self.as_dict())
def _load_probabilities_json(self, probabilities_file=None):
if probabilities_file:
filepath = probabilities_file
else:
filepath = os.path.join(
PROBABILITIES_DIR,
"%s.json" % (self.api.__class__.__name__)
)
try:
f = open(filepath, 'r')
data = f.read()
f.close()
return json.loads(data)
except json.scanner.JSONDecodeError as e:
sys.stderr.write("Invalid Json - Problem decoding %s\n" % filepath)
sys.stderr.write("%s\n" % e)
sys.exit(1)
except IOError:
sys.stderr.write(
"Could not find probabilities file %s\n" % filepath)
sys.exit(1)
def randomize(
self,
probabilities=None,
probabilities_local=False
):
"""assign random values to all params
if using a probabilities.json file, weight is taken
into account"""
if probabilities:
probabilities_dict = self._load_probabilities_json(probabilities)
elif probabilities_local:
probabilities_dict = self._load_probabilities_json()
else:
probabilities_dict = {}
for param in self._params:
param.randomize(probabilities=probabilities_dict.get(param.name))
@property
def api(self):
"""property setter for im api"""
return self._api
@api.setter
def api(self, cls):
"""property getter for im api"""
self._api = cls
def execute(self):
"""calls the associated api"""
return self.api.call(self)
def as_dict(self):
"""displays the params names and values in dictionary form
used by the api call
"""
result = {}
for param in self._params:
result[param.name] = param.value
return result
def from_dict(self, params_dict):
"""set param values manually from a dictionary"""
for param in self._params:
if param.name in params_dict.keys():
param.value_set(params_dict[param.name])
|