- Timestamp:
- 08/25/10 17:28:08 (21 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/parameter_set_schema_validation/src/parameters/__init__.py
r450 r456 193 193 global_dict.update(update_namespace) 194 194 195 196 D=None 195 197 try: 196 198 D = eval(s, global_dict) 197 except SyntaxError ,e:199 except SyntaxError as e: 198 200 raise SyntaxError("Invalid string for ParameterSet definition: %s\n%s" % (s,e)) 201 199 202 return D or {} 200 203 … … 229 232 pstr = f.read() 230 233 self._url = initialiser 234 235 231 236 except IOError: 232 237 pstr = initialiser … … 235 240 f.close() 236 241 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 238 256 239 257 # By this stage, `initialiser` should be a dict. Iterate through it, … … 297 315 return dict.__getitem__(self,split[0])[split[1]] 298 316 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 299 336 def __setitem__(self,name,value): 300 337 """ Modified set that detects dots '.' in the names and goes down the … … 306 343 else: 307 344 # 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 316 347 317 348 # should __len__() be the usual dict length, or the flattened length? Probably the former for consistency with dicts
