Show
Ignore:
Timestamp:
08/25/10 17:28:08 (21 months ago)
Author:
emuller
Message:

ParameterSchema? and basic validation implemented.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/parameter_set_schema_validation/src/parameters/validators.py

    r450 r456  
    2424 
    2525    def validate(self, leaf): 
    26         return issubclass(leaf, type) 
     26        return isinstance(leaf, self.type) 
    2727 
    2828    def __repr__(self): 
     
    7777            value = x[1] 
    7878            if isinstance(value, SchemaBase): 
    79                 self[key] = value 
     79                self.flat_add(key,value) 
    8080            else: 
    81                 self[key] = Subclass(type=type(value)) 
    82  
    83  
    84  
    85 # Schema ParameterSet types 
    86  
    87  
    88  
    89  
    90 class CongruencyValidator: 
    91     pass 
    92  
    93  
    94  
     81                self.flat_add(key,Subclass(type=type(value))) 
     82 
     83 
     84 
     85 
     86class ValidationError(Exception): 
     87    def __init__(self, path='',schema_base=None, parameter=None ): 
     88        self.path = path 
     89        self.schema_base = schema_base 
     90        self.parameter = parameter 
     91    def __str__(self): 
     92        return 'validation error @ %s: parameter "%s" failed against schema: %s' % (self.path, self.parameter, self.schema_base) 
     93     
     94 
     95 
     96class CongruencyValidator(object): 
     97    """ 
     98    A CongruencyValidator validates a ParameterSet against a ParameterSchema 
     99    either returning True, or raising a ValidationError with the path, SchemaBase subclass 
     100    and parameter value for which the validation failed. 
     101 
     102    The CongruencyValidator expects all names defined in the schema to be present in the parameter set 
     103    and vice-versa, and will run validation for each item in the namespace tree. 
     104     
     105    The validation functionality is available via the "validate" member 
     106    CongruencyValidator.validate(parameter_set, parameter_schema) 
     107 
     108    example: 
     109 
     110    validator = CongruencyValidator() 
     111 
     112    try: 
     113       validator.validate(parameter_set,parameter_schema) 
     114    except ValidationError, e: 
     115        
     116 
     117    """ 
     118 
     119    def __init__(self): 
     120        pass 
     121 
     122 
     123    def validate(self, parameter_set, parameter_schema): 
     124        """ 
     125        CongruencyValidator.validate(parameter_set, parameter_schema) 
     126         
     127        validates a ParameterSet against a ParameterSchema 
     128        either returning True, or raising a ValidationError with the path and SchemaBase subclass 
     129        for which validation failed. 
     130 
     131 
     132        expects all names defined in the schema to be present in the parameter set 
     133        and vice-versa, and will run validation for each item in the namespace tree. 
     134 
     135        See als0: CongruencyValidator docstring. 
     136 
     137        """ 
     138 
     139        ps = parameter_set 
     140        schema = parameter_schema 
     141 
     142        ps_keys = set() 
     143        schema_keys = set() 
     144 
     145        for path,sb in schema.flat(): 
     146            try: 
     147                val = ps[path] 
     148            except KeyError: 
     149                e = ValidationError(path=path,schema_base=sb,parameter='<MISSING>') 
     150                raise e 
     151            if not sb.validate(val): 
     152                e = ValidationError(path=path,schema_base=sb,parameter=val) 
     153                raise e 
     154 
     155         
     156        for path,val in ps.flat(): 
     157            try: 
     158                sb = schema[path] 
     159            except KeyError: 
     160                e = ValidationError(path=path,schema_base='<MISSING>',parameter=val) 
     161                raise e 
     162             
     163        return True 
     164         
     165             
    95166 
    96167def congruent_dicts(template, candidate, subset=False,parent_path=''): 
     
    149220NeuroTools.parameters.Subclass = Subclass 
    150221NeuroTools.parameters.CongruencyValidator = CongruencyValidator 
    151  
    152  
    153      
     222NeuroTools.parameters.ValidationError = ValidationError 
     223 
     224 
     225 
     226 
     227