Re: [Zope-dev] adding attributes to a python product
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 <[EMAIL PROTECTED]> 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 - [EMAIL PROTECTED] 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 ) ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
Re: [Zope-dev] adding attributes to a python product
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. >From http://www.zope.org/Members/michel/HowTos/ZODB-How-To: "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:[EMAIL PROTECTED] Going Virtual, L.L.C. http://www.goingv.com/ ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
RE: [Zope-dev] adding attributes to a python product
> 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. Steve, The "best way" to do this is to define the new attribute (with its default value) as a class attribute. That way when you ask for the attribute on old instances you'll get the default value found in the class. If you change the attribute (through the web or whatever), the changed attribute will be saved *in the instance*. That way you don't even have to bother setting a default in the constructor (though it won't really hurt anything to do so). For example: # old class class Spam: """A spam object""" def cook(self): return 'cooked!' # new class - we want to add a 'color' attribute that defaults # to 'pink' and to be able to change the color, but we need old # instances to support this too. class Spam: """A spam object""" def cook(self): return 'cooked!' # class attribute - doing a getattr on 'color' will find # this for old instances. color='pink' def setColor(self, color='green'): # if this is called, the color attr will be saved in the # *instance*. The class default will not change. self.color=color Note that there is one caveat to this approach - if the attr that you want a default for is a _mutable_ Python object (like a dict or list), then you have to be careful not to modify the class attribute directly. In those cases you need to do a little more work: class Spam: """A spam object""" # A dict of spam flavors. flavors={} def addFlavor(self, name, desc): # We have to be careful here. We can't just say # 'self.flavors[name]=desc', because if we are an old # instance then self.flavors will give us the 'flavors' # *class attribute*. We don't want to mutate that, what # we really want to do is create flavors as an instance # attribute if we are getting the default. # set dict to either the 'flavors' from the _instance_ or # a fresh new dictionary. dict=self.__dict__.get('flavors', {}) # add the entry dict[name]=desc # this is important - the Zope persistence machinery can't # automatically know about changes to mutable builtin Python # types like dictionaries, so we have do a setattr on self # to make sure the changes to the dictionary are saved! self.flavors=dict Hope this helps! Brian Lloyd[EMAIL PROTECTED] Software Engineer 540.371.6909 Digital Creations http://www.digicool.com ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )
[Zope-dev] 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 ___ Zope-Dev maillist - [EMAIL PROTECTED] http://lists.zope.org/mailman/listinfo/zope-dev ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope )