summaryrefslogtreecommitdiff
path: root/ricky/param/param.py
diff options
context:
space:
mode:
Diffstat (limited to 'ricky/param/param.py')
-rw-r--r--ricky/param/param.py46
1 files changed, 39 insertions, 7 deletions
diff --git a/ricky/param/param.py b/ricky/param/param.py
index 0d1739f..57c801a 100644
--- a/ricky/param/param.py
+++ b/ricky/param/param.py
@@ -15,6 +15,21 @@ class Param(object):
self.is_ready = 0
self._value = value
self.set_by_user = set_by_user
+ self._options = kwargs.get('options') or []
+ if len(self._options):
+ self._validate_options()
+ """default value is the option with the heaviest weight"""
+ self.default(self._choose_heaviest())
+
+ def options(self):
+ return self._options
+
+ def _validate_options(self):
+ try:
+ int(self._options[0]['weight'])
+ self._options[0]['value']
+ except Exception:
+ raise ValueError('Unable to validate %s\n:' % self.name)
def __str__(self):
return pprint.pformat(vars(self))
@@ -26,13 +41,13 @@ class Param(object):
def value_set(self, value):
self._value = value
- sys.stderr.write("trying to set %s - %s: %s \n" %
- (
- self.__class__.__name__,
- self.name,
- value
- )
- )
+# sys.stderr.write("trying to set %s - %s: %s \n" %
+# (
+# self.__class__.__name__,
+# self.name,
+# value
+# )
+# )
if self._value:
self.is_ready = 1
self.set_by_user = 1
@@ -52,3 +67,20 @@ class Param(object):
def randomize(self):
pass
+
+ def _choose_heaviest(self):
+ heaviest_idx = 0
+ heaviest_weight = 0
+ idx = 0
+ if (len(self.options())):
+ for elem in self.options():
+ if elem["weight"] > heaviest_weight:
+ heaviest_weight = elem["weight"]
+ heaviest_idx = idx
+ idx += 1
+ return self.options()[heaviest_idx]["value"]
+ else:
+ self.randomize()
+
+ def heaviest(self):
+ self.value = self._choose_heaviest()