summaryrefslogtreecommitdiff
path: root/ricky/param
diff options
context:
space:
mode:
Diffstat (limited to 'ricky/param')
-rw-r--r--ricky/param/__init__.py1
-rw-r--r--ricky/param/color.py30
-rw-r--r--ricky/param/imageurl.py7
-rw-r--r--ricky/param/multiselect.py56
-rw-r--r--ricky/param/numberrange.py27
-rw-r--r--ricky/param/option.py9
-rw-r--r--ricky/param/options.py21
-rw-r--r--ricky/param/param.py38
-rw-r--r--ricky/param/string.py4
-rw-r--r--ricky/param/username.py7
10 files changed, 200 insertions, 0 deletions
diff --git a/ricky/param/__init__.py b/ricky/param/__init__.py
new file mode 100644
index 0000000..5149889
--- /dev/null
+++ b/ricky/param/__init__.py
@@ -0,0 +1 @@
+from ricky.param.param import Param
diff --git a/ricky/param/color.py b/ricky/param/color.py
new file mode 100644
index 0000000..8980236
--- /dev/null
+++ b/ricky/param/color.py
@@ -0,0 +1,30 @@
+from ricky.param.multiselect import MultiSelect
+import random
+class Color(MultiSelect):
+ def __init__(self, **kwargs):
+ super(Color, self).__init__(**kwargs)
+
+ @classmethod
+ def from_rgb(cls, r,g,b):
+ return cls(value="rgb({},{},{})".format(r,g,b))
+
+ @property
+ def value(self):
+ return super(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)
+ choice = random.randint(0, weights_total)
+ position = 0
+ for elem in self.options():
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ return
+ self.value = "rgb({},{},{})".format( random.randint(0,255), random.randint(0,255), random.randint(0,255))
diff --git a/ricky/param/imageurl.py b/ricky/param/imageurl.py
new file mode 100644
index 0000000..8c26e8f
--- /dev/null
+++ b/ricky/param/imageurl.py
@@ -0,0 +1,7 @@
+from ricky.config import TEST_URL
+from ricky.param.string import String
+
+class ImageUrl(String):
+ def __init__(self, *args, **kwargs):
+ super(ImageUrl, self).__init__(*args, **kwargs)
+ self.default(TEST_URL)
diff --git a/ricky/param/multiselect.py b/ricky/param/multiselect.py
new file mode 100644
index 0000000..d7db80f
--- /dev/null
+++ b/ricky/param/multiselect.py
@@ -0,0 +1,56 @@
+import random
+from ricky.param import Param
+
+class MultiSelect(Param):
+ def __init__(self, **kwargs):
+ self._options = []
+ if 'options' in kwargs: self._options = kwargs['options'] or []
+ super(MultiSelect, self).__init__(**kwargs)
+ if len(self._options):
+ self._validate_options()
+ 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 as e:
+ raise ValueError
+
+ def get_value(self):
+ return super(MultiSelect, self).get_value()
+ def set_value(self, value):
+ if not any([ value == i['value'] for i in self._options ]) and value != None:
+ raise ValueError
+ super(MultiSelect, self).set_value(value)
+ value = property(get_value, set_value)
+
+
+ def randomize(self):
+ weights_total = sum(map(lambda x: x["weight"], self.options()))
+ choice = random.randint(0, weights_total)
+ position = 0
+ for elem in self.options():
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ break
+
+ 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()
diff --git a/ricky/param/numberrange.py b/ricky/param/numberrange.py
new file mode 100644
index 0000000..7e01a81
--- /dev/null
+++ b/ricky/param/numberrange.py
@@ -0,0 +1,27 @@
+import sys
+from ricky.param.multiselect import MultiSelect
+import random
+class NumberRange(MultiSelect):
+ def __init__(self, **kwargs):
+ super(NumberRange, self).__init__(**kwargs)
+ 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
+ choice = random.randint(0, weights_total)
+ position = 0
+ for elem in self.options():
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ return
+ self.value = random.randint(self.range_min,self.range_max)
+ @property
+ def value(self):
+ return super(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/ricky/param/option.py b/ricky/param/option.py
new file mode 100644
index 0000000..89bd5db
--- /dev/null
+++ b/ricky/param/option.py
@@ -0,0 +1,9 @@
+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
new file mode 100644
index 0000000..2a21cce
--- /dev/null
+++ b/ricky/param/options.py
@@ -0,0 +1,21 @@
+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
new file mode 100644
index 0000000..05388f8
--- /dev/null
+++ b/ricky/param/param.py
@@ -0,0 +1,38 @@
+import pprint
+
+class Param(object):
+# def __init__(self, **kwargs):
+ def __init__(self, required=0, set_by_user=0, value=None, name=None, **kwargs):
+ self._value_default = None
+ self.name = name
+ self.required = required
+ self.is_ready = 0
+ self.value = value
+ self.set_by_user = set_by_user
+ def __str__(self):
+ return pprint.pformat(vars(self))
+
+ def get_value(self):
+ if self.set_by_user == 1:
+ return self._value
+ return self._value_default
+ def set_value(self, value):
+ self._value = value
+ if not self._value is None:
+ self.is_ready = 1
+ self.set_by_user = 1
+ value = property(get_value, set_value)
+
+ def default(self, value):
+ self._value_default = value
+
+ @property
+ def is_ready(self):
+ return self._is_ready or not self.required
+ @is_ready.setter
+ def is_ready(self, n):
+ self._is_ready = n
+
+
+ def randomize(self):
+ pass
diff --git a/ricky/param/string.py b/ricky/param/string.py
new file mode 100644
index 0000000..10e289a
--- /dev/null
+++ b/ricky/param/string.py
@@ -0,0 +1,4 @@
+from ricky.param import Param
+class String(Param):
+ def __init__(self, **kwargs):
+ super(String, self).__init__(**kwargs)
diff --git a/ricky/param/username.py b/ricky/param/username.py
new file mode 100644
index 0000000..e5023ec
--- /dev/null
+++ b/ricky/param/username.py
@@ -0,0 +1,7 @@
+from ricky.config import USERNAME
+from ricky.param.string import String
+
+class Username(String):
+ def __init__(self, **kwargs):
+ super(Username, self).__init__(**kwargs)
+ self.default(USERNAME)