junapp
October 20th, 2010, 03:20 PM
Hi,
Using google app engine where an object has properties which are accessed through the standard dot notation.
Using an inventory system of computers as an example
item.formfactor # returns 'slim form'
If one of the properties of a model is a reference property of another model, expanding the dot notation gives the value of the related object.
item.manufacturer.name # returns 'dell'
I have a need for some generic purposes to use getattr
myProp = "formfactor"
getattr(object,myProp) # returns 'slim form' as desired.
myProp = "manufacturer.name"
getattr(object,myProp) # returns an error.
however
getattr(getattr(object,"manufacturer"),"name") # returns dell
My question is how do I manage this? I'd like to have a wrapper of some sort mygetattr which expands the prop argument to chain the getattr calls in this fashion, but can't quite seem to piece it together. Any pointers?
def mygetattr(obj,prop):
# need to amend to allow a dot delimited list in prop
return getattr(obj,prop)
Using google app engine where an object has properties which are accessed through the standard dot notation.
Using an inventory system of computers as an example
item.formfactor # returns 'slim form'
If one of the properties of a model is a reference property of another model, expanding the dot notation gives the value of the related object.
item.manufacturer.name # returns 'dell'
I have a need for some generic purposes to use getattr
myProp = "formfactor"
getattr(object,myProp) # returns 'slim form' as desired.
myProp = "manufacturer.name"
getattr(object,myProp) # returns an error.
however
getattr(getattr(object,"manufacturer"),"name") # returns dell
My question is how do I manage this? I'd like to have a wrapper of some sort mygetattr which expands the prop argument to chain the getattr calls in this fashion, but can't quite seem to piece it together. Any pointers?
def mygetattr(obj,prop):
# need to amend to allow a dot delimited list in prop
return getattr(obj,prop)