Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion enaml/stdlib/fields.enaml
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
)
51 changes: 51 additions & 0 deletions enaml/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be Typed(complex) ?


#: The maximum value allowed for the float, inclusive, or None if
#: there is no upper bound.
maximum = Typed(float)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be Typed(complex) ?


#: 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like tabs for indentation on this line - should be 4 spaces


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no blank line

maximum = self.maximum

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no blank line

if maximum is not None and (value.real > maximum or value.imag > maximum):
return False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no blank line

if not self.allow_exponent and 'e' in text.lower():
return False

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no blank line

return True


class RegexValidator(Validator):
""" A concrete Validator which handles text input.

Expand Down