[CentralOH] Data Members / Properties - 2 questions
Nick Albright
nick.albright at gmail.com
Sun Feb 14 04:44:33 CET 2010
Oh sorry! I answered too fast/missed the 2nd half of your Q. Initially I'd
think that a metaclass would be the answer to allowing you to change/effect
the class dynamically.
-Nick
On Sat, Feb 13, 2010 at 10:41 PM, Nick Albright <nick.albright at gmail.com>wrote:
> Hey Mark!
>
> I think you can use getattr() and setattr() instead of __dict__ and it'll
> catch the property.
>
> I hope that helps! (And I hope my memory is right ;)
> -Nick
>
> On Sat, Feb 13, 2010 at 9:54 PM, Mark Erbaugh <mark at microenh.com> wrote:
>
>> 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
>>
>>
>>
>> _______________________________________________
>> CentralOH mailing list
>> CentralOH at python.org
>> http://mail.python.org/mailman/listinfo/centraloh
>>
>>
>
>
> --
> Please note that as of 1/20 I no longer have a land phone line, only my
> cell.
>
--
Please note that as of 1/20 I no longer have a land phone line, only my
cell.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/centraloh/attachments/20100213/ed946215/attachment.htm>
More information about the CentralOH
mailing list