Using __*attr__ with __*item__
me at home.com
me at home.com
Wed Aug 7 22:45:13 EDT 2002
More information about the Python-list mailing list
Wed Aug 7 22:45:13 EDT 2002
- Previous message (by thread): Using __*attr__ with __*item__
- Next message (by thread): Python and Zope Developer Available
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
I will be providing a data structure-like interface to some raw
data, so using __getattr__ and __setattr__ are just what I need to use.
The problem comes when I need to operate as if I have an array of
elements in this data.
Each class that is instantiated has an instance of a data block
that is operates on via __getattr__ and __setattr__. This works well
because the attribute name helps to determine where the data is
located, and how it is to be accessed. When I have an array of related
data in the block, I would like to use __getitem__ and __setitem__.
The problem is that __*item__ does not get the attribute name passed to
it, just the index range and potential values. Because of this, I have
no way to know how to access the buffer. The class is defined
something like:
class foo:
def __init__ ():
self.data = get the data buffer
def __setattr__ (self, attr, value):
load 'value' into self.data per 'attr'
def __getattr__ (self, attr):
read value from sefl.data per 'attr' and return it
def __setitem__ (self, item, values):
### Can't determine which of the possible arrays are being
### referenced
def __getitem__ (self, item):
### Can't determine which of the possible arrays are being
### referenced
# end class foo
If I move the __*item__ routines into their own package, then all
visibility to self.data is lost.
I have a work around, but it bothers me because it looks too much
like a bad hack. The solution I will be trying is to move the
__*item__ routines into their own class. Then define EACH element of
the array as a unique attribute. Once this is done, instantiate the
array class with these elements and pass them BACK INTO the structure
class. Something like this:
class foo:
def __init__ (self, other_params):
self.data = get the data buffer
def __setattr__ (self, attr, value):
load 'value' into self.data per 'attr'
def __getattr__ (self, attr):
read value from sefl.data per 'attr' and return it
def set_array (self, array_attribute, the_array):
self.__dict__[array_attribute] = the_array
# end class foo
class bar:
def __init__ (self, array_element_list):
self.the_array = array_element_list
def __setitem__ (self, item, values):
for index in range (item.start, item.stop):
# this should trap to the proper __setattr__ in class foo
self.the_array [index] = values [index - item.start]
def __getitem__ (self, item):
the_answer = []
for index in range (item.start, item.stop):
# this should trap to the proper __getattr__ in class foo
the_answer.append (self.the_array [index])
# end class bar
FOO = foo()
the_array = bar([FOO.array_element_1, FOO.array_element_2, ...])
FOO.set_array ("array_name", the_array)
Because of how I am creating FOO, I can hide the instantiation of
bar, but it still bothers me.
Any thoughts as to a better way to do this?
Thanks,
Jonathan Polley
jwpolley at rockwellcollins dot com
- Previous message (by thread): Using __*attr__ with __*item__
- Next message (by thread): Python and Zope Developer Available
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list