[CentralOH] Data Members / Properties - 2 questions

Mark Erbaugh mark at microenh.com
Sun Feb 14 03:54:18 CET 2010


Hello all,

I'm reworking some older code, changing it from using a cobbled together database based on shelves to use SQLITE3.

I have several data objects stored as data members of a container object. These data members were read from the shelve when the container was created and saved when a save() method of the container was called. When the program needed the data is just accessed the member:  value = object.SHOW_QTY or object.SHOW_QTY = value

Now the data lives in the SQLITE database. I would like to read and write the data from the database on demand.  It seems like a perfect place for a property.  For the above example, instead of SHOW_QTY being a data member it would be a property, thus the client code could still use value = object.SHOW_QTY and object.SHOW_QTY = value.

The problem I have is that in some places, the existing code accesses these data members indirectly passing a string version of the data name (i.e. 'SHOW_QTY').  With a data member, it's a simple matter to look up the data in the container's __dict__.  However, it doesn't look like properties work the same way. At one time I had a semi understanding of how descriptors worked under the hood, but I've been away from this level of Python for a while...

If you have an easy answer - great! If not, can you point me to a good reference to refresh my understanding?

The answer to this question may also help with the second.  In creating these properties, I ended up with a lot of repetitive getters and setters.  The only difference was a parameter indicating which data element to use:

>     def _getSHOW_QTY(self):
>         return self._getAUDIT('SHOW_QTY')
>     
>     def _setSHOW_QTY(self, data):
>         self._setAUDIT('SHOW_QTY', data)
>         
>     def _getBY_PILL(self):
>         return self._getAUDIT('BY_PILL')
>     
>     def _setBY_PILL(self, data):
>         self._setAUDIT('BY_PILL', data)

>     SHOW_QTY = property(_getSHOW_QTY, _setSHOW_QTY)
>     BY_PILL = property(_getBY_PILL, _setBY_PILL)

There are several more properties like this. Is there a way to iterate through a sequence (i.e. (SHOW_QTY, BY_PILL, ...)) and have the getters, setters and properties created automatically?

Thanks,
Mark


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20100213/fe10d8f3/attachment.htm>


More information about the CentralOH mailing list