summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPepper <pepper@scannerjammer.com>2015-03-07 18:38:28 -0500
committerPepper <pepper@scannerjammer.com>2015-03-07 18:38:28 -0500
commit21b2e256c344659e2294bee33a68b5cb040b9234 (patch)
tree04d40d88ae9faab20ae671af6396eec0ee668981
parent107b40de0a2a7bcb6abcebb9aabd0f5bb7dabb4b (diff)
missing commit
-rw-r--r--Pb_Api/ImGrid/Params.py79
-rw-r--r--Pb_Api/ImGrid/__init__.py13
-rw-r--r--Pb_Api/ImPattern/Params.py15
-rwxr-xr-xPb_Api/ImPattern/__init__.py14
-rw-r--r--Pb_Api/Param/Angle.py37
-rw-r--r--Pb_Api/Param/Bool.py0
-rw-r--r--Pb_Api/Param/Color.py41
-rw-r--r--Pb_Api/Param/Number.py50
-rw-r--r--Pb_Api/Params.py20
-rw-r--r--Pb_Api/__init__.py2
-rw-r--r--config.py4
-rw-r--r--test2.py14
12 files changed, 260 insertions, 29 deletions
diff --git a/Pb_Api/ImGrid/Params.py b/Pb_Api/ImGrid/Params.py
index e69de29..7c304ad 100644
--- a/Pb_Api/ImGrid/Params.py
+++ b/Pb_Api/ImGrid/Params.py
@@ -0,0 +1,79 @@
+import re
+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
+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
+
+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 )
+
+class ImPattern_Params(Pb_Api_Params):
+ def __init__(self):
+ self.params = [
+ Pb_Api_Param_Username(name="username", required=0),
+ Pb_Api_Param_Image_Url(name="bgimage", required=0),
+ Pb_Api_Param_Image_Url(name="imageinstead", required=0),
+ 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_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),
+ ]
+#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
+
+ "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 ),
+)
+
+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)
+])
+
+pattern_url_options.search("a10").weight = 20;
diff --git a/Pb_Api/ImGrid/__init__.py b/Pb_Api/ImGrid/__init__.py
index e69de29..0c238b9 100644
--- a/Pb_Api/ImGrid/__init__.py
+++ b/Pb_Api/ImGrid/__init__.py
@@ -0,0 +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
+
+class Pb_Api_ImPattern(Pb_Api):
+ def __init__(self):
+ self.url = IMPATTERN_URL
+ def request_new(self):
+ new_params = ImPattern_Params()
+ #new_params = self.get_from_server()
+ new_params.api = self
+ return new_params
diff --git a/Pb_Api/ImPattern/Params.py b/Pb_Api/ImPattern/Params.py
index 1d31645..1e1214a 100644
--- a/Pb_Api/ImPattern/Params.py
+++ b/Pb_Api/ImPattern/Params.py
@@ -18,14 +18,6 @@ class Pattern_Url_Option(Pb_Api_Param_Option):
formatted = "{}/{}.png".format(PATTERN_BASE_URL, kwargs["value"])
return cls(weight=kwargs["weight"], value=formatted )
-
-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)
-])
-
-pattern_url_options.search("a10").weight = 20;
-
class ImPattern_Params(Pb_Api_Params):
def __init__(self):
self.params = [
@@ -33,3 +25,10 @@ class ImPattern_Params(Pb_Api_Params):
Pb_Api_Param_Image_Url(name="image_url", required=1),
Pb_Api_Param_MultiSelect(name="pattern_url", required=1, options=pattern_url_options)
]
+
+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)
+])
+
+pattern_url_options.search("a10").weight = 20;
diff --git a/Pb_Api/ImPattern/__init__.py b/Pb_Api/ImPattern/__init__.py
index 1c25913..ed2cc69 100755
--- a/Pb_Api/ImPattern/__init__.py
+++ b/Pb_Api/ImPattern/__init__.py
@@ -1,11 +1,13 @@
#!/usr/bin/python2.7
from Pb_Api import Pb_Api
-from Pb_Api.ImPattern.Params import ImPattern_Params
-
-IMPATTERN_URL = "http://asdf.us/im/api/impattern"
+from Pb_Api.ImPattern.Params import ImGrid_params
+from config import IMGRID_URL
class Pb_Api_ImPattern(Pb_Api):
def __init__(self):
- self.url = IMPATTERN_URL
- def params(self):
- return ImPattern_Params()
+ self.url = IMGRID_URL
+ def request_new(self):
+ new_params = ImGrid_Params()
+ #new_params = self.get_from_server()
+ new_params.api = self
+ return new_params
diff --git a/Pb_Api/Param/Angle.py b/Pb_Api/Param/Angle.py
new file mode 100644
index 0000000..174add6
--- /dev/null
+++ b/Pb_Api/Param/Angle.py
@@ -0,0 +1,37 @@
+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
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/Pb_Api/Param/Bool.py
diff --git a/Pb_Api/Param/Color.py b/Pb_Api/Param/Color.py
new file mode 100644
index 0000000..9130794
--- /dev/null
+++ b/Pb_Api/Param/Color.py
@@ -0,0 +1,41 @@
+from Pb_Api.Param import Pb_Api_Param
+import random
+class Pb_Api_Param_Color(Pb_Api_Param):
+ 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
+ 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 = "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/Number.py b/Pb_Api/Param/Number.py
new file mode 100644
index 0000000..c1f6c6e
--- /dev/null
+++ b/Pb_Api/Param/Number.py
@@ -0,0 +1,50 @@
+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()
diff --git a/Pb_Api/Params.py b/Pb_Api/Params.py
index 9ab90a9..29bb423 100644
--- a/Pb_Api/Params.py
+++ b/Pb_Api/Params.py
@@ -1,26 +1,32 @@
import pprint
class Pb_Api_Params(object):
+ def __init__(self):
+ self._api = None
def param(self, name):
for p in self.params:
if p.name == name:
return p
return None
-# def __iter__(self):
-# return iter(self.params)
-
def __str__(self):
return pprint.pformat({ "params": map(lambda x: vars(x), self.params) })
+
def randomize(self):
for el in self.params:
if el.set_by_user:
continue
el.randomize()
-
- def __dict__(self):
- return dict(self)
-
+ @property
+ def api(self):
+ return self._api
+ @api.setter
+ def api(self, cls):
+ self._api = cls
+
+ #does this really have to be here in Pb_Api_Params? seems confusing though yeah it has to be here because ImPattern inherits from it? impatter params
+ def execute(self):
+ return self.api.call(self)
def is_ready(self):
for p in self.params:
diff --git a/Pb_Api/__init__.py b/Pb_Api/__init__.py
index 1b89b9b..25f78dd 100644
--- a/Pb_Api/__init__.py
+++ b/Pb_Api/__init__.py
@@ -32,4 +32,4 @@ class Pb_Api:
sys.stderr.write(str(e))
raise
def call(self, params):
- return json.loads(self.post_request(self.url, params))
+ return json.loads(self.post_request(self.url, params.as_hash()))
diff --git a/config.py b/config.py
index 46d01ea..954d922 100644
--- a/config.py
+++ b/config.py
@@ -1,3 +1,7 @@
USERNAME = "RICHARD_GIOVANNI"
TEST_URL = "http://i.asdf.us/im/fc/imBreak5qI6DN2_1425433157_.png"
PATTERN_BASE_URL = "http://asdf.us/impattern/patterns"
+IMPATTERN_URL = "http://asdf.us/im/api/impattern"
+IMGRID_URL = "http://asdf.us/im/api/imgrid"
+ANGLE_MAX= 175
+ANGLE_MIN= -175
diff --git a/test2.py b/test2.py
index 5af5154..9525c4f 100644
--- a/test2.py
+++ b/test2.py
@@ -1,11 +1,11 @@
#!/usr/bin/python2.7
from Pb_Api.ImPattern import Pb_Api_ImPattern
+from Pb_Api.ImGrid import Pb_Api_ImGrid
import pprint
-p = Pb_Api_ImPattern()
-params = p.params()
-#params.param("image_url").value = "http://hello"
-#print params
-params.randomize()
-pprint.pprint(params.as_hash())
-print p.call(params.as_hash())
+api = Pb_Api_ImPattern()
+request = api.request_new()
+request.randomize()
+resp = request.execute()
+
+