summaryrefslogtreecommitdiff
path: root/Pb_Api/Param
diff options
context:
space:
mode:
Diffstat (limited to 'Pb_Api/Param')
-rw-r--r--Pb_Api/Param/Color.py19
-rw-r--r--Pb_Api/Param/MultiSelect.py19
-rw-r--r--Pb_Api/Param/NumberRange.py14
-rw-r--r--Pb_Api/Param/Options.py12
4 files changed, 44 insertions, 20 deletions
diff --git a/Pb_Api/Param/Color.py b/Pb_Api/Param/Color.py
index 1c14c8d..678a087 100644
--- a/Pb_Api/Param/Color.py
+++ b/Pb_Api/Param/Color.py
@@ -7,10 +7,19 @@ class Pb_Api_Param_Color(Pb_Api_Param_MultiSelect):
@classmethod
def from_rgb(cls, r,g,b):
return cls(value="rgb({},{},{})".format(r,g,b))
-
+
+ @property
+ def value(self):
+ return super(Pb_Api_Param_MultiSelect, self).get_value()
+ @value.setter
+ def value(self, value):
+ self._value = value
+ if not self._value is None:
+ self.is_ready = 1
+ self.set_by_user = 1
def randomize(self):
- weights_total = sum(map(lambda x: x["weight"], self.options()) + (255 * 255 * 255)
+ weights_total = sum(map(lambda x: x["weight"], self.options())) + (255 * 255 * 255)
choice = random.randint(0, weights_total)
position = 0
for elem in self.options():
@@ -18,8 +27,4 @@ class Pb_Api_Param_Color(Pb_Api_Param_MultiSelect):
if position >= choice:
self.value = elem["value"]
return
- self.value = "rgb({},{},{})".format(
- random.randint(0,255),
- random.randint(0,255),
- random.randint(0,255),
- )
+ self.value = "rgb({},{},{})".format( random.randint(0,255), random.randint(0,255), random.randint(0,255))
diff --git a/Pb_Api/Param/MultiSelect.py b/Pb_Api/Param/MultiSelect.py
index b84d75b..3761302 100644
--- a/Pb_Api/Param/MultiSelect.py
+++ b/Pb_Api/Param/MultiSelect.py
@@ -2,9 +2,10 @@ import random
from Pb_Api.Param import Pb_Api_Param
class Pb_Api_Param_MultiSelect(Pb_Api_Param):
- def __init__(self, *args, **kwargs):
- self._options = kwargs['options'] if options in kwargs else []
- super(Pb_Api_Param_MultiSelect, self).__init__(*args, **kwargs)
+ def __init__(self, **kwargs):
+ self._options = []
+ if 'options' in kwargs: self._options = kwargs['options'] or []
+ super(Pb_Api_Param_MultiSelect, self).__init__(**kwargs)
if len(self._options):
self._validate_options()
self.default(self._choose_heaviest())
@@ -19,14 +20,14 @@ class Pb_Api_Param_MultiSelect(Pb_Api_Param):
except Exception as e:
raise ValueError
- @property
- def value(self):
+ def get_value(self):
return super(Pb_Api_Param_MultiSelect, self).get_value()
- @value.setter
- def value(self, value):
- if not any([ value == i['value'] for i in self.options() ]) and value != None:
+ def set_value(self, value):
+ if not any([ value == i['value'] for i in self._options ]) and value != None:
raise ValueError
super(Pb_Api_Param_MultiSelect, self).set_value(value)
+ value = property(get_value, set_value)
+
def randomize(self):
weights_total = sum(map(lambda x: x["weight"], self.options()))
@@ -48,7 +49,7 @@ class Pb_Api_Param_MultiSelect(Pb_Api_Param):
heaviest_weight = elem["weight"]
heaviest_idx = idx;
idx += 1
- self.value = self.options()[heaviest_idx]["value"]
+ return self.options()[heaviest_idx]["value"]
else:
self.randomize()
def heaviest(self):
diff --git a/Pb_Api/Param/NumberRange.py b/Pb_Api/Param/NumberRange.py
index fa11a29..fe1ece6 100644
--- a/Pb_Api/Param/NumberRange.py
+++ b/Pb_Api/Param/NumberRange.py
@@ -1,3 +1,4 @@
+import sys
from Pb_Api.Param.MultiSelect import Pb_Api_Param_MultiSelect
import random
class Pb_Api_Param_NumberRange(Pb_Api_Param_MultiSelect):
@@ -6,7 +7,7 @@ class Pb_Api_Param_NumberRange(Pb_Api_Param_MultiSelect):
self.range_min = kwargs['min']
self.range_max = kwargs['max']
def randomize(self):
- weights_total = sum(map(lambda x: x["weight"], self.options()) + self.range_max - self.range_min
+ weights_total = sum(map(lambda x: x["weight"], self.options())) + self.range_max - self.range_min
choice = random.randint(0, weights_total)
position = 0
for elem in self.options():
@@ -14,4 +15,13 @@ class Pb_Api_Param_NumberRange(Pb_Api_Param_MultiSelect):
if position >= choice:
self.value = elem["value"]
return
- self.value = random.randint(ANGLE_MIN,ANGLE_MAX),
+ self.value = random.randint(self.range_min,self.range_max)
+ @property
+ def value(self):
+ return super(Pb_Api_Param_MultiSelect, self).get_value()
+ @value.setter
+ def value(self, value):
+ self._value = value
+ if not self._value is None:
+ self.is_ready = 1
+ self.set_by_user = 1
diff --git a/Pb_Api/Param/Options.py b/Pb_Api/Param/Options.py
index d19f42a..ebc6fc9 100644
--- a/Pb_Api/Param/Options.py
+++ b/Pb_Api/Param/Options.py
@@ -1,8 +1,12 @@
+from Pb_Api.Param.Option import Pb_Api_Param_Option
+import sys
class Pb_Api_Param_Options:
- def __init__(self, arr):
- self._values = arr
+ 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):
@@ -11,3 +15,7 @@ class Pb_Api_Param_Options:
for i in self:
if str(s) in i.value:
return i
+ @classmethod
+ def from_dict(cls, *args):
+ options = map(lambda x: Pb_Api_Param_Option(**x), args);
+ return cls(*options);