diff options
Diffstat (limited to 'tutorius/constraints.py')
-rw-r--r-- | tutorius/constraints.py | 68 |
1 files changed, 60 insertions, 8 deletions
diff --git a/tutorius/constraints.py b/tutorius/constraints.py index 455ede3..e6c942f 100644 --- a/tutorius/constraints.py +++ b/tutorius/constraints.py @@ -205,21 +205,73 @@ class FileConstraint(Constraint): raise FileConstraintError("Non-existing file : %s"%value) return +class IntCheckConstraintError(Exception):####################################################### + pass + +class IntCheckConstraint(Constraint):########################################################### + """ + Ensures that the value can be converted into an Int + """ + def validate(self, value): + try: + int(value)#la fonction int() ne peut convertir que des nombres ou des string sinon error + except: + raise IntCheckConstraintError("The value can't be converted into an Int") + return + +class FloatCheckConstraintError(Exception):####################################################### + pass + +class FloatCheckConstraint(Constraint):########################################################### + """ + Ensures that the value can be converted into a float + """ + def validate(self, value): + try: + float(value)#la fonction float() ne peut convertir que des nombres ou des string sinon error (ex pas de int([0,0])) + except: + raise FloatCheckConstraintError("The value can't be converted into a float") + return + +class ListIntCheckConstraintError(Exception):##################################################### + pass + +class ListIntCheckConstraint(Constraint):######################################################### + """ + Ensures that the value is a tuple of int or can be converted into this + """ + def validate(self, value): + try: + if len(value)==0: + raise IntCheckConstraint("Tuple is empty") + for i in range(len(value)): + int(value[i])#la fonction int() ne peut convertir que des nombres ou des string sinon error (ex pas de int([0,0])) + except: + raise IntCheckConstraintError("The value can't be converted into an int") + return + +class TypeConstraint(Constraint):################################################################ + """ + A Type constraint contains a OneType member that can be used in a children + class as a basic string. ex : SameTypeConstraint + """ + def __init__(self, one_type): + self.one_type = one_type -class SameTypeConstraintError(Exception): +class SameTypeConstraintError(Exception):######################################################## pass -class SameTypeConstraint(Constraint): +class SameTypeConstraint(TypeConstraint):######################################################## """ Ensures that the list is not empty and that properties of the list have the same type. """ - def validate(self, value): - if len(value)==0: - raise SameTypeConstraintError("The list is empty") - for i in value: - if i.type != value[0].type: - raise SameTypeConstraintError("Properties have diffferent type") + if self.one_type is not None: + if len(value)==0: + raise SameTypeConstraintError("The list is empty") + for i in value: + if type(i) != self.one_type: + raise SameTypeConstraintError("Properties have diffferent type") return |