Jan-10-2019, 06:35 AM
i added an __init__() method which handles zero or more arguments of dict/attrdict or list/tuple of zero or more key:value 2-sequence pairs.
class attrdict(dict): '''Class that is like a dictionary with items usable like attributes. #--------------------------------------------------------------- # purpose class that is a dictionary with items usable # like attributes # # init usage object = attrdict(dictionary) # object = attrdict(dictionary,key=value...) # object = attrdict(key=value...) # # attr usage object.name # # dict usage object[key] # # note attribute usage is like string keys that are # limited to what can be a valid identifier. # # thanks [email protected] #--------------------------------------------------------------- ''' def __init__(self,*args,**opts): arn = 0 for arg in args: arn += 1 if isinstance(arg,(attrdict,dict)): self.update(arg) elif arg and isinstance(arg,(list,tuple)): an = -1 for ar in arg: an += 1 if isinstance(ar,(list,tuple)) and len(ar)==2: self[ar[0]] = ar[1] else: raise TypeError('not a 2-sequence at ['+str(an)+'] of argument '+str(arn)) else: raise TypeError('argument '+str(arn)+' is not a sequence') if opts: if isinstance(opts,(attrdict,dict)): self.update(opts) else: raise TypeError('options ('+repr(opts)+') is not a dictionary') def __getattr__(self, key): return self[key] def __setattr__(self, key, value): self[key] = value
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.