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: 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 Subject: Re: Dynamically creating properties? Date: Fri, 28 Oct 2011 14:42:34 +1100 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 10/28/2011 08:48 AM, DevPlayer wrote: > On Oct 27, 3:59 pm, Andy Dingley 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 "", line 1 > x.1 > ^ > SyntaxError: invalid syntax > > # \\\\\\\\\\\\\\\\\\\\\\\\\ > x.'1' > File "", 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().