[Zope3-Users] Infinite recursion when storing FieldProperties in as annotations (using IAttributeAnnotations).

Alec Munro alecmunro at gmail.com
Mon Aug 29 10:07:06 EDT 2005


On 8/28/05, Stephan Richter <srichter at cosmos.phy.tufts.edu> wrote:
> On Wednesday 24 August 2005 15:22, Alec Munro wrote:
> > KEY = "demographicinfo"
> >
> > def getterAndSetter(key):
> > def getter(self):
> > return self.mapping[key]
> > def setter(self, obj):
> > self.mapping[key] = obj
> > return getter, setter
> > 
> > class DemographicInfo(object):
> > implements(IDemographicInfo)
> > 
> > def getAnnotations(self):
> > annotations = IAnnotations(self.context)
> > return annotations
> 
> This is overkill. You only call this method in the constructor.
> 
I subclass this for a principal as well, in which case I override the
above method, which seemed the most obvious way to do it.

....

> > blank['birth_date'] =
> > FieldProperty(IDemographicInfo['birth_date']) blank['sex'] =
> > FieldProperty(IDemographicInfo['sex'])
> 
> I am 80% sure this is your problem. Properties are meant to be used as
> attributes on classes, not as items in a dictionary/mapping.
>
It works without properties, so I am sure you are right. 
> > mapping = annotations[KEY] = blank
> > self.mapping = mapping
> > 
> > birth_date = property(*getterAndSetter('birth_date'))
> > sex = property(*getterAndSetter('sex'))
> 
> This all seems convoluted to me. I think you would be better off with a
> __getattr__ and __setattr__ in the adapter that manually does the validation.
> 
I suppose I could do something like:

def __setattr__(self, name, obj):
    if self.mapping.has_key(name):
        IDemographicInfo[name].validate(obj)
        self.mapping[name] = obj
def __getattr__(self, name):
    if self.mapping.has_key(name):
        return self.mapping[name]

birth_date = IDemographicInfo['birth_date'].default
sex = IDemographicInfo['sex'].default

That seems like it might get close to the same effect. I've never user
__getattr__ and __setattr__ before, so if you see any downfalls, or
there are any cautions I should take, please let me know.

Thanks for your response,

Alec


More information about the Zope3-users mailing list