summaryrefslogtreecommitdiff
path: root/ricky/param
diff options
context:
space:
mode:
Diffstat (limited to 'ricky/param')
-rw-r--r--ricky/param/__init__.py17
-rw-r--r--ricky/param/bool.py6
-rw-r--r--ricky/param/color.py5
-rw-r--r--ricky/param/constrainednumber.py4
-rw-r--r--ricky/param/enum.py6
5 files changed, 30 insertions, 8 deletions
diff --git a/ricky/param/__init__.py b/ricky/param/__init__.py
index cbacf6f..4ec8204 100644
--- a/ricky/param/__init__.py
+++ b/ricky/param/__init__.py
@@ -1,4 +1,5 @@
import pprint
+import random
class Param(object):
@@ -53,7 +54,21 @@ class Param(object):
default = property(default_get, default_set)
- def randomize(self):
+ def _choose_from_probabilities(self, probabilities):
+ """
+ if using weights, considers all weights as a percentage of
+ 100
+ """
+ choice = random.randint(0, 100)
+ position = 0
+ for elem in probabilities:
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ return True
+ return True
+
+ def randomize(self, probabilities=None):
pass
def from_normalized(self, value):
diff --git a/ricky/param/bool.py b/ricky/param/bool.py
index 2b25bbd..4f75dc6 100644
--- a/ricky/param/bool.py
+++ b/ricky/param/bool.py
@@ -32,8 +32,10 @@ class Bool(Param):
raise ValueError("Bad Value for Bool %s" % value)
return value
- def randomize(self):
- self.value_set(random.choice([True, False]))
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
+ self.value = random.choice([True, False])
def from_normalized(self, value):
value_as_int = int(round(value, 0))
diff --git a/ricky/param/color.py b/ricky/param/color.py
index 5da05ed..7abc23a 100644
--- a/ricky/param/color.py
+++ b/ricky/param/color.py
@@ -35,10 +35,11 @@ class Color(Param):
value = int(self.as_hex(), 16)
return decimal.Decimal(value)/decimal.Decimal(maximum)
- def randomize(self):
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
self.value = "rgb(%s,%s,%s)" % (
random.randint(0, 255),
random.randint(0, 255),
random.randint(0, 255)
)
- self._is_rgb = True
diff --git a/ricky/param/constrainednumber.py b/ricky/param/constrainednumber.py
index 3563d22..3af3bf9 100644
--- a/ricky/param/constrainednumber.py
+++ b/ricky/param/constrainednumber.py
@@ -73,7 +73,9 @@ class ConstrainedNumber(Param):
value = int(value)
self.value = value
- def randomize(self, tries=0):
+ def randomize(self, tries=0, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
tries_max = 30
try:
self._generate_random()
diff --git a/ricky/param/enum.py b/ricky/param/enum.py
index d4a839a..137c58e 100644
--- a/ricky/param/enum.py
+++ b/ricky/param/enum.py
@@ -30,8 +30,10 @@ class Enum(Param):
value = property(value_get, value_set)
- def randomize(self):
- self.value_set(random.choice(self._options))
+ def randomize(self, probabilities=None):
+ if probabilities and self._choose_from_probabilities(probabilities):
+ return
+ self.value = random.choice(self._options)
def as_dict(self):
my_dict = super(Enum, self).as_dict()