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


Groups > comp.lang.python > #5233 > unrolled thread

generate properties code in class dynamically

Started byJamesEM <james.housden@deutsche-boerse.com>
First post2011-05-12 06:11 -0700
Last post2011-05-12 22:45 -0700
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  generate properties code in class dynamically JamesEM <james.housden@deutsche-boerse.com> - 2011-05-12 06:11 -0700
    Re: generate properties code in class dynamically nn <pruebauno@latinmail.com> - 2011-05-12 07:14 -0700
    Re: generate properties code in class dynamically Terry Reedy <tjreedy@udel.edu> - 2011-05-12 16:04 -0400
      Re: generate properties code in class dynamically JamesEM <james.housden@deutsche-boerse.com> - 2011-05-12 22:45 -0700

#5233 — generate properties code in class dynamically

FromJamesEM <james.housden@deutsche-boerse.com>
Date2011-05-12 06:11 -0700
Subjectgenerate properties code in class dynamically
Message-ID<f5a65253-def8-41c8-b722-97cd76bc72d0@32g2000vbe.googlegroups.com>
Hello,
I have a python class that contains a dictionary.
I would like to use python properties to access the elements of the
dictionary.
This could be achieved as follows:

class MyClass(object):

    def __init__(self):
        self.d = {}
        d['field1'] = 1.0
        d['field2'] = 'A'
        d['field3'] = [10.0,20.0,30.0]

    @property
    def field1(self):
        return self.d['field1']

    @field1.setter
    def field1(self, value):
        self.d['field1'] = value

    @field1.deleter
    def field1(self):
        del self.d['field1']

    @property
    def field2(self):
        return self.d['field2']

    @field1.setter
    def field2(self, value):
        self.d['field2'] = value

    @field1.deleter
    def field2(self):
        del self.d['field2']

    @property
    def field3(self):
        return self.d['field3']

    @field3.setter
    def field3(self, value):
        self.d['field3'] = value

    @field3.deleter
    def field3(self):
        del self.d['field3']


However, I am effectively writing the same properties code three
times.
I would prefer to generate the properties code dynamically from the
keys of the dictionaries.
What I am looking for is something like:

class MyClass(object):

    def __init__(self):
        self.d = {}
        d['field1'] = 1.0
        d['field2'] = 'A'
        d['field3'] = [10.0,20.0,30.0]
        for f in d:
           create_property(f)


where create_property(f) dynamically creates the property code for
field f in MyClass.

Is this possible?
If so, how could I do it?

Thanks,
James

[toc] | [next] | [standalone]


#5239

Fromnn <pruebauno@latinmail.com>
Date2011-05-12 07:14 -0700
Message-ID<922112f8-956f-49da-9a95-fd2eccd4911f@l30g2000vbn.googlegroups.com>
In reply to#5233
On May 12, 9:11 am, JamesEM <james.hous...@deutsche-boerse.com> wrote:
> Hello,
> I have a python class that contains a dictionary.
> I would like to use python properties to access the elements of the
> dictionary.
> This could be achieved as follows:
>
> class MyClass(object):
>
>     def __init__(self):
>         self.d = {}
>         d['field1'] = 1.0
>         d['field2'] = 'A'
>         d['field3'] = [10.0,20.0,30.0]
>
>     @property
>     def field1(self):
>         return self.d['field1']
>
>     @field1.setter
>     def field1(self, value):
>         self.d['field1'] = value
>
>     @field1.deleter
>     def field1(self):
>         del self.d['field1']
>
>     @property
>     def field2(self):
>         return self.d['field2']
>
>     @field1.setter
>     def field2(self, value):
>         self.d['field2'] = value
>
>     @field1.deleter
>     def field2(self):
>         del self.d['field2']
>
>     @property
>     def field3(self):
>         return self.d['field3']
>
>     @field3.setter
>     def field3(self, value):
>         self.d['field3'] = value
>
>     @field3.deleter
>     def field3(self):
>         del self.d['field3']
>
> However, I am effectively writing the same properties code three
> times.
> I would prefer to generate the properties code dynamically from the
> keys of the dictionaries.
> What I am looking for is something like:
>
> class MyClass(object):
>
>     def __init__(self):
>         self.d = {}
>         d['field1'] = 1.0
>         d['field2'] = 'A'
>         d['field3'] = [10.0,20.0,30.0]
>         for f in d:
>            create_property(f)
>
> where create_property(f) dynamically creates the property code for
> field f in MyClass.
>
> Is this possible?
> If so, how could I do it?
>
> Thanks,
> James

Some searching in the cookbook should help. I found this:

http://code.activestate.com/recipes/577590-dictionary-whos-keys-act-like-attributes-as-well/

[toc] | [prev] | [next] | [standalone]


#5254

FromTerry Reedy <tjreedy@udel.edu>
Date2011-05-12 16:04 -0400
Message-ID<mailman.1489.1305230663.9059.python-list@python.org>
In reply to#5233
On 5/12/2011 9:11 AM, JamesEM wrote:

> I would prefer to generate the properties code dynamically from the
> keys of the dictionaries.
> What I am looking for is something like:
>
> class MyClass(object):
>
>      def __init__(self):
>          self.d = {}
>          d['field1'] = 1.0
>          d['field2'] = 'A'
>          d['field3'] = [10.0,20.0,30.0]
>          for f in d:
>             create_property(f)
>
> where create_property(f) dynamically creates the property code for
> field f in MyClass.
>
> Is this possible?

Without actually trying, I am not sure, but I believe maybe (possibly 
version dependent). The init method is the wrong place. Create the 
properties exactly once, just after the class is created. It is possible 
to add functions to classes as attributes (instance methods) after they 
are created. The property decorators *might* require that they be 
invoked with the class body, I do not know. I would first try with 
property().

Assuming dict name 'd' is fixed:

def gsd(key):
   def get(self):
     return self.d[key]
   def set(self, value):
     self.d[key] = value
   def del(self):
     del self.d[key]
   return get,set,del

for key in fieldnames:
   setattr(MyClass, key, property(*gsd(key)))

For recent versions, this could be done within a class decorator, but 
that is only convenient syntactic sugar.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#5280

FromJamesEM <james.housden@deutsche-boerse.com>
Date2011-05-12 22:45 -0700
Message-ID<9d949617-f4f0-4421-8de7-f283861995ac@t19g2000yql.googlegroups.com>
In reply to#5254
On May 12, 10:04 pm, Terry Reedy <tjre...@udel.edu> wrote:
> On 5/12/2011 9:11 AM, JamesEM wrote:
>
>
>
> > I would prefer to generate the properties code dynamically from the
> > keys of the dictionaries.
> > What I am looking for is something like:
>
> > class MyClass(object):
>
> >      def __init__(self):
> >          self.d = {}
> >          d['field1'] = 1.0
> >          d['field2'] = 'A'
> >          d['field3'] = [10.0,20.0,30.0]
> >          for f in d:
> >             create_property(f)
>
> > where create_property(f) dynamically creates the property code for
> > field f in MyClass.
>
> > Is this possible?
>
> Without actually trying, I am not sure, but I believe maybe (possibly
> version dependent). The init method is the wrong place. Create the
> properties exactly once, just after the class is created. It is possible
> to add functions to classes as attributes (instance methods) after they
> are created. The property decorators *might* require that they be
> invoked with the class body, I do not know. I would first try with
> property().
>
> Assuming dict name 'd' is fixed:
>
> def gsd(key):
>    def get(self):
>      return self.d[key]
>    def set(self, value):
>      self.d[key] = value
>    def del(self):
>      del self.d[key]
>    return get,set,del
>
> for key in fieldnames:
>    setattr(MyClass, key, property(*gsd(key)))
>
> For recent versions, this could be done within a class decorator, but
> that is only convenient syntactic sugar.
>
> --
> Terry Jan Reedy

Thanks for your help.
I tried the above for get and set which worked as desired. However,
the del did not seem to work for me (using python 2.6.5).
I hope I did not mistype anything, but it objects to def del with a
syntax error. I guess because del is a reserved word.
James

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web