From e6bb4d31ac122c6d435c530a09d33d9f451385e9 Mon Sep 17 00:00:00 2001 From: AndyGreenwell Date: Tue, 31 Mar 2015 20:52:13 -0400 Subject: [PATCH 1/3] Update fields.enaml Adding ComplexField enamldef to define a field that will accept and display a complex number. Corresponding changes made to validator.py to add a ComplexValidator. --- enaml/stdlib/fields.enaml | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/enaml/stdlib/fields.enaml b/enaml/stdlib/fields.enaml index b978c64cd..6f8f8cdb5 100644 --- a/enaml/stdlib/fields.enaml +++ b/enaml/stdlib/fields.enaml @@ -10,7 +10,7 @@ This is a library of Enaml components deriving from the builtin Field. """ -from enaml.validator import RegexValidator, IntValidator, FloatValidator +from enaml.validator import RegexValidator, IntValidator, FloatValidator, ComplexValidator from enaml.widgets.field import Field @@ -58,3 +58,19 @@ enamldef FloatField(Field): validator << FloatValidator( minimum=minimum, maximum=maximum, allow_exponent=allow_exponent ) + + +enamldef ComplexField(Field): + """ A Field that only accept complex floating point values. + + """ + attr minimum = None + attr maximum = None + attr allow_exponent : bool = True + attr value : complex = 0.0 + 0.0*1j + attr converter = unicode + text << converter(value) + text :: self.value = complex(text) + validator << ComplexValidator( + minimum=minimum, maximum=maximum, allow_exponent=allow_exponent + ) From 05c2b22ef4d0c686462422758d0ea822e6c561ba Mon Sep 17 00:00:00 2001 From: AndyGreenwell Date: Tue, 31 Mar 2015 20:55:49 -0400 Subject: [PATCH 2/3] Update validator.py Adding ComplexValidator that is used in conjunction with the ComplexField definition added to fields.enaml --- enaml/validator.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/enaml/validator.py b/enaml/validator.py index c12c999df..28661b4e4 100644 --- a/enaml/validator.py +++ b/enaml/validator.py @@ -151,6 +151,57 @@ def validate(self, text): return True +class ComplexValidator(Validator): + """ A concrete Validator which handles complex floating point input. + + This validator ensures that the text represents a complex floating point + number within a specified range. + + """ + #: The minimum value allowed for the float, inclusive, or None if + #: there is no lower bound. + minimum = Typed(float) + + #: The maximum value allowed for the float, inclusive, or None if + #: there is no upper bound. + maximum = Typed(float) + + #: Whether or not to allow exponents like '1e6' in the input. + allow_exponent = Bool(True) + + def validate(self, text): + """ Validates the given text matches the complex float range. + + Parameters + ---------- + text : unicode + The unicode text edited by the client widget. + + Returns + ------- + result : bool + True if the text is valid, False otherwise. + + """ + try: + value = complex(text) + except ValueError: + return False + minimum = self.minimum + if minimum is not None and (value.real < minimum or value.imag < minimum): + return False + + maximum = self.maximum + + if maximum is not None and (value.real > maximum or value.imag > maximum): + return False + + if not self.allow_exponent and 'e' in text.lower(): + return False + + return True + + class RegexValidator(Validator): """ A concrete Validator which handles text input. From 2b7bada149a27ae2ccd4abbbddab4b61ee415073 Mon Sep 17 00:00:00 2001 From: AndyGreenwell Date: Wed, 1 Apr 2015 12:14:37 -0400 Subject: [PATCH 3/3] Update validator.py Addressing comments by @sccolbert --- enaml/validator.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/enaml/validator.py b/enaml/validator.py index 28661b4e4..f710605cc 100644 --- a/enaml/validator.py +++ b/enaml/validator.py @@ -160,11 +160,11 @@ class ComplexValidator(Validator): """ #: The minimum value allowed for the float, inclusive, or None if #: there is no lower bound. - minimum = Typed(float) + minimum = Typed(complex) #: The maximum value allowed for the float, inclusive, or None if #: there is no upper bound. - maximum = Typed(float) + maximum = Typed(complex) #: Whether or not to allow exponents like '1e6' in the input. allow_exponent = Bool(True) @@ -189,16 +189,12 @@ def validate(self, text): return False minimum = self.minimum if minimum is not None and (value.real < minimum or value.imag < minimum): - return False - + return False maximum = self.maximum - if maximum is not None and (value.real > maximum or value.imag > maximum): return False - if not self.allow_exponent and 'e' in text.lower(): return False - return True