Skip to content
Snippets Groups Projects
Commit 5488df08 authored by Paul McCarthy's avatar Paul McCarthy
Browse files

A little bit of property type tweaking; list still broken

parent 2c342ace
No related branches found
No related tags found
No related merge requests found
...@@ -6,10 +6,9 @@ ...@@ -6,10 +6,9 @@
# Author: Paul McCarthy <pauldmccarthy@gmail.com> # Author: Paul McCarthy <pauldmccarthy@gmail.com>
# #
import os
import os.path as op import os.path as op
from collections import OrderedDict import numbers
import matplotlib.colors as mplcolors import matplotlib.colors as mplcolors
import matplotlib.cm as mplcm import matplotlib.cm as mplcm
...@@ -30,8 +29,8 @@ class Boolean(props.PropertyBase): ...@@ -30,8 +29,8 @@ class Boolean(props.PropertyBase):
def validate(self, instance, value): def validate(self, instance, value):
props.PropertyBase.validate(self, instance, value) props.PropertyBase.validate(self, instance, value)
try: value = bool(value) if not isinstance(value, bool):
except: raise ValueError('Must be a boolean') raise ValueError('Must be a boolean')
class Number(props.PropertyBase): class Number(props.PropertyBase):
...@@ -68,6 +67,9 @@ class Number(props.PropertyBase): ...@@ -68,6 +67,9 @@ class Number(props.PropertyBase):
def validate(self, instance, value): def validate(self, instance, value):
props.PropertyBase.validate(self, instance, value) props.PropertyBase.validate(self, instance, value)
if not isinstance(value, numbers.Number):
raise ValueError('Must be a number')
minval = self.getConstraint(instance, 'minval') minval = self.getConstraint(instance, 'minval')
maxval = self.getConstraint(instance, 'maxval') maxval = self.getConstraint(instance, 'maxval')
...@@ -94,15 +96,16 @@ class Int(Number): ...@@ -94,15 +96,16 @@ class Int(Number):
def validate(self, instance, value): def validate(self, instance, value):
try: value = int(value)
except: raise ValueError('Must be an integer ({})'.format(value))
Number.validate(self, instance, value) Number.validate(self, instance, value)
if not isinstance(value, numbers.Integral):
raise ValueError('Must be an integer')
class Double(Number): class Double(Number):
""" """
A property which encapsulates a double. A property which encapsulates a double. TODO Double is a silly name.
Change it to Real.
""" """
def __init__(self, **kwargs): def __init__(self, **kwargs):
...@@ -114,12 +117,12 @@ class Double(Number): ...@@ -114,12 +117,12 @@ class Double(Number):
def validate(self, instance, value): def validate(self, instance, value):
try: value = float(value)
except: raise ValueError('Must be a number ({})'.format(value))
Number.validate(self, instance, value) Number.validate(self, instance, value)
if not isinstance(value, numbers.Real):
raise ValueError('Must be a floating point number')
class Percentage(Double): class Percentage(Double):
""" """
...@@ -171,11 +174,12 @@ class String(props.PropertyBase): ...@@ -171,11 +174,12 @@ class String(props.PropertyBase):
if value is None: return if value is None: return
if not isinstance(value, basestring):
raise ValueError('Must be a string')
minlen = self.getConstraint(instance, 'minlen') minlen = self.getConstraint(instance, 'minlen')
maxlen = self.getConstraint(instance, 'maxlen') maxlen = self.getConstraint(instance, 'maxlen')
value = str(value)
if minlen is not None and len(value) < minlen: if minlen is not None and len(value) < minlen:
raise ValueError('Must have length at least {}'.format(minlen)) raise ValueError('Must have length at least {}'.format(minlen))
...@@ -233,8 +237,6 @@ class Choice(String): ...@@ -233,8 +237,6 @@ class Choice(String):
""" """
String.validate(self, instance, value) String.validate(self, instance, value)
value = str(value)
if value not in self.choices: if value not in self.choices:
raise ValueError('Invalid choice ({})'.format(value)) raise ValueError('Invalid choice ({})'.format(value))
...@@ -742,6 +744,6 @@ class ColourMap(props.PropertyBase): ...@@ -742,6 +744,6 @@ class ColourMap(props.PropertyBase):
elif not isinstance(value, mplcolors.Colormap): elif not isinstance(value, mplcolors.Colormap):
raise ValueError( raise ValueError(
'Invalid ColourMap value: '.format( 'Invalid ColourMap value: '.format(
default.__class__.__name__)) value.__class__.__name__))
props.PropertyBase.__set__(self, instance, value) props.PropertyBase.__set__(self, instance, value)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment