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/__init__.py

    r450 r456  
    193193            global_dict.update(update_namespace) 
    194194 
     195         
     196        D=None 
    195197        try: 
    196198            D = eval(s, global_dict) 
    197         except SyntaxError, e: 
     199        except SyntaxError as e: 
    198200            raise SyntaxError("Invalid string for ParameterSet definition: %s\n%s" % (s,e)) 
     201             
    199202        return D or {} 
    200203     
     
    229232                pstr = f.read() 
    230233                self._url = initialiser 
     234 
     235                 
    231236            except IOError: 
    232237                pstr = initialiser 
     
    235240                f.close() 
    236241 
    237             initialiser = ParameterSet.read_from_str(pstr,update_namespace) 
     242 
     243            # is it a yaml url? 
     244            if self._url: 
     245                import urlparse, os.path 
     246                o = urlparse.urlparse(self._url) 
     247                base,ext = os.path.splitext(o.path) 
     248                if ext in ['.yaml','.yml']: 
     249                    import yaml 
     250                    initialiser = yaml.load(pstr) 
     251                else: 
     252                    initialiser = ParameterSet.read_from_str(pstr,update_namespace) 
     253            else: 
     254                initialiser = ParameterSet.read_from_str(pstr,update_namespace) 
     255 
    238256         
    239257        # By this stage, `initialiser` should be a dict. Iterate through it, 
     
    297315        return dict.__getitem__(self,split[0])[split[1]] 
    298316 
     317    def flat_add(self,name,value): 
     318        """ Like __setitem__, but it will add ParametSet({}) objects 
     319        into the namespace tree if needed. """ 
     320 
     321        split = name.split('.',1) 
     322        if len(split)==1: 
     323            dict.__setitem__(self,name,value) 
     324        else: 
     325            # nested set 
     326            try: 
     327                ps = dict.__getitem__(self,split[0]) 
     328            except KeyError: 
     329                # setting nested name without parent existing 
     330                # create parent 
     331                ps = ParameterSet({}) 
     332                dict.__setitem__(self,split[0],ps) 
     333                # and try again 
     334            ps.flat_add(split[1],value) 
     335 
    299336    def __setitem__(self,name,value): 
    300337        """ Modified set that detects dots '.' in the names and goes down the 
     
    306343        else: 
    307344            # nested set 
    308             try: 
    309                 dict.__getitem__(self,split[0])[split[1]]=value 
    310             except KeyError: 
    311                 # setting nested name without parent existing 
    312                 # create parent 
    313                 dict.__setitem__(self,split[0],{}) 
    314                 # and try again 
    315                 dict.__getitem__(self,split[0])[split[1]]=value 
     345            dict.__getitem__(self,split[0])[split[1]]=value 
     346 
    316347    
    317348    # should __len__() be the usual dict length, or the flattened length? Probably the former for consistency with dicts