From b02cc8d49513cdcc54f4421e71db3512354d4e2e Mon Sep 17 00:00:00 2001 From: yo mama Date: Sat, 10 Oct 2015 02:54:41 -0700 Subject: renamed options to selections --- ricky/param/color.py | 4 ++-- ricky/param/multiselect.py | 6 +++--- ricky/param/numberrange.py | 8 ++++---- ricky/param/option.py | 9 --------- ricky/param/options.py | 21 --------------------- ricky/param/param.py | 22 +++++++++++----------- ricky/param/selection.py | 9 +++++++++ ricky/param/selections.py | 21 +++++++++++++++++++++ 8 files changed, 50 insertions(+), 50 deletions(-) delete mode 100644 ricky/param/option.py delete mode 100644 ricky/param/options.py create mode 100644 ricky/param/selection.py create mode 100644 ricky/param/selections.py (limited to 'ricky/param') diff --git a/ricky/param/color.py b/ricky/param/color.py index ecec79c..4d9f4d5 100644 --- a/ricky/param/color.py +++ b/ricky/param/color.py @@ -23,11 +23,11 @@ class Color(Param): def randomize(self): weights_total = sum( - map(lambda x: x["weight"], self.options()) + map(lambda x: x["weight"], self.selections()) ) + (255 * 255 * 255) choice = random.randint(0, weights_total) position = 0 - for elem in self.options(): + for elem in self.selections(): position += elem["weight"] if position >= choice: self.value = elem["value"] diff --git a/ricky/param/multiselect.py b/ricky/param/multiselect.py index 68df161..21eca78 100644 --- a/ricky/param/multiselect.py +++ b/ricky/param/multiselect.py @@ -10,7 +10,7 @@ class MultiSelect(Param): return super(MultiSelect, self).value_get() def value_set(self, value): - if not any([value == i['value'] for i in self._options]) and \ + if not any([value == i['value'] for i in self._selections]) and \ value is not None: raise ValueError super(MultiSelect, self).value_set(value) @@ -18,10 +18,10 @@ class MultiSelect(Param): value = property(value_get, value_set) def randomize(self): - weights_total = sum(map(lambda x: x["weight"], self.options())) + weights_total = sum(map(lambda x: x["weight"], self.selections())) choice = random.randint(0, weights_total) position = 0 - for elem in self.options(): + for elem in self.selections(): position += elem["weight"] if position >= choice: self.value = elem["value"] diff --git a/ricky/param/numberrange.py b/ricky/param/numberrange.py index 8fe517b..626d128 100644 --- a/ricky/param/numberrange.py +++ b/ricky/param/numberrange.py @@ -10,18 +10,18 @@ class NumberRange(Param): def randomize(self): weights_total = sum( - map(lambda x: x["weight"], self.options()) + map(lambda x: x["weight"], self.selections()) )# + self.range_max - self.range_min if weights_total < 100: weights_total = 100; choice = random.randint(0, weights_total) import sys - sys.stderr.write("choosing %s: random_int: %s, options: %s\n" % ( + sys.stderr.write("choosing %s: random_int: %s, selections: %s\n" % ( self.name, choice, - self.options())) + self.selections())) position = 0 - for elem in self.options(): + for elem in self.selections(): position += elem["weight"] if position >= choice: self.value = elem["value"] diff --git a/ricky/param/option.py b/ricky/param/option.py deleted file mode 100644 index 89bd5db..0000000 --- a/ricky/param/option.py +++ /dev/null @@ -1,9 +0,0 @@ -class Option(dict): - def __init__(self, **kwargs): - super(Option, self).__init__(**kwargs) - self.value = kwargs["value"] - self.weight = kwargs["weight"] - def __getattr__(self, attr): - return self.get(attr) - __setattr__= dict.__setitem__ - __delattr__= dict.__delitem__ diff --git a/ricky/param/options.py b/ricky/param/options.py deleted file mode 100644 index 2a21cce..0000000 --- a/ricky/param/options.py +++ /dev/null @@ -1,21 +0,0 @@ -from ricky.param.option import Option -import sys -class Options: - def __init__(self, *args): - self._values = args - def __iter__(self): - return iter(self._values) - def __len__(self): - return len(self._values) - def __str__(self): - return str(self._values) - def __getitem__(self, i): - return self._values[i] - def search(self, s): - for i in self: - if str(s) in i.value: - return i - @classmethod - def from_dict(cls, *args): - options = map(lambda x: Option(**x), args); - return cls(*options); diff --git a/ricky/param/param.py b/ricky/param/param.py index 57c801a..6f7e2a7 100644 --- a/ricky/param/param.py +++ b/ricky/param/param.py @@ -15,19 +15,19 @@ 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() + self._selections = kwargs.get('selections') or [] + if len(self._selections): + self._validate_selections() """default value is the option with the heaviest weight""" self.default(self._choose_heaviest()) - def options(self): - return self._options + def selections(self): + return self._selections - def _validate_options(self): + def _validate_selections(self): try: - int(self._options[0]['weight']) - self._options[0]['value'] + int(self._selections[0]['weight']) + self._selections[0]['value'] except Exception: raise ValueError('Unable to validate %s\n:' % self.name) @@ -72,13 +72,13 @@ class Param(object): heaviest_idx = 0 heaviest_weight = 0 idx = 0 - if (len(self.options())): - for elem in self.options(): + if (len(self.selections())): + for elem in self.selections(): if elem["weight"] > heaviest_weight: heaviest_weight = elem["weight"] heaviest_idx = idx idx += 1 - return self.options()[heaviest_idx]["value"] + return self.selections()[heaviest_idx]["value"] else: self.randomize() diff --git a/ricky/param/selection.py b/ricky/param/selection.py new file mode 100644 index 0000000..93a8388 --- /dev/null +++ b/ricky/param/selection.py @@ -0,0 +1,9 @@ +class Selection(dict): + def __init__(self, **kwargs): + super(Selection, self).__init__(**kwargs) + self.value = kwargs["value"] + self.weight = kwargs["weight"] + def __getattr__(self, attr): + return self.get(attr) + __setattr__= dict.__setitem__ + __delattr__= dict.__delitem__ diff --git a/ricky/param/selections.py b/ricky/param/selections.py new file mode 100644 index 0000000..303c92e --- /dev/null +++ b/ricky/param/selections.py @@ -0,0 +1,21 @@ +from ricky.param.option import Selection +import sys +class Selections: + def __init__(self, *args): + self._values = args + def __iter__(self): + return iter(self._values) + def __len__(self): + return len(self._values) + def __str__(self): + return str(self._values) + def __getitem__(self, i): + return self._values[i] + def search(self, s): + for i in self: + if str(s) in i.value: + return i + @classmethod + def from_dict(cls, *args): + selections = map(lambda x: Selection(**x), args); + return cls(*selections); -- cgit v1.2.3-70-g09d2