Ridley5 opened this issue on Jul 26, 2010 · 1724 posts
adp001 posted Sat, 21 August 2010 at 8:29 AM
Being compatible with different flavours is nice :)
Here is a parameter wrapper to use parameters additionally as attributes than as dictionary elements:
from Parameters import Parameters as pdict
class Parameters(object):
def __init__(self):
self.dict = pdict() # include dictionary
def __getattr__(self, name):
if name in ("read", "write", "get") : # local attributes
return getattr(self.dict, name)
if name == "dict" :
return self.__dict__[name]
return self.dict.get(name)
def __setattr__(self, name, value):
if name == "dict" : # local attribute
self.__dict__[name] = value
else:
self.dict[name] = value
################################################################
if __name__ == "__main__" :
p = Parameters()
p.read("../AIR/LuxPose/data/dataOut.bbml")
camera = p.get("Camera")
film = p.get("Film")
renderer = p.get("Renderer")
print "CAMERA as dict:", p.get("Camera", "No Camera found")
print "FILM as dict:", p.get("Film", "No Film found")
print "RENDERER as dict:", p.get("Renderer", "No Renderer Found")
print
print "Camera as attribute:", p.CaMeRa # attributes ignore upper-/lowercase
print "Film as attribute:", p.Film
print "RENDERER as attribute:", p.renderer
print
print 'camera.has_key() reports "cameratype" is %sincluded in p.camera' % ("not ","")[p.camera.has_key("cameratype")]
print
print "Camera contains this attributes/keys", p.camera.keys()
p.write("../AIR/LuxPose/data/dataOut-copy.bbml")