summaryrefslogtreecommitdiff
path: root/Pb_Api/ImGrid
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-03-11 02:51:58 -0700
committeryo mama <pepper@scannerjammer.com>2015-03-11 02:51:58 -0700
commita5123bfab06f7fd8884b9b4f12f579dceec43aa1 (patch)
tree5120729cadf48ce97caeb90c34b850308c9a9f21 /Pb_Api/ImGrid
parentd9651ca5fb6c655afbe88a014c8bb8a8f000a70d (diff)
finished OOP rewriteOOP
Diffstat (limited to 'Pb_Api/ImGrid')
-rw-r--r--Pb_Api/ImGrid/Params.py132
-rw-r--r--Pb_Api/ImGrid/__init__.py10
-rwxr-xr-xPb_Api/ImGrid/options.py64
3 files changed, 139 insertions, 67 deletions
diff --git a/Pb_Api/ImGrid/Params.py b/Pb_Api/ImGrid/Params.py
index ef07251..c469b6a 100644
--- a/Pb_Api/ImGrid/Params.py
+++ b/Pb_Api/ImGrid/Params.py
@@ -1,4 +1,4 @@
-import re
+import re, random
from Pb_Api.Params import Pb_Api_Params
from Pb_Api.Param import Pb_Api_Param
from Pb_Api.Param.Option import Pb_Api_Param_Option
@@ -9,23 +9,16 @@ 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
-#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)
- @classmethod
- def from_name(cls, **kwargs):
- formatted = "{}/{}.png".format(PATTERN_BASE_URL, kwargs["value"])
- return cls(weight=kwargs["weight"], value=formatted )
-#I made options optional
+from Pb_Api.ImGrid.options import *
class Param_Zoom(Pb_Api_Param_NumberRange):
def __init__(self, **kwargs):
- super(Pb_Api_Param_Option, self).__init__(**kwargs)
+ super(Param_Zoom, self).__init__(**kwargs)
self.exclusion_range = kwargs['exclusion_range']
+ def test_value(self):
+ return not ((self.value > self.exclusion_range[0]) and (self.value < self.exclusion_range[1]))
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])
+ weights_total = sum(map(lambda x: x["weight"], self.options())) + 10
choice = random.randint(0, weights_total)
position = 0
for elem in self.options():
@@ -35,19 +28,36 @@ class Param_Zoom(Pb_Api_Param_NumberRange):
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
+ self.value = round(random.uniform(self.range_max, self.range_min), 2)
+ if self.test_value: 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.
+
+ @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
+
+class Param_Opacity(Pb_Api_Param_NumberRange):
+ def __init__(self, **kwargs):
+ super(Param_Opacity, self).__init__(**kwargs)
+ 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 = round(random.uniform(self.range_min,range_max), 2),
-class ImPattern_Params(Pb_Api_Params):
+class ImGrid_Params(Pb_Api_Params):
def __init__(self):
self.params = [
Pb_Api_Param_Username(name="username", required=0),
@@ -56,48 +66,46 @@ class ImPattern_Params(Pb_Api_Params):
Pb_Api_Param_Image_Url(name="planebgimage", required=0),
Pb_Api_Param_MultiSelect(name="format", required=0, options=format_options),
Pb_Api_Param_MultiSelect(name="transition", required=1, options=transition_options),
- Pb_Api_Param_Color(name="skycolor", required=0, preferred_colors=skycolor_colors),
- Pb_Api_Param_Color(name="planebgcolor", required=0, preferred_colors=planebgcolor_colors),
- Pb_Api_Param_Color(name="bgcolor", required=0, preferred_colors=bgcolor_colors),
- Pb_Api_Param_Color(name="linecolor", required=0, preferred_colors=linecolor_colors),
- Pb_Api_Param_NumberRange(name="swing", required=0, options=swing_options),
- Pb_Api_Param_NumberRange(name="tilt", required=0, options=tilt_options),
- Pb_Api_Param_NumberRange(name="roll", required=0, options=roll_options),
- Pb_Api_Param_NumberRange(name="width", required=0, options=width_options),
- Pb_Api_Param_NumberRange(name="height", required=0, options=height_options),
- 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_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_Color(name="skycolor", required=0, options=skycolor_colors),
+ Pb_Api_Param_Color(name="planebgcolor", required=0, options=planebgcolor_colors),
+ Pb_Api_Param_Color(name="bgcolor", required=0, options=bgcolor_colors),
+ Pb_Api_Param_Color(name="linecolor", required=0, options=linecolor_colors),
+ Pb_Api_Param_NumberRange(name="swing", required=0, options=swing_options, min=-170, max=170),
+ Pb_Api_Param_NumberRange(name="tilt", required=0, options=tilt_options, min=-170, max=170),
+ Pb_Api_Param_NumberRange(name="roll", required=0, options=roll_options, min=-170, max=170),
+ Pb_Api_Param_NumberRange(name="width", required=0, options=width_options, min=100, max=800),
+ Pb_Api_Param_NumberRange(name="height", required=0, options=height_options, min=100, max=800),
+ Pb_Api_Param_NumberRange(name="linethickness", required=0, options=linethickness_options, min=1, max=30),
+ Pb_Api_Param_NumberRange(name="opacity", required=0, options=opacity_options, min=0, max=1),
+ Pb_Api_Param_NumberRange(name="spacing", required=0, options=spacing_options, min=2, max=100),
+ Pb_Api_Param_MultiSelect(name="vlines", required=0, options=vlines_options),
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),
+ Param_Zoom(name="zoom", required=0, options=zoom_options, min=-12, max=12, exclusion_range=[-1.1, 1.1]),
]
+ def test_values(self):
+ p = self.params
+ return not any([
+ (self.param('spacing').value > self.param('width').value),
+ (self.param('spacing').value > self.param('height').value),
+ (self.param('linethickness').value > self.param('width').value),
+ (self.param('linethickness').value > self.param('height').value),
+ ])
def randomize(self):
- 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 ]):
- 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 ),
- Pb_Api_Param_Option( value='silver', weight=20 ),
-)
-
-pattern_url_options = Pb_Api_Param_Options([
- Pattern_Url_Option.from_name(weight=0, value=i) for i in range(1,100) ] + [
- Pattern_Url_Option.from_name(weight=0, value="a{}".format(i)) for i in range(0, 42)
-])
+ p = self.params
+ for el in p:
+ if el in ['spacing', 'linethickness']:
+ continue
+ if el.set_by_user:
+ continue
+ el.randomize()
+ for name in ['spacing', 'linethickness']:
+ max_tries = 10000
+ while(max_tries):
+ self.param(name).randomize()
+ if self.test_values():
+ return
+ max_tries -= 1
+ raise ValueError
-pattern_url_options.search("a10").weight = 20;
diff --git a/Pb_Api/ImGrid/__init__.py b/Pb_Api/ImGrid/__init__.py
index 0c238b9..553c04c 100644
--- a/Pb_Api/ImGrid/__init__.py
+++ b/Pb_Api/ImGrid/__init__.py
@@ -1,13 +1,13 @@
#!/usr/bin/python2.7
from Pb_Api import Pb_Api
from Pb_Api.ImGrid.Params import ImGrid_Params
-from config import IMPATTERN_URL
+from config import IMGRID_URL
-class Pb_Api_ImPattern(Pb_Api):
+class Pb_Api_ImGrid(Pb_Api):
def __init__(self):
- self.url = IMPATTERN_URL
- def request_new(self):
- new_params = ImPattern_Params()
+ self.url = IMGRID_URL
+ def params_init(self):
+ new_params = ImGrid_Params()
#new_params = self.get_from_server()
new_params.api = self
return new_params
diff --git a/Pb_Api/ImGrid/options.py b/Pb_Api/ImGrid/options.py
new file mode 100755
index 0000000..387a489
--- /dev/null
+++ b/Pb_Api/ImGrid/options.py
@@ -0,0 +1,64 @@
+from Pb_Api.Param.Options import Pb_Api_Param_Options
+format_options = Pb_Api_Param_Options.from_dict(
+ { 'weight': 20, 'value': 'png' },
+ { 'weight': 0, 'value': 'gif' },
+ { 'weight': 0, 'value': 'jpg' },
+)
+transition_options = Pb_Api_Param_Options.from_dict(
+ { "value" : "background", "weight": 1 },
+ { "value" : "dither", "weight": 1 },
+ { "value" : "random", "weight": 1 },
+ { "value" : "tile", "weight": 1 },
+ { "value" : "edge", "weight": 1 },
+)
+skycolor_colors = \
+ bgcolor_colors = planebgcolor_colors = Pb_Api_Param_Options.from_dict(
+ { "value" : "white", "weight" : 1 },
+ { "value" : "silver", "weight" : 1 },
+ { "value" : None, "weight" : 10 },
+)
+
+linecolor_colors = Pb_Api_Param_Options.from_dict(
+ { "value" : "black", "weight" : 1 },
+ { "value" : "white", "weight" : 1 },
+ { "value" : "silver", "weight" : 1 },
+)
+swing_options = tilt_options = roll_options = Pb_Api_Param_Options.from_dict(
+ {"value": "", "weight": 2},
+ {"value": 30, "weight": 1},
+ {"value": -30, "weight": 1},
+)
+width_options = height_options = Pb_Api_Param_Options.from_dict(
+ { "value" : 400, "weight" : 1 },
+ { "value" : 600, "weight" : 1 },
+)
+linethickness_options = Pb_Api_Param_Options.from_dict(
+ {"value":1, "weight": 2},
+ {"value":2, "weight": 1},
+)
+opacity_options = Pb_Api_Param_Options.from_dict(
+ {"value":1, "weight": 2},
+ {"value":0.5, "weight": 1},
+)
+spacing_options = Pb_Api_Param_Options.from_dict(
+ {"value":10, "weight": 1},
+ {"value":15, "weight": 1},
+)
+vlines_options = hlines_options = Pb_Api_Param_Options.from_dict(
+ {"value":"", "weight": 2},
+ {"value":"true", "weight": 1},
+)
+shadow_options = Pb_Api_Param_Options.from_dict(
+ {"value":"", "weight": 1},
+ {"value":"true", "weight": 1},
+)
+zoom_options = Pb_Api_Param_Options.from_dict(
+ {"value": 0, "weight": 3},
+ {"value": 1.2, "weight": 1},
+ {"value": -1.2, "weight": 1},
+)
+trim_options = Pb_Api_Param_Options.from_dict(
+ {"value":"", "weight": 1},
+ {"value":"true", "weight": 1},
+)
+