ComputedAttribute, Persitent and "too many datafull base classes"
Hi, In a product I am writing, I am trying to add a ComputedAttribute to an instance of a Folder descendant class. This is the code I use: def addComputedAttribute(self, attributeName, sourceCode): methodName = '_ca_get%s' % attributeName self.addMethod(methodName, sourceCode) setattr(self, attributeName, ContextComputedAttribute(self.__class__.__dict__[methodName])) where at first I defined ContextComputed Attribute like this: import ComputedAttribute _ComputedAttribute = ComputedAttribute.ComputedAttribute del ComputedAttribute class ContextComputedAttribute(_ComputedAttribute): pass This put the Folder in a bad state : when I access it later, the Zope instance crashes. So I supposed I needed to use a persistent Attribute and I tried with ContextComputedAttribute class defined like this: class ContextComputedAttribute(Persistent): def __init__(self, test): pass In this case, the Folder stays in a good state but it does not fullfill the requirements as ContextComputedAttribute does not descend from the real Zope ComputedAttribute class. My problem is the following : I cannot define a class like this: import ComputedAttribute _ComputedAttribute = ComputedAttribute.ComputedAttribute del ComputedAttribute class ContextComputedAttribute(_ComputedAttribute, Persistent): pass In this case, I get the following error TypeError: too many datafull base classes which has to do with too many ExtensionClass derivation... Can someone tell me how to define a class with both ComputedAttribute and Persistent behaviours ? Thanks a lot. -- Godefroid Chapelle BubbleNet sprl rue Victor Horta, 30 1348 Louvain-la-Neuve Belgium Tel + 32 (10) 457490 Mob + 32 (477) 363942 TVA 467 093 008 RC Niv 49849
At 03:44 PM 10/20/01 +0200, Godefroid Chapelle wrote:
Hi,
In a product I am writing, I am trying to add a ComputedAttribute to an instance of a Folder descendant class.
This is the code I use:
def addComputedAttribute(self, attributeName, sourceCode): methodName = '_ca_get%s' % attributeName self.addMethod(methodName, sourceCode) setattr(self, attributeName, ContextComputedAttribute(self.__class__.__dict__[methodName]))
I think the problem you're having is that the method object being used in the last line (i.e., self.__class__.__dict__[methodName]) is not persistent. If it is a Python function, it will not be saved persistently within the computed attribute. This is probably why the Zope instance crashes at a later time. This doesn't have anything to do with whether ComputedAttribute is Persistent. It doesn't need to be, which is a good thing because you can't derive a class from both (as you've discovered). You can write a Python version of ComputedAttribute by deriving only from Persistent, but as I said there is really no need to do that. You need the function to be persistent, not the ComputedAttribute itself. If the function is currently a pure-Python function, you need to replace it with some kind of callable object that can be pickled, such as a PythonScript. You could also write your own class to do this, but again it is not the ComputedAttribute part that would cause a problem.
participants (2)
-
Godefroid Chapelle -
Phillip J. Eby