##// END OF EJS Templates
Added casting versions of all the basic traitlet types.
Brian Granger -
Show More
@@ -660,6 +660,7 b' class Any(TraitletType):'
660
660
661
661
662 class Int(TraitletType):
662 class Int(TraitletType):
663 """A integer traitlet."""
663
664
664 evaluate = int
665 evaluate = int
665 default_value = 0
666 default_value = 0
@@ -670,8 +671,18 b' class Int(TraitletType):'
670 return value
671 return value
671 self.error(obj, value)
672 self.error(obj, value)
672
673
674 class CInt(Int):
675 """A casting version of the int traitlet."""
676
677 def validate(self, obj, value):
678 try:
679 return int(value)
680 except:
681 self.error(obj, value)
682
673
683
674 class Long(TraitletType):
684 class Long(TraitletType):
685 """A long integer traitlet."""
675
686
676 evaluate = long
687 evaluate = long
677 default_value = 0L
688 default_value = 0L
@@ -685,7 +696,18 b' class Long(TraitletType):'
685 self.error(obj, value)
696 self.error(obj, value)
686
697
687
698
699 class CLong(Long):
700 """A casting version of the long integer traitlet."""
701
702 def validate(self, obj, value):
703 try:
704 return long(value)
705 except:
706 self.error(obj, value)
707
708
688 class Float(TraitletType):
709 class Float(TraitletType):
710 """A float traitlet."""
689
711
690 evaluate = float
712 evaluate = float
691 default_value = 0.0
713 default_value = 0.0
@@ -699,7 +721,17 b' class Float(TraitletType):'
699 self.error(obj, value)
721 self.error(obj, value)
700
722
701
723
724 class CFloat(Float):
725 """A casting version of the float traitlet."""
726
727 def validate(self, obj, value):
728 try:
729 return float(value)
730 except:
731 self.error(obj, value)
732
702 class Complex(TraitletType):
733 class Complex(TraitletType):
734 """A traitlet for complex numbers."""
703
735
704 evaluate = complex
736 evaluate = complex
705 default_value = 0.0 + 0.0j
737 default_value = 0.0 + 0.0j
@@ -713,7 +745,18 b' class Complex(TraitletType):'
713 self.error(obj, value)
745 self.error(obj, value)
714
746
715
747
748 class CComplex(Complex):
749 """A casting version of the complex number traitlet."""
750
751 def validate (self, obj, value):
752 try:
753 return complex(value)
754 except:
755 self.error(obj, value)
756
757
716 class Str(TraitletType):
758 class Str(TraitletType):
759 """A traitlet for strings."""
717
760
718 evaluate = lambda x: x
761 evaluate = lambda x: x
719 default_value = ''
762 default_value = ''
@@ -725,7 +768,21 b' class Str(TraitletType):'
725 self.error(obj, value)
768 self.error(obj, value)
726
769
727
770
771 class CStr(Str):
772 """A casting version of the string traitlet."""
773
774 def validate(self, obj, value):
775 try:
776 return str(value)
777 except:
778 try:
779 return unicode(value)
780 except:
781 self.error(obj, value)
782
783
728 class Unicode(TraitletType):
784 class Unicode(TraitletType):
785 """A traitlet for unicode strings."""
729
786
730 evaluate = unicode
787 evaluate = unicode
731 default_value = u''
788 default_value = u''
@@ -739,8 +796,18 b' class Unicode(TraitletType):'
739 self.error(obj, value)
796 self.error(obj, value)
740
797
741
798
742 class Bool(TraitletType):
799 class CUnicode(Unicode):
800 """A casting version of the unicode traitlet."""
743
801
802 def validate(self, obj, value):
803 try:
804 return unicode(value)
805 except:
806 self.error(obj, value)
807
808
809 class Bool(TraitletType):
810 """A boolean (True, False) traitlet."""
744 evaluate = bool
811 evaluate = bool
745 default_value = False
812 default_value = False
746 info_text = 'a boolean'
813 info_text = 'a boolean'
@@ -750,3 +817,12 b' class Bool(TraitletType):'
750 return value
817 return value
751 self.error(obj, value)
818 self.error(obj, value)
752
819
820
821 class CBool(Bool):
822 """A casting version of the boolean traitlet."""
823
824 def validate(self, obj, value):
825 try:
826 return bool(value)
827 except:
828 self.error(obj, value) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now