##// END OF EJS Templates
simplify float/int slider abbreviation logic...
MinRK -
Show More
@@ -52,28 +52,24 b' def _get_min_max_value(min, max, value=None, step=None):'
52 min, max, value = 0.0, 1.0, 0.5
52 min, max, value = 0.0, 1.0, 0.5
53 elif value == 0:
53 elif value == 0:
54 min, max, value = 0, 1, 0
54 min, max, value = 0, 1, 0
55 elif isinstance(value, float):
55 elif isinstance(value, (int, float)):
56 min, max = (-value, 3.0*value) if value > 0 else (3.0*value, -value)
57 elif isinstance(value, int):
58 min, max = (-value, 3*value) if value > 0 else (3*value, -value)
56 min, max = (-value, 3*value) if value > 0 else (3*value, -value)
59 else:
57 else:
60 raise TypeError('expected a number, got: %r' % value)
58 raise TypeError('expected a number, got: %r' % value)
61 else:
59 else:
62 raise ValueError('unable to infer range, value from: ({0}, {1}, {2})'.format(min, max, value))
60 raise ValueError('unable to infer range, value from: ({0}, {1}, {2})'.format(min, max, value))
63 if step:
61 if step is not None:
64 # ensure value is on a step
62 # ensure value is on a step
65 r = (value - min) % step
63 r = (value - min) % step
66 value = value - r
64 value = value - r
67 return min, max, value
65 return min, max, value
68
66
69 def _widget_abbrev_single_value(o):
67 def _widget_abbrev_single_value(o):
70 """Make widgets from single values, which can be used written as parameter defaults."""
68 """Make widgets from single values, which can be used as parameter defaults."""
71 if isinstance(o, string_types):
69 if isinstance(o, string_types):
72 return TextWidget(value=unicode_type(o))
70 return TextWidget(value=unicode_type(o))
73 elif isinstance(o, dict):
71 elif isinstance(o, dict):
74 # get a single value in a Python 2+3 way:
72 return DropdownWidget(values=o)
75 value = next(iter(o.values()))
76 return DropdownWidget(value=value, values=o)
77 elif isinstance(o, bool):
73 elif isinstance(o, bool):
78 return CheckboxWidget(value=o)
74 return CheckboxWidget(value=o)
79 elif isinstance(o, float):
75 elif isinstance(o, float):
@@ -87,25 +83,27 b' def _widget_abbrev_single_value(o):'
87
83
88 def _widget_abbrev(o):
84 def _widget_abbrev(o):
89 """Make widgets from abbreviations: single values, lists or tuples."""
85 """Make widgets from abbreviations: single values, lists or tuples."""
86 float_or_int = (float, int)
90 if isinstance(o, (list, tuple)):
87 if isinstance(o, (list, tuple)):
91 if _matches(o, (int, int)):
88 if o and all(isinstance(x, string_types) for x in o):
89 return DropdownWidget(values=[unicode_type(k) for k in o])
90 elif _matches(o, (float_or_int, float_or_int)):
92 min, max, value = _get_min_max_value(o[0], o[1])
91 min, max, value = _get_min_max_value(o[0], o[1])
93 return IntSliderWidget(value=value, min=min, max=max)
92 if all(isinstance(_, int) for _ in o):
94 elif _matches(o, (int, int, int)):
93 cls = IntSliderWidget
95 min, max, value = _get_min_max_value(o[0], o[1], step=o[2])
94 else:
96 return IntSliderWidget(value=value, min=min, max=max, step=o[2])
95 cls = FloatSliderWidget
97 elif _matches(o, (float, float)):
96 return cls(value=value, min=min, max=max)
98 min, max, value = _get_min_max_value(o[0], o[1])
97 elif _matches(o, (float_or_int, float_or_int, float_or_int)):
99 return FloatSliderWidget(value=value, min=min, max=max)
98 step = o[2]
100 elif _matches(o, (float, float, float)):
99 if step <= 0:
101 min, max, value = _get_min_max_value(o[0], o[1], step=o[2])
100 raise ValueError("step must be >= 0, not %r" % step)
102 return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
101 min, max, value = _get_min_max_value(o[0], o[1], step=step)
103 elif _matches(o, (float, float, int)):
102 if all(isinstance(_, int) for _ in o):
104 min, max, value = _get_min_max_value(o[0], o[1], step=o[2])
103 cls = IntSliderWidget
105 return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
104 else:
106 elif all(isinstance(x, string_types) for x in o):
105 cls = FloatSliderWidget
107 return DropdownWidget(value=unicode_type(o[0]),
106 return cls(value=value, min=min, max=max, step=step)
108 values=[unicode_type(k) for k in o])
109 else:
107 else:
110 return _widget_abbrev_single_value(o)
108 return _widget_abbrev_single_value(o)
111
109
General Comments 0
You need to be logged in to leave comments. Login now