diff options
| author | pepperpepperpepper <pepper@scannerjammer.com> | 2015-12-05 16:57:53 -0800 |
|---|---|---|
| committer | pepperpepperpepper <pepper@scannerjammer.com> | 2015-12-05 16:57:53 -0800 |
| commit | e85b0885ac45e5773a2e9dfb2b91bc8a57de98e2 (patch) | |
| tree | d4100b771fa1fd14dd09d62770ae5165bd5fdf4d /ricky/param/enum.py | |
| parent | 0b0ac03c2f74996b178282bbaa0684229fd18393 (diff) | |
added normalization to params
Diffstat (limited to 'ricky/param/enum.py')
| -rw-r--r-- | ricky/param/enum.py | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/ricky/param/enum.py b/ricky/param/enum.py index f0adc9e..3a9fe0e 100644 --- a/ricky/param/enum.py +++ b/ricky/param/enum.py @@ -1,10 +1,11 @@ import random from ricky.param import Param +import decimal class Enum(Param): def __init__(self, options=None, **kwargs): - self._options = options + self._options = set(options) if not self._options: raise ValueError("Object must be initialized with options set") super(Enum, self).__init__(**kwargs) @@ -36,3 +37,13 @@ class Enum(Param): my_dict = super(Enum, self).as_dict() my_dict['options'] = self._options return my_dict + + def from_normalized(self, value): + maximum = len(self._options - 1) + idx_val = int(round(maximum * value, 0)) + self.value = self._options[idx_val] + + def as_normalized(self): + maximum = len(self._options - 1) + value = int(self.as_hex(), 16) + return decimal.Decimal(value)/decimal.Decimal(maximum) |
