diff options
| author | Pepper <pepper@scannerjammer.com> | 2015-03-08 18:25:42 -0400 |
|---|---|---|
| committer | Pepper <pepper@scannerjammer.com> | 2015-03-08 18:25:42 -0400 |
| commit | d9651ca5fb6c655afbe88a014c8bb8a8f000a70d (patch) | |
| tree | cdd939a8a8192fe6f6b439a6098ad55b56dabf0f /Pb_Api/ImGrid/Params.py | |
| parent | 193178ea8a038b183388999a2853433cf45a054c (diff) | |
fixes
Diffstat (limited to 'Pb_Api/ImGrid/Params.py')
| -rw-r--r-- | Pb_Api/ImGrid/Params.py | 50 |
1 files changed, 40 insertions, 10 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 ), |
