property collections 
Author Message
 property collections

I'm looking for ways to spell property collection ie

PC.attr1 = value1
PC.attr2 = value2

PC[i].attr1 = value

...

if PC[i].attr hasn't been explicitly set then the default is found on
the un-indexed thing.

My solutions seem to involve elements which need to refer to the parent
and thus create a loop.
--
Robin Becker



Thu, 06 Nov 2003 21:54:27 GMT  
 property collections

Quote:

> I'm looking for ways to spell property collection ie

> PC.attr1 = value1
> PC.attr2 = value2

> PC[i].attr1 = value

> ...

> if PC[i].attr hasn't been explicitly set then the default is found on
> the un-indexed thing.

> My solutions seem to involve elements which need to refer to the parent
> and thus create a loop.

I don't think this creates a loop:

class PropertyCollection:
        class Properties:
                def __init__(self, dict):
                        self.__dict__.update(dict)
        def __init__(self):
                self.__dict__['attrs'] = {}
                self.__dict__['i_attrs'] = {}
        def __getitem__(self, i):
                if not self.i_attrs.has_key(i):
                        self.i_attrs[i] = self.Properties(self.attrs)
                return self.i_attrs[i]
        def __getattr__(self, k):
                return self.attrs[k]
        def __setattr__(self, k, v):
                self.attrs[k] = v
        def __delattr__(self, k):
                del self.attrs[k]
--
Barnabas T. Rumjuggler
I had a son once, I ain't got no son today
My son is gone now, my son is gone, oy weh
 -- Joe Frank, "Woman and Bull in a Paint Factory"



Fri, 07 Nov 2003 01:24:27 GMT  
 property collections

    ...

Quote:
> My solutions seem to involve elements which need to refer to the parent
> and thus create a loop.

Not sure I understand your general problem, but, regarding
this last item, have you tried the (new in 2.1) weak refs...?

Alex



Thu, 06 Nov 2003 22:51:55 GMT  
 property collections


Quote:


>    ...
>> My solutions seem to involve elements which need to refer to the parent
>> and thus create a loop.

>Not sure I understand your general problem, but, regarding
>this last item, have you tried the (new in 2.1) weak refs...?

>Alex

well now I would certainly use weak refs if that were feasible for all,
but some people are still using 1.5.2 and so I still have to worry about
memory loops. Weak refs are exactly the python only solution for this
sort of acquisition style object.
--
Robin Becker


Fri, 07 Nov 2003 18:22:42 GMT  
 property collections


...
well I tried and get an error

C:\Python\reportlab\graphics>python \tmp\tpc.py
TPC a=1001 b=1002
Traceback (most recent call last):
  File "\tmp\tpc.py", line 34, in ?
    print 'TPC[%d] a=%d b=%d'% (i, TPC[i].a, TPC[i].b)
AttributeError: 'Properties' instance has no attribute 'b'

################# yoir code + __len__ method and test
class PropertyCollection:
        class Properties:
                def __init__(self, dict):
                        self.__dict__.update(dict)
        def __init__(self):
                self.__dict__['attrs'] = {}
                self.__dict__['i_attrs'] = {}
        def __getitem__(self, i):
                if not self.i_attrs.has_key(i):
                        self.i_attrs[i] = self.Properties(self.attrs)
                return self.i_attrs[i]
        def __getattr__(self, k):
                return self.attrs[k]
        def __setattr__(self, k, v):
                self.attrs[k] = v
        def __delattr__(self, k):
                del self.attrs[k]
        def __len__(self):
                return len(self.i_attrs)

TPC = PropertyCollection()
TPC[0].a=11
TPC.a = 31
TPC[1].b=12
TPC.b = 32
TPC[2].a=101

TPC.a=1001
TPC.b =1002

n=len(TPC)
print 'TPC a=%d b=%d'% (TPC.a, TPC.b)
for i in xrange(n):
        print 'TPC[%d] a=%d b=%d'% (i, TPC[i].a, TPC[i].b)

--
Robin Becker



Fri, 07 Nov 2003 19:29:56 GMT  
 property collections

Quote:





> ...
> well I tried and get an error

> C:\Python\reportlab\graphics>python \tmp\tpc.py TPC a=1001 b=1002
> Traceback (most recent call last):
>   File "\tmp\tpc.py", line 34, in ?
>     print 'TPC[%d] a=%d b=%d'% (i, TPC[i].a, TPC[i].b)
> AttributeError: 'Properties' instance has no attribute 'b'

I realized after I posted the code that properties added to the instance
after an index is reference won't be available to the indexed instance.
Oops.

--
Barnabas T. Rumjuggler
I had a son once, I ain't got no son today
My son is gone now, my son is gone, oy weh
 -- Joe Frank, "Woman and Bull in a Paint Factory"



Fri, 07 Nov 2003 23:52:17 GMT  
 property collections

Quote:





> >    ...
> >> My solutions seem to involve elements which need to refer to the parent
> >> and thus create a loop.

> >Not sure I understand your general problem, but, regarding
> >this last item, have you tried the (new in 2.1) weak refs...?

> >Alex
> well now I would certainly use weak refs if that were feasible for all,
> but some people are still using 1.5.2 and so I still have to worry about
> memory loops. Weak refs are exactly the python only solution for this
> sort of acquisition style object.

This seems to work without cyclic references:

class ItemProxy:

    def __init__(self, parent, index):
        self.__dict__["_parent"] = parent
        self.__dict__["_index"] = index

    def __getattr__(self, attr):
        return self._parent.get_attribute(self._index, attr)

    def __setattr__(self, attr, value):
        self._parent.set_attribute(self._index, attr, value)

class Parent:

    def __init__(self):
        self.items = {}

    def __getitem__(self, index):
        return ItemProxy(self, index)

    def set_attribute(self, index, attr, value):
        item = self.items.get(index)
        if item is None:
            item = self.items[index] = {}
        item[attr] = value

    def get_attribute(self, index, attr):
        item = self.items.get(index)
        if item is not None and item.has_key(attr):
            return item[attr]
        return getattr(self, attr)

Sample session:

$ python1.5 -i itemproxy.py

Quote:
>>> p = Parent()
>>> p.a = 1
>>> p.b = 2
>>> p[1].a = 0.5
>>> p[1].a
0.5
>>> p[1].b
2
>>> p[2].a
1
>>> p[2].b
2

The trick here is that the parent does not have any references to the
objects returned by the __getitem__ method, so there are no cyclic
references.

--
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



Sat, 08 Nov 2003 02:42:36 GMT  
 property collections


...

This looks more interesting. The problem for me is that the actual items
are of a particular PropertyHolder class, but I think it's possible to
create these on the fly.

Thanks for the code.

Quote:
>This seems to work without cyclic references:

>class ItemProxy:

>    def __init__(self, parent, index):
>        self.__dict__["_parent"] = parent
>        self.__dict__["_index"] = index

>    def __getattr__(self, attr):
>        return self._parent.get_attribute(self._index, attr)

>    def __setattr__(self, attr, value):
>        self._parent.set_attribute(self._index, attr, value)

>class Parent:

>    def __init__(self):
>        self.items = {}

>    def __getitem__(self, index):
>        return ItemProxy(self, index)

>    def set_attribute(self, index, attr, value):
>        item = self.items.get(index)
>        if item is None:
>            item = self.items[index] = {}
>        item[attr] = value

>    def get_attribute(self, index, attr):
>        item = self.items.get(index)
>        if item is not None and item.has_key(attr):
>            return item[attr]
>        return getattr(self, attr)

>Sample session:

>$ python1.5 -i itemproxy.py
>>>> p = Parent()
>>>> p.a = 1
>>>> p.b = 2
>>>> p[1].a = 0.5
>>>> p[1].a
>0.5
>>>> p[1].b
>2
>>>> p[2].a
>1
>>>> p[2].b
>2

>The trick here is that the parent does not have any references to the
>objects returned by the __getitem__ method, so there are no cyclic
>references.

--
Robin Becker


Sat, 08 Nov 2003 05:48:17 GMT  
 property collections
If you insist on this interface:

default = -1

class PC:
  def __init__(self):
    self.__dict__["index"] = default
  def __getitem__(self, item):
    self.__dict__["index"] = item
    return self
  def __getattr__(self, attr):
    index = self.index
    self.__dict__["index"] = default
    if self.__dict__.has_key((index, attr)):
      return self.__dict__[(index, attr)]
    return self.__dict__[(default, attr)]
  def __setattr__(self, attr, value):
    index = self.index
    self.__dict__["index"] = default
    self.__dict__[(index, attr)] = value

gzeljko

Quote:
----- Original Message -----

Newsgroups: comp.lang.python

Sent: Monday, May 21, 2001 11:48 PM
Subject: Re: property collections




> ...

> This looks more interesting. The problem for me is that the actual items
> are of a particular PropertyHolder class, but I think it's possible to
> create these on the fly.

> Thanks for the code.

> >This seems to work without cyclic references:

> >class ItemProxy:

> >    def __init__(self, parent, index):
> >        self.__dict__["_parent"] = parent
> >        self.__dict__["_index"] = index

> >    def __getattr__(self, attr):
> >        return self._parent.get_attribute(self._index, attr)

> >    def __setattr__(self, attr, value):
> >        self._parent.set_attribute(self._index, attr, value)

> >class Parent:

> >    def __init__(self):
> >        self.items = {}

> >    def __getitem__(self, index):
> >        return ItemProxy(self, index)

> >    def set_attribute(self, index, attr, value):
> >        item = self.items.get(index)
> >        if item is None:
> >            item = self.items[index] = {}
> >        item[attr] = value

> >    def get_attribute(self, index, attr):
> >        item = self.items.get(index)
> >        if item is not None and item.has_key(attr):
> >            return item[attr]
> >        return getattr(self, attr)

> >Sample session:

> >$ python1.5 -i itemproxy.py
> >>>> p = Parent()
> >>>> p.a = 1
> >>>> p.b = 2
> >>>> p[1].a = 0.5
> >>>> p[1].a
> >0.5
> >>>> p[1].b
> >2
> >>>> p[2].a
> >1
> >>>> p[2].b
> >2

> >The trick here is that the parent does not have any references to the
> >objects returned by the __getitem__ method, so there are no cyclic
> >references.

> --
> Robin Becker
> --
> http://mail.python.org/mailman/listinfo/python-list



Sat, 08 Nov 2003 21:25:22 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. undefined property in property node

2. Documenting properties created by property() properly

3. Collection of collections, how to collect?

4. collection inside a collection: how to selectively collect?

5. In Vo DataWindow SingleLineEdit Properties has "Input Method Editor" Properties

6. LabVIEW - Excel problem, error: "Property Node: Invallid property"

7. Dyalog AutoConf Property

8. Setting ActiveX properties at development time

9. Has anyone worked with file property sets?

10. Properties of embeded ActiveX control?

11. fix with execution properties

12. File Properties

 

 
Powered by phpBB® Forum Software