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