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


Groups > comp.lang.python > #15085

Re: Dynamically creating properties?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'oct': 0.02; 'attributes': 0.05; 'xml,': 0.05; 'attribute': 0.07; 'python': 0.08; 'dict': 0.09; 'dynamically': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:\\ 20': 0.09; 'structure.': 0.09; 'am,': 0.12; 'def': 0.15; 'allowed.': 0.16; 'attribute,': 0.16; 'lambda': 0.16; 'subject:creating': 0.16; 'syntaxerror:': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'thanks,': 0.18; 'header:In- Reply-To:1': 0.22; '(or': 0.23; 'dictionary': 0.23; 'pm,': 0.24; 'variable': 0.24; 'xml': 0.25; 'invalid': 0.28; 'pass': 0.29; '27,': 0.29; 'elements': 0.29; 'unknown': 0.29; 'andy': 0.30; 'syntax,': 0.30; 'values,': 0.30; 'class': 0.30; 'subject:?': 0.31; 'generally': 0.32; 'does': 0.32; 'to:addr:python-list': 0.33; 'named': 0.33; 'header:User-Agent:1': 0.34; 'header:X -Complaints-To:1': 0.35; 'reference': 0.35; 'object': 0.35; 'file': 0.36; 'addition,': 0.36; 'properties': 0.36; 'question': 0.36; 'element': 0.37; 'run': 0.37; 'but': 0.37; 'received:org': 0.38; 'somewhat': 0.38; 'some': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'getting': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; "i'd": 0.40; 'easily': 0.61; 'your': 0.61; 'property': 0.63; 'validate': 0.67; 'time?': 0.73; "'1',": 0.84; "'one'": 0.84; '08:48': 0.84; 'methods)': 0.84; 'subject:properties': 0.84; 'received:120': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Lie Ryan <lie.1296@gmail.com>
Subject Re: Dynamically creating properties?
Date Fri, 28 Oct 2011 14:42:34 +1100
References <a47fb589-520a-49ec-9864-cac1d7ea11eb@s9g2000yqi.googlegroups.com> <b955b690-8045-4de1-b0dd-dcf0a05cc92f@v5g2000vbh.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 120.22.251.164
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1
In-Reply-To <b955b690-8045-4de1-b0dd-dcf0a05cc92f@v5g2000vbh.googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2274.1319773376.27778.python-list@python.org> (permalink)
Lines 73
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1319773376 news.xs4all.nl 6915 [2001:888:2000:d::a6]:50022
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:15085

Show key headers only | View raw


On 10/28/2011 08:48 AM, DevPlayer wrote:
> On Oct 27, 3:59 pm, Andy Dingley<ding...@codesmiths.com>  wrote:
>> I have some XML, with a variable and somewhat unknown structure. I'd
>> like to encapsulate this in a Python class and expose the text of the
>> elements within as properties.
>>
>> How can I dynamically generate properties (or methods) and add them to
>> my class?  I can easily produce a dictionary of the required element
>> names and their text values, but how do I create new properties at run
>> time?
>>
>> Thanks,
>
>      class MyX(object):
>          pass
>      myx = myx()
>
>      xml_tag = parse( file.readline() )
>
>      # should be a valid python named-reference syntax,
>      # although any object that can be a valid dict key is allowed.
>      # generally valid python named reference would be the answer to
> your question
>      attribute = validate( xml_tag )
>
>      # dynamicly named property
>      setattr( myx, attribute, property(get_func, set_func, del_func,
> attr_doc) )
>
>      # "dynamicly named method"
>      # really should be a valid python named-reference syntax
>      myfunc_name = validate(myfunc_name)
>
>      def somefunc(x):
>          return x+x
>      # or
>      somefunc = lambda x: x + x
>
>      setattr( myx, myfunc_name, somefunc )
>
>
> So beaware of:
>      # \\\\\\\\\\\\\\\\\\\\\\\\\
>      setattr(myx, '1', 'one')
>
>      myx.1
>          File "<input>", line 1
>          x.1
>            ^
>      SyntaxError: invalid syntax
>
>      # \\\\\\\\\\\\\\\\\\\\\\\\\
>      x.'1'
>        File "<input>", line 1
>          x.'1'
>              ^
>      SyntaxError: invalid syntax
>
>      # \\\\\\\\\\\\\\\\\\\\\\\\\
>      x.__dict__['1']   # returns
>      'one'
>
>      x.__dict__        # returns
>      {'1': 'one'}
>
> So you should validate your variable names if you are getting them
> from somewhere.

XML does not allow attribute names to start with a number, so I doubt 
you need to worry about that. In addition, if you also need to 
dynamically access attributes and you have zero control of the name, you 
can use getattr().

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


Thread

Dynamically creating properties? Andy Dingley <dingbat@codesmiths.com> - 2011-10-27 12:59 -0700
  Re: Dynamically creating properties? John Gordon <gordon@panix.com> - 2011-10-27 20:14 +0000
  Re: Dynamically creating properties? DevPlayer <devplayer@gmail.com> - 2011-10-27 14:48 -0700
    Re: Dynamically creating properties? DevPlayer <devplayer@gmail.com> - 2011-10-27 16:00 -0700
      Re: Dynamically creating properties? DevPlayer <devplayer@gmail.com> - 2011-10-27 16:15 -0700
        Re: Dynamically creating properties? DevPlayer <devplayer@gmail.com> - 2011-10-27 17:35 -0700
      Re: Dynamically creating properties? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-10-28 07:42 +0000
        Re: Dynamically creating properties? DevPlayer <devplayer@gmail.com> - 2011-10-30 07:11 -0700
    Re: Dynamically creating properties? Lie Ryan <lie.1296@gmail.com> - 2011-10-28 14:42 +1100

csiph-web