Re: Dynamically creating properties?

2011-10-30 Thread DevPlayer
To be honest, I was hoping someone would have posted a link to a well known and tested recipe. You'd think this function would be in the standard library or a specific Exception tied directly with setattr() and getattr() (and possibly __getattr__(), __getattribute__(), __setattr__()) The main thin

Re: Dynamically creating properties?

2011-10-28 Thread Steven D'Aprano
On Thu, 27 Oct 2011 16:00:57 -0700, DevPlayer wrote: > def isvalid_named_reference( astring ): > # "varible name" is really a named_reference > # import string # would be cleaner I don't understand the comment about "variable name". > valid_first_char = > '_abcdefghijklmnopqrstuvw

Re: Dynamically creating properties?

2011-10-27 Thread Lie Ryan
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 (

Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
Second error def isvalid_named_reference( astring ): # "varible name" is really a named_reference import __builtin__# add line -- http://mail.python.org/mailman/listinfo/python-list

Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
At least one error: change: > for astr in dir(__builtins__): to: for astr in __builtins__.__dict__: -- http://mail.python.org/mailman/listinfo/python-list

Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
Personally I like to use this function instead of a "try: except:" because try-except will allow names like __metaclass__. Remember, setattr(obj, attr_name, value) allows attr_name to be any valid str(). For example: '!@kdafk11', or '1_1', '1e-20', '0.0', '*one', '\n%%', etc. def isvalid_named_re

Re: Dynamically creating properties?

2011-10-27 Thread DevPlayer
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 cl

Re: Dynamically creating properties?

2011-10-27 Thread John Gordon
In Andy Dingley writes: > 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? You can set dynamic attributes on class objec

Dynamically creating properties?

2011-10-27 Thread Andy Dingley
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 requi