[CentralOH] Data Members / Properties - 2 questions

Kartic Krishnamurthy kartic.krish at gmail.com
Sun Feb 14 06:05:57 CET 2010


Mark - I have spent the last hour trying to figure out the solution to
your problem, like Nick says, using a  metaclass.

The easy part is creating the getters and setters...what's stumped me
is making the descriptor call the correct argument-specific version of
{get,set}AUDIT :)

I will post my code once I have something working.

+Kartic

On Sat, Feb 13, 2010 at 7:44 PM, Nick Albright <nick.albright at gmail.com> wrote:
> 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.
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> http://mail.python.org/mailman/listinfo/centraloh
>
>



-- 

Samuel Goldwyn  - "I'm willing to admit that I may not always be
right, but I am never wrong." -
http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html


More information about the CentralOH mailing list