summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryo mama <pepper@scannerjammer.com>2015-03-07 20:44:16 -0800
committeryo mama <pepper@scannerjammer.com>2015-03-07 20:44:16 -0800
commit588d6bc9734af06c1dbdb4fd687c2e55ebc6fb3d (patch)
tree5d66ab506da0f80d65a59982bc362ee33a63974c
parent27750cbcdc49d4a30da58ab07adda96eb642e5e7 (diff)
fixes
-rw-r--r--Pb_Api/ImGrid/Params.py72
-rw-r--r--Pb_Api/Param/Angle.py37
-rw-r--r--Pb_Api/Param/Bool.py6
-rw-r--r--Pb_Api/Param/Color.py25
-rw-r--r--Pb_Api/Param/MultiSelect.py25
-rw-r--r--Pb_Api/Param/Number.py50
6 files changed, 58 insertions, 157 deletions
diff --git a/Pb_Api/ImGrid/Params.py b/Pb_Api/ImGrid/Params.py
index 7c304ad..ff21e52 100644
--- a/Pb_Api/ImGrid/Params.py
+++ b/Pb_Api/ImGrid/Params.py
@@ -6,6 +6,9 @@ from Pb_Api.Param.Options import Pb_Api_Param_Options
from Pb_Api.Param.Username import Pb_Api_Param_Username
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
class Pattern_Url_Option(Pb_Api_Param_Option):
def __init__(self, **kwargs):
@@ -15,6 +18,11 @@ class Pattern_Url_Option(Pb_Api_Param_Option):
formatted = "{}/{}.png".format(PATTERN_BASE_URL, kwargs["value"])
return cls(weight=kwargs["weight"], value=formatted )
+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']
+
class ImPattern_Params(Pb_Api_Params):
def __init__(self):
self.params = [
@@ -25,47 +33,33 @@ class ImPattern_Params(Pb_Api_Params):
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_Angle(name="swing", required=0, preferred_angles=preferred_angles),
- Pb_Api_Param_Angle(name="tilt", required=0, preferred_angles=preferred_angles),
- Pb_Api_Param_Angle(name="roll", required=0, preferred_angles=preferred_angles),
+ 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_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),
+ Param_Zoom(name="zoom", required=0, options=zoom_options),
]
-#what about width and height, same as angle, just set a max and min? yeah can be that so should angle inherit from a number
-#class? yes i would make Number class which doesn't have prefered numbers, and angle can inherit and add preferend number functionality
- "width"
- "height"
-
- "skycolor"
- "planebgcolor"
- "bgcolor"
- "linecolor"
-
-#ok these are similar too, they have a limited range though
- "linethickness"
- "opacity"
- "spacing"
-
-
-
-
- "vlines"
- "hlines"
-
-
- "trim"
- "shadow"
-
-#ok swing tilt roll zoom is a number between -180 and 180
-#but it would be nice to prefer 0, 90, etc?
-#seems sort of similar to colors? yeah custom class basically the same logic as color? yeah can be same
+ def randomize(self):
+ super(ImPattern_Params, self).randomize()
+ p = self.params()
+ if 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()
- "swing"
- "tilt"
- "roll"
-#ok two more, the thing with this is that it has
-#a much smaller range than the others, and it can't be a number between 1 and -1
-#very weird parameters I guess that's the last thing
- "zoom"
-#like that? yep ok next thing is this above
skycolor_colors = Pb_Api_Param_Options(
Pb_Api_Param_Option( value='black', weight=50 ),
Pb_Api_Param_Option( value='silver', weight=20 ),
diff --git a/Pb_Api/Param/Angle.py b/Pb_Api/Param/Angle.py
deleted file mode 100644
index 174add6..0000000
--- a/Pb_Api/Param/Angle.py
+++ /dev/null
@@ -1,37 +0,0 @@
-from Pb_Api.Param import Pb_Api_Param
-import random
-from config import ANGLE_MAX, ANGLE_MIN
-class Pb_Api_Param_Angle(Pb_Api_Param):
- def __init__(self, **kwargs):
- self._preferred_angles = kwargs['preferred_angles']
- super(Pb_Api_Param_Angle, self).__init__(**kwargs)
-
- @classmethod
- def from_rgb(cls, r,g,b):
- return cls(value="rgb({},{},{})".format(r,g,b))
-
-
- def preferred_angles(self):
- return self._preferred_angles
- def randomize(self):
- weights_total = sum(map(lambda x: x["weight"], self.preferred_angles()) + ANGLE_MAX - ANGLE_MIN
- choice = random.randint(0, weights_total)
- position = 0
- for elem in self.options():
- position += elem["weight"]
- if position >= choice:
- self.value = elem["value"]
- break
- self.value = random.randint(ANGLE_MIN,ANGLE_MAX),
- def _choose_heaviest(self):
- heaviest_idx = 0
- heaviest_weight = 0
- idx = 0
- 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"]
- def heaviest(self):
- self.value = self._get_heaviest()
diff --git a/Pb_Api/Param/Bool.py b/Pb_Api/Param/Bool.py
index e69de29..487dcc0 100644
--- a/Pb_Api/Param/Bool.py
+++ b/Pb_Api/Param/Bool.py
@@ -0,0 +1,6 @@
+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 9130794..88bd9bd 100644
--- a/Pb_Api/Param/Color.py
+++ b/Pb_Api/Param/Color.py
@@ -1,41 +1,24 @@
-from Pb_Api.Param import Pb_Api_Param
+from Pb_Api.Param.MultiSelect import Pb_Api_Param_MultiSelect
import random
-class Pb_Api_Param_Color(Pb_Api_Param):
+class Pb_Api_Param_Color(Pb_Api_Param_MultiSelect):
def __init__(self, **kwargs):
- self._preferred_colors = kwargs['preferred_colors']
super(Pb_Api_Param_Color, self).__init__(**kwargs)
@classmethod
def from_rgb(cls, r,g,b):
return cls(value="rgb({},{},{})".format(r,g,b))
-
-
- def preferred_colors(self):
- return self._preferred_colors
def randomize(self):
- weights_total = sum(map(lambda x: x["weight"], self.preferred_angles()) + (255 * 255 * 255) #so this is the only thing that changes? yeah and
+ 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"]
- break
+ return
self.value = "rgb({},{},{})".format(
random.randint(0,255),
random.randint(0,255),
random.randint(0,255),
)
- def _choose_heaviest(self):
- heaviest_idx = 0
- heaviest_weight = 0
- idx = 0
- 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"]
- def heaviest(self):
- self.value = self._get_heaviest()
diff --git a/Pb_Api/Param/MultiSelect.py b/Pb_Api/Param/MultiSelect.py
index 88637b9..365258d 100644
--- a/Pb_Api/Param/MultiSelect.py
+++ b/Pb_Api/Param/MultiSelect.py
@@ -3,10 +3,12 @@ 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']
+ self._options = kwargs['options'] if options in kwargs else []
super(Pb_Api_Param_MultiSelect, self).__init__(*args, **kwargs)
- self._validate_options()
- self.default(self._choose_heaviest())
+ if len(self._options):
+ self._validate_options()
+ self.default(self._choose_heaviest())
+
def options(self):
return self._options
def _validate_options(self):
@@ -39,11 +41,14 @@ class Pb_Api_Param_MultiSelect(Pb_Api_Param):
heaviest_idx = 0
heaviest_weight = 0
idx = 0
- 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"]
+ if (len(self.options())):
+ for elem in self.options():
+ if elem["weight"] > heaviest_weight:
+ heaviest_weight = elem["weight"]
+ heaviest_idx = idx;
+ idx += 1
+ self.value = self.options()[heaviest_idx]["value"]
+ else:
+ self.randomize()
def heaviest(self):
- self.value = self._get_heaviest()
+ self.value = self._choose_heaviest()
diff --git a/Pb_Api/Param/Number.py b/Pb_Api/Param/Number.py
deleted file mode 100644
index c1f6c6e..0000000
--- a/Pb_Api/Param/Number.py
+++ /dev/null
@@ -1,50 +0,0 @@
-from Pb_Api.Param.Number import Pb_Api_Param_Number
-import random
-class Pb_Api_Param_Number(Pb_Api_Param):
- def __init__(self, **kwargs):
- super(Pb_Api_Param_Number, self).__init__(**kwargs)
- self._preferences = kwargs['preferences'] #ahhh if I name it preferences, then this can all just be number? yes
- self._max = kwargs['max']
- self._min = kwargs['min']
- self._ranges = kwargs['ranges']
- #now how do I add exclusions? well can add an array of ranges accepted instead of min/max, like:
-# a bit more calulations that way, but more generic yeah lets do it so how does it read from the kwargs? just accept array, like options
-#ahhh I think zoom is so different that it's a pain, it also requires float, where everything else is an Int logic is same though, you can have Param_Number and Param_Float, probably if constructed properly one can inherit from another, but that's complications.
-#ok so one thing that I'm learning is that inheritance does save code, but can be sort of the trickiest part of all OOP,
-#because it can be cnfusing having to plan out everything before you write, is that more or less true? yep
-#so OOP without inheritance is ok, but just results in much more code? well yeah, it have more code, but you use OOP to not have much code than in prodcedural way, to avoid copy-pasting, and benefit from sort of collective changes of all classes then you add method to parent class. so
-#basically you almost always should use inheritance, unless it is just too difficult? no well it's not always necessary, but parameters like here it's should be with inhertance, becuase they are just all the same multiselect or select over virtual range of numbers.
-#alright. I think I should take a break from this for a little while, read more code, and hopefully in about a week I'll understand it better
-#already, I think Ive more ideas that are related to OOP, but do you think it's realistic to expect it to be a gradual process for me to
-#shift everything ovnker? or should I try to force it? just take it step by step, this project is a bit complicated, can be written in many ways one of then will use a lot of things from oop world, and can be hard to understand right from start, so just write it the way you get it and then we can optimize it more and more. Right, but primarily the way to learn is to USE libraries, and observe how they work, right? yes ok
-#part of the issue I think is that in my mind I'm thinking like a shell script, and that is bad for OOP because it seems like
-#the shell really has nothing to do with any of this, calling the shell command on the terminal is ath bit different from calling a method here,
-#or is that wrong? it's right but I should think that the shell itself is an object, and the commands are sort of the api for generating new
-#method calls? yeah if computer is an object with fs and all stuff, then shell is api for that object and calling commands is calling methods of this object, which change it's state (modify internal "variables"- files). ok cool, but the confusing thing about that is that the naming is
-#all completely different from something like python or perl, right? yes ok cool I do understand, I think this was just a little too ambitious.
-#I'll work on some other code and come back to this next weekend. Thanks a lot for the help no problems
-
- def preferences(self):
- return self._preferences
- def randomize(self):
- weights_total = sum(map(lambda x: x["weight"], self.preferences()) + self._max - self._min
- choice = random.randint(0, weights_total)
- position = 0i
- for elem in self.options():
- position += elem["weight"]
- if position >= choice:
- self.value = elem["value"]
- break
- self.value = random.randint(ANGLE_MIN,ANGLE_MAX),
- def _choose_heaviest(self):
- heaviest_idx = 0
- heaviest_weight = 0
- idx = 0
- 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"]
- def heaviest(self):
- self.value = self._get_heaviest()