Validation on the python side

Jonathan Frederic 11 years ago committed by Sylvain Corlay
parent 6192c520b5
commit 11b0dbfd25

@ -38,14 +38,29 @@ class _BoundedFloat(_Float):
def __init__(self, *pargs, **kwargs):
"""Constructor"""
super(_BoundedFloat, self).__init__(*pargs, **kwargs)
self._validate('value', None, self.value)
self.on_trait_change(self._validate, ['value', 'min', 'max'])
self._value_changed('value', None, self.value)
self._min_changed('min', None, self.min)
self._max_changed('max', None, self.max)
def _validate(self, name, old, new):
"""Validate value, max, min."""
def _value_changed(self, name, old, new):
"""Validate value."""
if self.min > new or new > self.max:
self.value = min(max(new, self.min), self.max)
def _max_changed(self, name, old, new):
"""Make sure the min is always <= the max."""
if new < self.min:
raise ValueError("setting max < min")
if new < self.value:
self.value = new
def _min_changed(self, name, old, new):
"""Make sure the max is always >= the min."""
if new > self.max:
raise ValueError("setting min > max")
if new > self.value:
self.value = new
@register('IPython.FloatText')
class FloatText(_Float):

@ -41,24 +41,28 @@ class _BoundedInt(_Int):
def __init__(self, *pargs, **kwargs):
"""Constructor"""
super(_BoundedInt, self).__init__(*pargs, **kwargs)
self.on_trait_change(self._validate_value, ['value'])
self.on_trait_change(self._handle_max_changed, ['max'])
self.on_trait_change(self._handle_min_changed, ['min'])
self._value_changed('value', None, self.value)
self._min_changed('min', None, self.min)
self._max_changed('max', None, self.max)
def _validate_value(self, name, old, new):
def _value_changed(self, name, old, new):
"""Validate value."""
if self.min > new or new > self.max:
self.value = min(max(new, self.min), self.max)
def _handle_max_changed(self, name, old, new):
def _max_changed(self, name, old, new):
"""Make sure the min is always <= the max."""
if new < self.min:
raise ValueError("setting max < min")
if new < self.value:
self.value = new
def _handle_min_changed(self, name, old, new):
def _min_changed(self, name, old, new):
"""Make sure the max is always >= the min."""
if new > self.max:
raise ValueError("setting min > max")
if new > self.value:
self.value = new
@register('IPython.IntText')
class IntText(_Int):

Loading…
Cancel
Save