diff options
| author | Pepper <pepper@scannerjammer.com> | 2015-03-04 15:57:53 -0500 |
|---|---|---|
| committer | Pepper <pepper@scannerjammer.com> | 2015-03-04 15:57:53 -0500 |
| commit | 021842ec42b991e7a641ae26862d77b324ea05db (patch) | |
| tree | 81194a75da0ff1279af7346d6e50cd424c33bacb | |
| parent | 513526efe79ff90be5b23459253dd5f553ec73d6 (diff) | |
started oop code
| -rw-r--r-- | OLD.py | 167 | ||||
| -rw-r--r-- | Pb_Api/ImGradient/Params.py | 0 | ||||
| -rw-r--r-- | Pb_Api/ImGradient/__init__.py (renamed from lib/imgradient.py) | 15 | ||||
| -rw-r--r-- | Pb_Api/ImGrid/Params.py | 0 | ||||
| -rw-r--r-- | Pb_Api/ImGrid/__init__.py | 0 | ||||
| -rw-r--r-- | Pb_Api/ImPattern/Params.py | 18 | ||||
| -rwxr-xr-x | Pb_Api/ImPattern/__init__.py | 19 | ||||
| -rw-r--r-- | Pb_Api/Param/Image_Url.py | 7 | ||||
| -rw-r--r-- | Pb_Api/Param/MultiSelect.py | 16 | ||||
| -rw-r--r-- | Pb_Api/Param/String.py | 3 | ||||
| -rw-r--r-- | Pb_Api/Param/Username.py | 7 | ||||
| -rw-r--r-- | Pb_Api/Param/__init__.py | 23 | ||||
| -rw-r--r-- | Pb_Api/Params.py | 41 | ||||
| -rw-r--r-- | Pb_Api/__init__.py | 33 | ||||
| -rw-r--r-- | lib/imgradient_params_defaults.py | 42 | ||||
| -rwxr-xr-x | lib/impattern.py | 28 | ||||
| -rw-r--r-- | lib/impattern_params_defaults.py | 142 | ||||
| -rw-r--r-- | lib/utils.py | 50 | ||||
| -rwxr-xr-x | test.py | 27 | ||||
| -rw-r--r-- | test2.py | 13 |
20 files changed, 413 insertions, 238 deletions
@@ -0,0 +1,167 @@ +#!/usr/bin/python2.7 +import random +from config import USERNAME, TEST_URL +from lib.utils import Pb_Api_Params +PATTERN_URL_BASE = "http://asdf.us/impattern/patterns" +class ImPatternParams_FromDefaults(Pb_Api_Params): +#{{{ params +# "pattern_url", +# "pattern_data", +# "username", +# "image_url", +#}}} + def __init__(self): + self.weighted_pattern_url = [ +#{{{pattern urls + { "value":"1.png", "weight":1 }, + { "value":"10.png", "weight":1 }, + { "value":"11.png", "weight":1 }, + { "value":"12.png", "weight":1 }, + { "value":"13.png", "weight":1 }, + { "value":"14.png", "weight":1 }, + { "value":"15.png", "weight":1 }, + { "value":"16.png", "weight":1 }, + { "value":"17.png", "weight":1 }, + { "value":"18.png", "weight":1 }, + { "value":"19.png", "weight":1 }, + { "value":"2.png", "weight":1 }, + { "value":"20.png", "weight":1 }, + { "value":"21.png", "weight":1 }, + { "value":"22.png", "weight":1 }, + { "value":"23.png", "weight":1 }, + { "value":"24.png", "weight":1 }, + { "value":"25.png", "weight":1 }, + { "value":"26.png", "weight":1 }, + { "value":"27.png", "weight":1 }, + { "value":"28.png", "weight":1 }, + { "value":"29.png", "weight":1 }, + { "value":"3.png", "weight":1 }, + { "value":"30.png", "weight":1 }, + { "value":"31.png", "weight":1 }, + { "value":"32.png", "weight":1 }, + { "value":"33.png", "weight":1 }, + { "value":"34.png", "weight":1 }, + { "value":"35.png", "weight":1 }, + { "value":"36.png", "weight":1 }, + { "value":"37.png", "weight":1 }, + { "value":"38.png", "weight":1 }, + { "value":"39.png", "weight":1 }, + { "value":"4.png", "weight":1 }, + { "value":"40.png", "weight":1 }, + { "value":"41.png", "weight":1 }, + { "value":"42.png", "weight":1 }, + { "value":"43.png", "weight":1 }, + { "value":"44.png", "weight":1 }, + { "value":"45.png", "weight":1 }, + { "value":"46.png", "weight":1 }, + { "value":"47.png", "weight":1 }, + { "value":"48.png", "weight":1 }, + { "value":"49.png", "weight":1 }, + { "value":"5.png", "weight":1 }, + { "value":"50.png", "weight":1 }, + { "value":"51.png", "weight":1 }, + { "value":"52.png", "weight":1 }, + { "value":"53.png", "weight":1 }, + { "value":"54.png", "weight":1 }, + { "value":"55.png", "weight":1 }, + { "value":"56.png", "weight":1 }, + { "value":"57.png", "weight":1 }, + { "value":"58.png", "weight":1 }, + { "value":"59.png", "weight":1 }, + { "value":"6.png", "weight":1 }, + { "value":"60.png", "weight":1 }, + { "value":"61.png", "weight":1 }, + { "value":"62.png", "weight":1 }, + { "value":"63.png", "weight":1 }, + { "value":"64.png", "weight":1 }, + { "value":"65.png", "weight":1 }, + { "value":"66.png", "weight":1 }, + { "value":"67.png", "weight":1 }, + { "value":"68.png", "weight":1 }, + { "value":"69.png", "weight":1 }, + { "value":"7.png", "weight":1 }, + { "value":"70.png", "weight":1 }, + { "value":"71.png", "weight":1 }, + { "value":"72.png", "weight":1 }, + { "value":"73.png", "weight":1 }, + { "value":"74.png", "weight":1 }, + { "value":"75.png", "weight":1 }, + { "value":"76.png", "weight":1 }, + { "value":"77.png", "weight":1 }, + { "value":"78.png", "weight":1 }, + { "value":"79.png", "weight":1 }, + { "value":"8.png", "weight":1 }, + { "value":"80.png", "weight":1 }, + { "value":"81.png", "weight":1 }, + { "value":"82.png", "weight":1 }, + { "value":"83.png", "weight":1 }, + { "value":"84.png", "weight":1 }, + { "value":"85.png", "weight":1 }, + { "value":"86.png", "weight":1 }, + { "value":"87.png", "weight":1 }, + { "value":"88.png", "weight":1 }, + { "value":"89.png", "weight":1 }, + { "value":"9.png", "weight":1 }, + { "value":"90.png", "weight":1 }, + { "value":"91.png", "weight":1 }, + { "value":"92.png", "weight":1 }, + { "value":"93.png", "weight":1 }, + { "value":"94.png", "weight":1 }, + { "value":"95.png", "weight":1 }, + { "value":"96.png", "weight":1 }, + { "value":"a0.png", "weight":1 }, + { "value":"a1.png", "weight":1 }, + { "value":"a10.png", "weight":1 }, + { "value":"a11.png", "weight":1 }, + { "value":"a12.png", "weight":1 }, + { "value":"a13.png", "weight":1 }, + { "value":"a14.png", "weight":1 }, + { "value":"a15.png", "weight":1 }, + { "value":"a16.png", "weight":1 }, + { "value":"a17.png", "weight":1 }, + { "value":"a18.png", "weight":1 }, + { "value":"a19.png", "weight":1 }, + { "value":"a2.png", "weight":1 }, + { "value":"a20.png", "weight":1 }, + { "value":"a21.png", "weight":1 }, + { "value":"a22.png", "weight":1 }, + { "value":"a23.png", "weight":1 }, + { "value":"a24.png", "weight":1 }, + { "value":"a25.png", "weight":1 }, + { "value":"a26.png", "weight":1 }, + { "value":"a27.png", "weight":1 }, + { "value":"a28.png", "weight":1 }, + { "value":"a29.png", "weight":1 }, + { "value":"a3.png", "weight":1 }, + { "value":"a30.png", "weight":1 }, + { "value":"a31.png", "weight":1 }, + { "value":"a32.png", "weight":1 }, + { "value":"a33.png", "weight":1 }, + { "value":"a34.png", "weight":1 }, + { "value":"a35.png", "weight":1 }, + { "value":"a36.png", "weight":1 }, + { "value":"a37.png", "weight":1 }, + { "value":"a38.png", "weight":1 }, + { "value":"a39.png", "weight":1 }, + { "value":"a4.png", "weight":1 }, + { "value":"a40.png", "weight":1 }, + { "value":"a41.png", "weight":1 }, + { "value":"a5.png", "weight":1 }, + { "value":"a6.png", "weight":1 }, + { "value":"a7.png", "weight":1 }, + { "value":"a8.png", "weight":1 }, + { "value":"a9.png", "weight":1 }, +#}}} + ] + def from_random(self, url=TEST_URL): + return { + "image_url" : url, + "username" : USERNAME, + "pattern_url" : "{}/{}".format(PATTERN_URL_BASE, self._weighted_choice( self.weighted_pattern_url )), + } + def from_default(self, url=TEST_URL): + return { + "image_url" : url, + "username" : USERNAME, + "pattern_url" : "{}/{}".format(PATTERN_URL_BASE, self._default_choice( self.weighted_pattern_url )), + } diff --git a/Pb_Api/ImGradient/Params.py b/Pb_Api/ImGradient/Params.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Pb_Api/ImGradient/Params.py diff --git a/lib/imgradient.py b/Pb_Api/ImGradient/__init__.py index af743f1..18c27c0 100644 --- a/lib/imgradient.py +++ b/Pb_Api/ImGradient/__init__.py @@ -4,13 +4,13 @@ import urllib2 import simplejson as json import random import sys -from lib.utils import post_request, Service - -IMGRADIENT_URL = "http://asdf.us/im/api/imgradient" +from lib.utils import post_request, Pb_Api +IMGRADIENT_URL = "http://asdf.us/im/api/imgradient" +#from Api.ImGradient.Api import * -class ImGradient(Service): +class ImGradientApi(Pb_Api): def __init__(self): self.url = IMGRADIENT_URL self._required_keys = [ @@ -28,10 +28,3 @@ class ImGradient(Service): "gradienttype", "username", ] - def new(self, params): - for k in self._required_keys: - if not k in params: - params[k] = ""; - self.params = params - return json.loads(post_request(IMGRADIENT_URL, self.params)) - diff --git a/Pb_Api/ImGrid/Params.py b/Pb_Api/ImGrid/Params.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Pb_Api/ImGrid/Params.py diff --git a/Pb_Api/ImGrid/__init__.py b/Pb_Api/ImGrid/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Pb_Api/ImGrid/__init__.py diff --git a/Pb_Api/ImPattern/Params.py b/Pb_Api/ImPattern/Params.py new file mode 100644 index 0000000..9c86f7b --- /dev/null +++ b/Pb_Api/ImPattern/Params.py @@ -0,0 +1,18 @@ +#!/usr/bin/python2.7 +from Pb_Api.Params import Pb_Api_Params +from Pb_Api.Param.Username import Pb_Api_Param_Username +from Pb_Api.Param.String import Pb_Api_Param_String +from Pb_Api.Param.MultiSelect import Pb_Api_Param_MultiSelect + +pattern_url_options = [ + { "value":"1.png", "weight":1 }, + { "value":"2.png", "weight":1 }, +] +class ImPattern_Params(Pb_Api_Params): + def __init__(self): + self.params = [ + Pb_Api_Param_Username(name="username", required=1), + Pb_Api_Param_Image_Url(name="image_url", required=1), + Pb_Api_Param_MultiSelect(name="pattern_url", required=1, options=pattern_url_options + ), + ] diff --git a/Pb_Api/ImPattern/__init__.py b/Pb_Api/ImPattern/__init__.py new file mode 100755 index 0000000..f05cee9 --- /dev/null +++ b/Pb_Api/ImPattern/__init__.py @@ -0,0 +1,19 @@ +#!/usr/bin/python2.7 +import urllib +import urllib2 +import simplejson as json +import random +import sys +from lib.utils import post_request, Pb_Api + +IMPATTERN_URL = "http://asdf.us/im/api/impattern" + +#ok what about here? + +class ImPattern(Pb_Api): + def __init__(self): + self.url = IMPATTERN_URL + def params(self): + return ImPattern_Params() + + diff --git a/Pb_Api/Param/Image_Url.py b/Pb_Api/Param/Image_Url.py new file mode 100644 index 0000000..d199e11 --- /dev/null +++ b/Pb_Api/Param/Image_Url.py @@ -0,0 +1,7 @@ +from config import TEST_URL +from Pb_Api.Param.String import Pb_Api_Param_String + +class Pb_Api_Param_Image_Url(Pb_Api_Param_String): + def __init__(self, options): + super(Pb_Api_Param_Image_Url, self).__init__(*args, **kwargs) + self.value(TEST_URL, autogenerated=1) diff --git a/Pb_Api/Param/MultiSelect.py b/Pb_Api/Param/MultiSelect.py new file mode 100644 index 0000000..c482dfd --- /dev/null +++ b/Pb_Api/Param/MultiSelect.py @@ -0,0 +1,16 @@ +from Pb_Api.Param import Pb_Api_Param + +class Pb_Api_Param_MultiSelect(Pb_Api_Param): + def __init__(self, options): + super(Pb_Api_Param_MultiSelect, self).__init__(*args, **kwargs) + def options(self): + return self.options + def value(self, *args): + if len(args) > 1: + valid = False + for i in self.options: + if args[0] == i['value']: valid = True + if not valid : raise ValueError + super(Pb_Api_Param_MultiSelect, self).value(args[0]) + def randomize(self): + pass diff --git a/Pb_Api/Param/String.py b/Pb_Api/Param/String.py new file mode 100644 index 0000000..f7d873c --- /dev/null +++ b/Pb_Api/Param/String.py @@ -0,0 +1,3 @@ +class Pb_Api_Param_String(Pb_Api_Param): + def __init__(self, *args, **kwargs): + super(Pb_Api_Param_String, self).__init__(*args, **kwargs) diff --git a/Pb_Api/Param/Username.py b/Pb_Api/Param/Username.py new file mode 100644 index 0000000..80b445c --- /dev/null +++ b/Pb_Api/Param/Username.py @@ -0,0 +1,7 @@ +from config import USERNAME +from Pb_Api.Param.String import Pb_Api_Param_String + +class Pb_Api_Param_Username(Pb_Api_Param_String): + def __init__(self, options): + super(Pb_Api_Param_Username, self).__init__(*args, **kwargs) + self.value(USERNAME, autogenerated=1) diff --git a/Pb_Api/Param/__init__.py b/Pb_Api/Param/__init__.py new file mode 100644 index 0000000..af13a2a --- /dev/null +++ b/Pb_Api/Param/__init__.py @@ -0,0 +1,23 @@ +class Pb_Api_Param(object): + def name(self): + return self.name + def required(self): + return self.required + + def manually_set(self, *args): + if len(args) >= 1: + self._manually_set = args[0] + return self._manually_set + + def value(self, *args, **kwargs): + if len(args) >= 1: + self._value = args[0] + if not 'autogenerated' in kwargs: + self.manually_set(1) + self.is_ready(1) + return self._value + + def is_ready(self, *args): + if len(args) >= 1: + self._is_ready = args[0] + return self._is_ready or not self.required() diff --git a/Pb_Api/Params.py b/Pb_Api/Params.py new file mode 100644 index 0000000..b96a9af --- /dev/null +++ b/Pb_Api/Params.py @@ -0,0 +1,41 @@ +class Pb_Api_Params(object): + def _weighted_choice(self, param): + weights_total = sum(map(lambda x: x["weight"], param)) + choice = random.randint(0, weights_total) + position = 0 + for elem in param: + position += elem["weight"] + if position >= choice: + return elem["value"] + def _default_choice(self, param): + heaviest_idx = 0 + heaviest_weight = 0 + idx = 0 + for elem in param: + if elem["weight"] > heaviest_weight: + heaviest_weight = elem["weight"] + heaviest_idx = idx; + idx += 1 + return param[heaviest_idx]["value"] + def randomize(self): + for el in self.params: + if el.manually_set: + continue + el.randomize() + def is_ready(self): + for p in self.params(): + if not p.is_ready(): + return 0 + return 1 + def params(self): + return self._params + def param(self, name): + for p in self.params(): + if p.name() == name: + return p + return None + def as_hash(self): + result = {} + for p in self.params(): + result[p.name()] = p.value() + return result diff --git a/Pb_Api/__init__.py b/Pb_Api/__init__.py new file mode 100644 index 0000000..14bf18e --- /dev/null +++ b/Pb_Api/__init__.py @@ -0,0 +1,33 @@ +import urllib +import urllib2 +import sys +import random +import simplejson as json + +class Pb_Api: + def __init__(self): + self._required_keys = [] + self.url = "" + def new(self, params): + for k in self._required_keys: + if not k in params: + params[k] = ""; + self.current_params = params + return json.loads(post_request(self.url, self.current_params)) + def post_request(self, url, params): + params = urllib.urlencode(params) + sys.stderr.write(params) + headers = { + "Content-type": "application/x-www-form-urlencoded", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36", + "Accept": "text/plain" + } + try: + req = urllib2.Request(url, params, headers) + response = urllib2.urlopen(req) + return response.read() + except Exception as e: + sys.stderr.write(str(e)) + raise + def call(self, params): + return json.loads(self.post_request(self.url, params)) diff --git a/lib/imgradient_params_defaults.py b/lib/imgradient_params_defaults.py index 140939f..d0ba3ac 100644 --- a/lib/imgradient_params_defaults.py +++ b/lib/imgradient_params_defaults.py @@ -130,6 +130,48 @@ class ImGradientParams_FromDefaults(Pb_Api_Params): { "value" : "mirrored", "weight" : 1 }, { "value" : "noise", "weight" : 1 }, ] + +#I just needed a place to encapsulate the params and the weights, and methods to access them. I guess I didn't really know the right answer +#as far as the structure, so I tried to just imagine something somewhat related to what you were talking about before. +#do I need separate classes for the two methods below? +#well lets dicusss this a bit more, so services each have own param set, they are sort of original from service itself, so i guess there should be a way to get +#"fresh" copy of parameters accepted by this service, instead of rewriting code each time. do you mean just a list of the keynames? yeah and thier values +#the keys don't have default values necessarily. they just each have a range of accepted values. Basically just html forms hmm sort of like +#writing a bot that fills in some forms on the internet, like a bot to brute force a credit card form or something, funny example, but I guess a good one +#each parameter has values that are within an accepted range, like First Name would need to come from a list of frist names, and CC number would need to bevel +#intelligible ints, the first four should correspond with a known bank, etc. you understand what I mean? yeah +ok so since all services are about same, just a bunch of parameters and known values we can make base class for parameteres. it would look like: + + +class ApiParams(object): + def params(): + def randomize(): + def build(): + +class ImGradientParams(ApiParams): + def __init__(): + self.params = { + "width": # well here it's int i suppose, need somethig esle + "gradienttype": [ + { "value "...} + ] + } + +class ImGradientPb_Api(): + def params(): + return new ImGradientParams() + def call(params): + return image(); + + +api = ImGradientPb_Api() +image_params = api.params() +image_params.gradient_type("mirror") +image_params.randomize() +image = api.call(image _params) +# something like this, yeah I think so...lol I tried to do something like this + + def from_random(self): return { "username" : USERNAME, diff --git a/lib/impattern.py b/lib/impattern.py deleted file mode 100755 index 08e3f2a..0000000 --- a/lib/impattern.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python2.7 -import urllib -import urllib2 -import simplejson as json -import random -import sys -from lib.utils import post_request, Service - -IMPATTERN_URL = "http://asdf.us/im/api/impattern" - - - -class ImPattern(Service): - def __init__(self): - self.url = IMPATTERN_URL - self._required_keys = [ - "pattern_url", - "pattern_data", - "username", - "image_url", - ] - def new(self, params): - for k in self._required_keys: - if not k in params: - params[k] = ""; - self.params = params - return json.loads(post_request(IMPATTERN_URL, self.params)) - diff --git a/lib/impattern_params_defaults.py b/lib/impattern_params_defaults.py index cfad62f..c6b88b7 100644 --- a/lib/impattern_params_defaults.py +++ b/lib/impattern_params_defaults.py @@ -4,152 +4,10 @@ from config import USERNAME, TEST_URL from lib.utils import Pb_Api_Params PATTERN_URL_BASE = "http://asdf.us/impattern/patterns" class ImPatternParams_FromDefaults(Pb_Api_Params): -# "pattern_url", -# "pattern_data", -# "username", -# "image_url", def __init__(self): self.weighted_pattern_url = [ -#{{{pattern urls { "value":"1.png", "weight":1 }, - { "value":"10.png", "weight":1 }, - { "value":"11.png", "weight":1 }, - { "value":"12.png", "weight":1 }, - { "value":"13.png", "weight":1 }, - { "value":"14.png", "weight":1 }, - { "value":"15.png", "weight":1 }, - { "value":"16.png", "weight":1 }, - { "value":"17.png", "weight":1 }, - { "value":"18.png", "weight":1 }, - { "value":"19.png", "weight":1 }, { "value":"2.png", "weight":1 }, - { "value":"20.png", "weight":1 }, - { "value":"21.png", "weight":1 }, - { "value":"22.png", "weight":1 }, - { "value":"23.png", "weight":1 }, - { "value":"24.png", "weight":1 }, - { "value":"25.png", "weight":1 }, - { "value":"26.png", "weight":1 }, - { "value":"27.png", "weight":1 }, - { "value":"28.png", "weight":1 }, - { "value":"29.png", "weight":1 }, - { "value":"3.png", "weight":1 }, - { "value":"30.png", "weight":1 }, - { "value":"31.png", "weight":1 }, - { "value":"32.png", "weight":1 }, - { "value":"33.png", "weight":1 }, - { "value":"34.png", "weight":1 }, - { "value":"35.png", "weight":1 }, - { "value":"36.png", "weight":1 }, - { "value":"37.png", "weight":1 }, - { "value":"38.png", "weight":1 }, - { "value":"39.png", "weight":1 }, - { "value":"4.png", "weight":1 }, - { "value":"40.png", "weight":1 }, - { "value":"41.png", "weight":1 }, - { "value":"42.png", "weight":1 }, - { "value":"43.png", "weight":1 }, - { "value":"44.png", "weight":1 }, - { "value":"45.png", "weight":1 }, - { "value":"46.png", "weight":1 }, - { "value":"47.png", "weight":1 }, - { "value":"48.png", "weight":1 }, - { "value":"49.png", "weight":1 }, - { "value":"5.png", "weight":1 }, - { "value":"50.png", "weight":1 }, - { "value":"51.png", "weight":1 }, - { "value":"52.png", "weight":1 }, - { "value":"53.png", "weight":1 }, - { "value":"54.png", "weight":1 }, - { "value":"55.png", "weight":1 }, - { "value":"56.png", "weight":1 }, - { "value":"57.png", "weight":1 }, - { "value":"58.png", "weight":1 }, - { "value":"59.png", "weight":1 }, - { "value":"6.png", "weight":1 }, - { "value":"60.png", "weight":1 }, - { "value":"61.png", "weight":1 }, - { "value":"62.png", "weight":1 }, - { "value":"63.png", "weight":1 }, - { "value":"64.png", "weight":1 }, - { "value":"65.png", "weight":1 }, - { "value":"66.png", "weight":1 }, - { "value":"67.png", "weight":1 }, - { "value":"68.png", "weight":1 }, - { "value":"69.png", "weight":1 }, - { "value":"7.png", "weight":1 }, - { "value":"70.png", "weight":1 }, - { "value":"71.png", "weight":1 }, - { "value":"72.png", "weight":1 }, - { "value":"73.png", "weight":1 }, - { "value":"74.png", "weight":1 }, - { "value":"75.png", "weight":1 }, - { "value":"76.png", "weight":1 }, - { "value":"77.png", "weight":1 }, - { "value":"78.png", "weight":1 }, - { "value":"79.png", "weight":1 }, - { "value":"8.png", "weight":1 }, - { "value":"80.png", "weight":1 }, - { "value":"81.png", "weight":1 }, - { "value":"82.png", "weight":1 }, - { "value":"83.png", "weight":1 }, - { "value":"84.png", "weight":1 }, - { "value":"85.png", "weight":1 }, - { "value":"86.png", "weight":1 }, - { "value":"87.png", "weight":1 }, - { "value":"88.png", "weight":1 }, - { "value":"89.png", "weight":1 }, - { "value":"9.png", "weight":1 }, - { "value":"90.png", "weight":1 }, - { "value":"91.png", "weight":1 }, - { "value":"92.png", "weight":1 }, - { "value":"93.png", "weight":1 }, - { "value":"94.png", "weight":1 }, - { "value":"95.png", "weight":1 }, - { "value":"96.png", "weight":1 }, - { "value":"a0.png", "weight":1 }, - { "value":"a1.png", "weight":1 }, - { "value":"a10.png", "weight":1 }, - { "value":"a11.png", "weight":1 }, - { "value":"a12.png", "weight":1 }, - { "value":"a13.png", "weight":1 }, - { "value":"a14.png", "weight":1 }, - { "value":"a15.png", "weight":1 }, - { "value":"a16.png", "weight":1 }, - { "value":"a17.png", "weight":1 }, - { "value":"a18.png", "weight":1 }, - { "value":"a19.png", "weight":1 }, - { "value":"a2.png", "weight":1 }, - { "value":"a20.png", "weight":1 }, - { "value":"a21.png", "weight":1 }, - { "value":"a22.png", "weight":1 }, - { "value":"a23.png", "weight":1 }, - { "value":"a24.png", "weight":1 }, - { "value":"a25.png", "weight":1 }, - { "value":"a26.png", "weight":1 }, - { "value":"a27.png", "weight":1 }, - { "value":"a28.png", "weight":1 }, - { "value":"a29.png", "weight":1 }, - { "value":"a3.png", "weight":1 }, - { "value":"a30.png", "weight":1 }, - { "value":"a31.png", "weight":1 }, - { "value":"a32.png", "weight":1 }, - { "value":"a33.png", "weight":1 }, - { "value":"a34.png", "weight":1 }, - { "value":"a35.png", "weight":1 }, - { "value":"a36.png", "weight":1 }, - { "value":"a37.png", "weight":1 }, - { "value":"a38.png", "weight":1 }, - { "value":"a39.png", "weight":1 }, - { "value":"a4.png", "weight":1 }, - { "value":"a40.png", "weight":1 }, - { "value":"a41.png", "weight":1 }, - { "value":"a5.png", "weight":1 }, - { "value":"a6.png", "weight":1 }, - { "value":"a7.png", "weight":1 }, - { "value":"a8.png", "weight":1 }, - { "value":"a9.png", "weight":1 }, -#}}} ] def from_random(self, url=TEST_URL): return { diff --git a/lib/utils.py b/lib/utils.py index 78a7b1f..98b7c98 100644 --- a/lib/utils.py +++ b/lib/utils.py @@ -1,50 +1,2 @@ -import urllib -import urllib2 -import sys -import random -def post_request(url, params): - params = urllib.urlencode(params) - sys.stderr.write(params) - headers = { - "Content-type": "application/x-www-form-urlencoded", - "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36", - "Accept": "text/plain" - } - try: - req = urllib2.Request(url, params, headers) - response = urllib2.urlopen(req) - return response.read() - except Exception as e: - sys.stderr.write(str(e)) - raise -class Service: - def __init__(self): - self._required_keys = [] - self.url = "" - def new(self, params): - for k in self._required_keys: - if not k in params: - params[k] = ""; - self.current_params = params - return json.loads(post_request(self.url, self.current_params)) - -class Pb_Api_Params: - def _weighted_choice(self, param): - weights_total = sum(map(lambda x: x["weight"], param)) - choice = random.randint(0, weights_total) - position = 0 - for elem in param: - position += elem["weight"] - if position >= choice: - return elem["value"] - def _default_choice(self, param): - heaviest_idx = 0 - heaviest_weight = 0 - idx = 0 - for elem in param: - if elem["weight"] > heaviest_weight: - heaviest_weight = elem["weight"] - heaviest_idx = idx; - idx += 1 - return param[heaviest_idx]["value"] +#ok so does the way the files have been moved, split apart and renamed look good to you? almost, need to rename a bit more. as for this utils, there is only one method to call, so you can just put it inside Pb_Api.py it will be easier @@ -1,14 +1,25 @@ #!/usr/bin/python2.7 from lib.dumpfm import DumpFmImageSearch -from lib.imbreak_params_defaults import ImBreakParams_FromDefaults -from lib.imgradient_params_defaults import ImGradientParams_FromDefaults -from lib.imgrid_params_defaults import ImGridParams_FromDefaults -from lib.impattern_params_defaults import ImPatternParams_FromDefaults -from lib.imbreak import ImBreak -from lib.impattern import ImPattern -from lib.imgradient import ImGradient -from lib.imgrid import ImGrid +#ok so lib.dumpfm is completely different and I'll ask about that later + + +from Pb_Api.ImGradient import * +from Pb_Api.ImGrid import * + +#ok how do I initialize and load params? + +imgradient = Pb_Api_ImGradient() #this class name doesn't seem right though, is that what we named it? no, but should be about that, i'm using perl style, so path and class name basically same, we can't use dot in class name, so i'm using _. why can't we use the dot in the classname? i think it's syntax error. hmm should we test real quick? sure +params = imgradient.params() +params.param("gradienttype").value("mirror") +#params.randomize() +params.defaults() # if I want defaults? yep +result = imgradient.call(params) +# seomthing like this + +#this is how they are getting imported...does it look a little bit weird? +#well it fine, what about all those services, imbreak, imgradient, can they be sort of merged together? not really +#they take very different parameters...that's the issue, but the service call, I made a master class for it imbreak = ImBreak() imgradient = ImGradient() imgrid = ImGrid() diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..4754b1c --- /dev/null +++ b/test2.py @@ -0,0 +1,13 @@ +#!/usr/bin/python2.7 +from Pb_Api.ImPattern import Pb_Api_ImPattern + +p = Pb_Api_ImPattern() +params = p.params() +params.param("image_url").value("http://hello") +pprint.pprint(params.as_hash()) +# for now +#and then to call it, just below? yes are the params set, or do I pass them in? + +#p.call(params) #something like this? +#and inside .call() you need to use .as_hash() to pass to post request. ok good +#alright I'll test this on my own time. sort of a lot. Thanks again no problems |
