summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Pb_Api/ImGrid/Params.py50
-rw-r--r--Pb_Api/Param/Bool.py6
-rw-r--r--Pb_Api/Param/Color.py1
-rw-r--r--Pb_Api/Param/MultiSelect.py1
4 files changed, 42 insertions, 16 deletions
diff --git a/Pb_Api/ImGrid/Params.py b/Pb_Api/ImGrid/Params.py
index ff21e52..ef07251 100644
--- a/Pb_Api/ImGrid/Params.py
+++ b/Pb_Api/ImGrid/Params.py
@@ -8,8 +8,8 @@ from Pb_Api.Param.Image_Url import Pb_Api_Param_Image_Url
from Pb_Api.Param.MultiSelect import Pb_Api_Param_MultiSelect
from Pb_Api.Param.NumberRange import Pb_Api_Param_NumberRange
from Pb_Api.Param.Color import Pb_Api_Param_Color
-from Pb_Api.Param.Bool import Pb_Api_Param_Bool
+#ok so is there anything else a bit weird here?
class Pattern_Url_Option(Pb_Api_Param_Option):
def __init__(self, **kwargs):
super(Pb_Api_Param_Option, self).__init__(**kwargs)
@@ -17,12 +17,36 @@ class Pattern_Url_Option(Pb_Api_Param_Option):
def from_name(cls, **kwargs):
formatted = "{}/{}.png".format(PATTERN_BASE_URL, kwargs["value"])
return cls(weight=kwargs["weight"], value=formatted )
+#I made options optional
class Param_Zoom(Pb_Api_Param_NumberRange):
def __init__(self, **kwargs):
super(Pb_Api_Param_Option, self).__init__(**kwargs)
self.exclusion_range = kwargs['exclusion_range']
-
+ def randomize(self):
+ weights_total = sum(map(lambda x: x["weight"], self.options()) +
+ (self.range_max - self.range_min) - (self.exclusion_range[1] - self.exclusion_range[0])
+ choice = random.randint(0, weights_total)
+ position = 0
+ for elem in self.options():
+ position += elem["weight"]
+ if position >= choice:
+ self.value = elem["value"]
+ return
+ max_tries = 10000
+ while(max_tries):
+ self.value = random.randint(self.range_max, self.range_min),
+ if not (self.value > self.exclusion_range[0] or self.value < self.exclusion_range[1]):
+ return
+ max_tries -= 1;
+ raise ValueError
+#does this make any sense? yeah but can be improved a lot, so if you see you have a lot of ways to check if value is valid or not, and
+#those checks appear here and there, so it's time to just move that code into separate method like is_valid, so you could quickly check
+#if newly generate value is valid. moreover, this loop with max_tries already used in several classes (and i suspect you plan to use that later too). instead, you can defined randomize() method to have this max_tries loop, and call something like randomize_do() in child class, so all classes will have ability to regenrate value if it's not valid. I get it, and I can do that.
+#thing is that so far this is only needed once, for one param. and the randomize thing below was applying to the params class,
+#sort of a different check, to see if all the randomized params work with one another, so I think maybe I should wait to move it into
+#the parent? alright, ideally max_tries loop will be in Params class only, and Params class will have is_valid too, so basically if one param is not valid (as seen from Params) you will start over again. ahh, if one param isn't valid, it can just call randomize again on that param (individually) not on all the others, right? yep yeah that's much better ok I'll change that tomorrow.
+
class ImPattern_Params(Pb_Api_Params):
def __init__(self):
self.params = [
@@ -44,21 +68,27 @@ class ImPattern_Params(Pb_Api_Params):
Pb_Api_Param_NumberRange(name="linethickness", required=0, options=height_options),
Pb_Api_Param_NumberRange(name="opacity", required=0, options=height_options),
Pb_Api_Param_NumberRange(name="spacing", required=0, options=height_options),
- Pb_Api_Param_Bool(name="vlines", required=0, options=vlines_options),
- Pb_Api_Param_Bool(name="hlines", required=0, options=hlines_options),
- Pb_Api_Param_Bool(name="trim", required=0, options=trim_options),
- Pb_Api_Param_Bool(name="shadow", required=0, options=shadow_options),
+ Pb_Api_Param_MultiSelect(name="vlines", required=0, options=vlines_options),# not defined yet? oh the thing is the options have a
+#predefined weight...that's what you're supposed to set here, anyway hm, that complicated thing if different bool values have different weigths, so it's better to keep them as is then, o rs change to multiselect
+ Pb_Api_Param_MultiSelect(name="hlines", required=0, options=hlines_options),
+ Pb_Api_Param_MultiSelect(name="trim", required=0, options=trim_options),
+ Pb_Api_Param_MultiSelect(name="shadow", required=0, options=shadow_options),
Param_Zoom(name="zoom", required=0, options=zoom_options),
]
def randomize(self):
- super(ImPattern_Params, self).randomize()
- p = self.params()
- if any([
+ max_tries = 10000
+ while(max_tries):
+ super(ImPattern_Params, self).randomize()
+ p = self.params()
+ if not any([
p['spacing'].value > p['width'].value,
p['spacing'].value > p['height'].value,
p['linethickness'] > p['width'].value,
p['linethickness'] > p['height'].value ]):
- self.randomize()
+ return
+ max_tries -= 1
+ raise ValueError
+##ok I think I get it, last thing is this Param_Zoom
skycolor_colors = Pb_Api_Param_Options(
Pb_Api_Param_Option( value='black', weight=50 ),
diff --git a/Pb_Api/Param/Bool.py b/Pb_Api/Param/Bool.py
deleted file mode 100644
index 487dcc0..0000000
--- a/Pb_Api/Param/Bool.py
+++ /dev/null
@@ -1,6 +0,0 @@
-from Pb_Api.Param.MultiSelect import Pb_Api_Param_MultiSelect
-class Pb_Api_Param_Bool(Pb_Api_Param_MultiSelect):
- def __init__(self, **kwargs):
- super(Pb_Api_Param_Bool, self).__init__(**kwargs)
- if len(this.options()) != 2:
- raise ValueError
diff --git a/Pb_Api/Param/Color.py b/Pb_Api/Param/Color.py
index 88bd9bd..1c14c8d 100644
--- a/Pb_Api/Param/Color.py
+++ b/Pb_Api/Param/Color.py
@@ -8,6 +8,7 @@ class Pb_Api_Param_Color(Pb_Api_Param_MultiSelect):
def from_rgb(cls, r,g,b):
return cls(value="rgb({},{},{})".format(r,g,b))
+
def randomize(self):
weights_total = sum(map(lambda x: x["weight"], self.options()) + (255 * 255 * 255)
choice = random.randint(0, weights_total)
diff --git a/Pb_Api/Param/MultiSelect.py b/Pb_Api/Param/MultiSelect.py
index 365258d..b84d75b 100644
--- a/Pb_Api/Param/MultiSelect.py
+++ b/Pb_Api/Param/MultiSelect.py
@@ -9,6 +9,7 @@ class Pb_Api_Param_MultiSelect(Pb_Api_Param):
self._validate_options()
self.default(self._choose_heaviest())
+
def options(self):
return self._options
def _validate_options(self):