##// END OF EJS Templates
Validation on the python side
Jonathan Frederic -
Show More
@@ -38,14 +38,29 b' class _BoundedFloat(_Float):'
38 38 def __init__(self, *pargs, **kwargs):
39 39 """Constructor"""
40 40 super(_BoundedFloat, self).__init__(*pargs, **kwargs)
41 self._validate('value', None, self.value)
42 self.on_trait_change(self._validate, ['value', 'min', 'max'])
41 self._value_changed('value', None, self.value)
42 self._min_changed('min', None, self.min)
43 self._max_changed('max', None, self.max)
43 44
44 def _validate(self, name, old, new):
45 """Validate value, max, min."""
45 def _value_changed(self, name, old, new):
46 """Validate value."""
46 47 if self.min > new or new > self.max:
47 48 self.value = min(max(new, self.min), self.max)
48 49
50 def _max_changed(self, name, old, new):
51 """Make sure the min is always <= the max."""
52 if new < self.min:
53 raise ValueError("setting max < min")
54 if new < self.value:
55 self.value = new
56
57 def _min_changed(self, name, old, new):
58 """Make sure the max is always >= the min."""
59 if new > self.max:
60 raise ValueError("setting min > max")
61 if new > self.value:
62 self.value = new
63
49 64
50 65 @register('IPython.FloatText')
51 66 class FloatText(_Float):
@@ -41,24 +41,28 b' class _BoundedInt(_Int):'
41 41 def __init__(self, *pargs, **kwargs):
42 42 """Constructor"""
43 43 super(_BoundedInt, self).__init__(*pargs, **kwargs)
44 self.on_trait_change(self._validate_value, ['value'])
45 self.on_trait_change(self._handle_max_changed, ['max'])
46 self.on_trait_change(self._handle_min_changed, ['min'])
44 self._value_changed('value', None, self.value)
45 self._min_changed('min', None, self.min)
46 self._max_changed('max', None, self.max)
47 47
48 def _validate_value(self, name, old, new):
48 def _value_changed(self, name, old, new):
49 49 """Validate value."""
50 50 if self.min > new or new > self.max:
51 51 self.value = min(max(new, self.min), self.max)
52 52
53 def _handle_max_changed(self, name, old, new):
53 def _max_changed(self, name, old, new):
54 54 """Make sure the min is always <= the max."""
55 55 if new < self.min:
56 56 raise ValueError("setting max < min")
57 if new < self.value:
58 self.value = new
57 59
58 def _handle_min_changed(self, name, old, new):
60 def _min_changed(self, name, old, new):
59 61 """Make sure the max is always >= the min."""
60 62 if new > self.max:
61 63 raise ValueError("setting min > max")
64 if new > self.value:
65 self.value = new
62 66
63 67 @register('IPython.IntText')
64 68 class IntText(_Int):
General Comments 0
You need to be logged in to leave comments. Login now