Structure accessible by attribute name or index

2010-03-17 Thread Wes Santee
I am very new to Python, and trying to figure out how to create an
object that has values that are accessible either by attribute name,
or by index.  For example, the way os.stat() returns a stat_result or
pwd.getpwnam() returns a struct_passwd.

In trying to figure it out, I've only come across C implementations of
the above types.  Nothing specifically in Python.  What is the Python
native way to create this kind of object?

I apologize if this has been widely covered already.  In searching for
an answer, I must be missing some fundamental concept that is
excluding me from finding an answer.

Cheers,
-Wes
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Structure accessible by attribute name or index

2010-03-17 Thread Wes Santee
On Mar 17, 11:14 am, Christian Heimes  wrote:
> Wes Santee wrote:
> > I am very new to Python, and trying to figure out how to create an
> > object that has values that are accessible either by attribute name,
> > or by index.  For example, the way os.stat() returns a stat_result or
> > pwd.getpwnam() returns a struct_passwd.
>
> > In trying to figure it out, I've only come across C implementations of
> > the above types.  Nothing specifically in Python.  What is the Python
> > native way to create this kind of object?
>
> > I apologize if this has been widely covered already.  In searching for
> > an answer, I must be missing some fundamental concept that is
> > excluding me from finding an answer.
>
> You can't use the same implementation as the result object of os.stat()
> and others. However Python 2.6 has a new factory that creates a similar
> datatype called named 
> tuple:http://docs.python.org/library/collections.html#namedtuple-factory-fu...

Thanks for the pointer.  At least I know there is a reason why I
wasn't finding a solution. :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Structure accessible by attribute name or index

2010-03-17 Thread Wes Santee
On Mar 17, 11:34 am, Bruno Desthuilliers  wrote:
> Wes Santee a écrit :
>
> > I am very new to Python, and trying to figure out how to create an
> > object that has values that are accessible either by attribute name,
> > or by index.  For example, the way os.stat() returns a stat_result or
> > pwd.getpwnam() returns a struct_passwd.
>
> > In trying to figure it out, I've only come across C implementations of
> > the above types.  Nothing specifically in Python.  What is the Python
> > native way to create this kind of object?
>
> Using the appropriate __magicmethods__ for indexed access and computed
> attributes for the attribute access might be a good solution:

This is as good an excuse as any to get familiar with the rest of the
__magicmethods__. :)

>
> # warning : Q&D implementation, would require some sanity checks.
>
> class IndexedValueDescriptor(object):
>      def __init__(self, index):
>          self._index = index
>      def __get__(self, instance, cls):
>          if instance is None:
>              return self
>          return instance[self._index]
>      def __set__(self, instance, value):
>          instance[self._index] = value
>
> class Structure(object):
>      def __init__(self, value1, value2, value3):
>          self._values = [value1, value2, value3]
>      def __setitem__(self, index, value):
>          self._values[index] = value
>      def __getitem__(self, index):
>          return self._values[index]
>      value1 = IndexedValueDescriptor(0)
>      value2 = IndexedValueDescriptor(1)
>      value3 = IndexedValueDescriptor(2)
>
> Note that there are probably other solutions... Don't know how
> os.stat_result is implemented, might be worth looking at the source
> code. But anyway : the above should get you started.
>
> > I apologize if this has been widely covered already.   In searching for
> > an answer, I must be missing some fundamental concept that is
> > excluding me from finding an answer.
>
> Well, that's not really a FAQ AFAICT !-)

-- 
http://mail.python.org/mailman/listinfo/python-list