adding attributes to a python product
Hi folks, I have a Python Product that I'm developing. During the course of development, I want to add a new attribute. All new instances get this attribute, as it is defined with a default value in the constructor. In addition, all instances that get edited via the web get the attribute, as the edit-processing method is defined to have a default value for this attribute. Is there any way of interacting with the ZODB persistence machinery to add the default attribute to all instances as they are brought out of persistent storage -- so that I can just restart Zope, and have all of my instances updated as I use them ? I can't find the right method or whatever in the ZODB on-line docs, or in the source. Thanks for any help. -- Steve Alexander Software Engineer Cat-Box limited http://www.cat-box.net
Hello, Just uploaded version 0.2.3 of the CachePool product. It fixes a serious bug wich crashed zope (a little too often) among some other improvements. For those of you who don't know, this product caches pages in an array, dependent of the calling URL and the query string. you can get it at http://www.zope.org/Members/neli/CachePool ---- Carlos Neves cneves@ruido-visual.pt
Steve, If the attribute is immutable, just define it as a 'class' variable, rather than in the constructor. e.g., class Foo(.. ): newAttribute = 'hello?' You can of course *change* it in the constructor for new object, but all your old objects will just use the class variable. If it's mutable.. then you need to fix it in __setstate__(). -steve
"Steve" == Steve Alexander <steve@cat-box.net> writes:
Steve> Hi folks, I have a Python Product that I'm Steve> developing. During the course of development, I want to add Steve> a new attribute. All new instances get this attribute, as Steve> it is defined with a default value in the constructor. Steve> In addition, all instances that get edited via the web get Steve> the attribute, as the edit-processing method is defined to Steve> have a default value for this attribute. Steve> Is there any way of interacting with the ZODB persistence Steve> machinery to add the default attribute to all instances as Steve> they are brought out of persistent storage -- so that I can Steve> just restart Zope, and have all of my instances updated as Steve> I use them ? Steve> I can't find the right method or whatever in the ZODB Steve> on-line docs, or in the source. Steve> Thanks for any help. Steve> -- Steve Alexander Software Engineer Cat-Box limited Steve> http://www.cat-box.net Steve> _______________________________________________ Zope-Dev Steve> maillist - Zope-Dev@zope.org Steve> http://lists.zope.org/mailman/listinfo/zope-dev ** No cross Steve> posts or HTML encoding! ** (Related lists - Steve> http://lists.zope.org/mailman/listinfo/zope-announce Steve> http://lists.zope.org/mailman/listinfo/zope )
On Thu, 29 Jun 2000, Steve Alexander wrote:
Hi folks,
Is there any way of interacting with the ZODB persistence machinery to add the default attribute to all instances as they are brought out of persistent storage -- so that I can just restart Zope, and have all of my instances updated as I use them ?
In addition to the method Bryan detailed, you should look into the __setstate__() method.
"When an object is activated by the object database and brought into memory, it's __setstate__() method is called. A Persistent class can override this method to initialize the object every time it is brought into memory. __setstate__() is also useful to upgrade an object from one version to another. If you add instance attributes to your product, older versions of the instances of that product will not have the new attribute. __setstate__ can check for the existance of new attributes, and create them with sane defaults in older versions of the instance."
Thanks for any help.
Hope this helps. --Jeff --- Jeff K. Hoffman 704.849.0731 x108 Chief Technology Officer mailto:jeff.hoffman@goingv.com Going Virtual, L.L.C. http://www.goingv.com/
participants (4)
-
Carlos Neves -
Jeff K. Hoffman -
Steve Alexander -
Steve Spicklemire