Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51553

Python descriptor protocol (for more or less structured data)

Newsgroups comp.lang.python
Date 2013-07-30 06:04 -0700
Message-ID <040f33f6-6435-4aea-98ae-eabf8c16b167@googlegroups.com> (permalink)
Subject Python descriptor protocol (for more or less structured data)
From CWr <christoph.wruck@gmail.com>

Show all headers | View raw


Hi together,

Some years ago I started a small WSGI project at my university. Since then the project was grown up every year. Some classes have more than 600 lines of code with (incl. boiler-plates mostly in descriptors/properties). 

Many of these properties are similar or have depencies among themselves.
The idea is to grouping similar properties like:

new style:
----------
>>>m = MyClass(...)
>>>m.attr = 'some; complex:data#string'

>>>m.attr.value
'some'
>>>m.attr.extras
{'complex':('data','string')}

I wrote this descriptor:

class Descr:
    
    def __init__(self, value):
        self.attribute = self.__class__.__name__
        self.__set__(None, value)

    def __get__(self, obj, Type=None):
        return getattr(obj, self.attribute, self)
        
    def __set__(self, obj, value):
        if obj is None: # descripting yourself
            # do something here ...
            self.value = value
        else:
            if hasattr(obj, self.attribute):
                self.__get__(obj).__set__(None, value)
            else:
                setattr(obj, self.attribute, type(self)(value))

This works fine as long as the value attribute of Descr is read-only and the 
user have to use the descriptor interface e.g. __get__/__set__. Because
it's not guaranteed that the user gets a seperated instance of Descr which
will be living in obj.__dict__. If obj is None the descriptor will be returned
themselves.

But I would like that the user can use the following statement:

>>>m = MyClass(...)
>>>m.attr = 'some; complex:data#string'
>>>m.attr.value
'some'
>>>m.attr.value = 'some other'
>>>m.attr.value
'some other'

But this usage will be problematic. If the descriptor returned themselves (default case) and the user modified the value, he modified the default
value without to create a seperated instance attribute.

>>>class C:
>>>    def __init__(self, value):
>>>        if not value is None:
>>>            self.attr = value
>>>    attr = Descr('default value')

>>># explicit default usage (no problem): 
>>>C.attr.value
'default value'

>>>a = C()
>>>a.attr.value
'default value'

The following is the main Problem:

>>>a.attr.value = 'other'
>>>C.attr.value
'other'

The user could think that a new instance based value will be created. But it
isn't.

It will works fine only if I assign a value explicitly.

>>>m = MyClass(value='test')
>>>m.__dict__
>>>{'Descr':<object of ...>}

Has anyone had a similar problem in the past? Or I am on the wrong way.

Kind Regards,
Chris

Sorry for my terrible english ...





 




Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Python descriptor protocol (for more or less structured data) CWr <christoph.wruck@gmail.com> - 2013-07-30 06:04 -0700
  Re: Python descriptor protocol (for more or less structured data) Peter Otten <__peter__@web.de> - 2013-07-30 16:35 +0200
    Re: Python descriptor protocol (for more or less structured data) CWr <christoph.wruck@gmail.com> - 2013-07-31 02:16 -0700
      Re: Python descriptor protocol (for more or less structured data) Terry Reedy <tjreedy@udel.edu> - 2013-07-31 13:44 -0400

csiph-web