Python-list Digest, Vol 27, Issue 123
Michael Williams
mwilliams at mgreg.com
Thu Dec 8 19:14:35 EST 2005
Thanks, Heiko, I'll give this a try. In the meantime, I'll try to
explain what exactly I mean.
Basically, I want the ability to reference a variable just as I am
able to set a variable (or attribute) on the fly. For instance, say
the user has the following list in a text file:
[butter, cream, eggs, toast, jam]
I want to be able to loop through that and say the following:
item.__setattr__(list[0].value,myclass())
At that point I have item.butter, but I don't want to have to know
(or hardcode) this, I want to then be able to do the following:
item.__getattr__(list[0].value).__setattr__(list[1].value)
So that I now have:
item.butter.cream
. . .and so on and so forth. I want the entire thing to be
dynamically created no matter what the user has entered. And I for
my part will simply limit the users input with a list of my own like so:
if list[i].value not in my_list:
raise MyError()
I don't doubt that that's probably more confusing, but that's what I
want to be able to do.
Regards,
Michael
On Dec 7, 2005, at 1:45 AM, python-list-request at python.org wrote:
> It's not exactly clear what you're trying to tell us here.
> Basically, what I
> guess you want is:
>
> getattr(object,"varname")
>
> This retrieves the value that is bound to varname at "namespace"
> object. You
> cannot retrieve a variable per se in Python, as a variable is just
> a name
> that is a binding to an object.
>
> The second part of what you're trying to do sounds more like stacking
> objects. I'll just give it a shot and implement a little bit to
> test on.
> You'll have to extend it to suit your needs... ;-)
>
> class xmlnode(object):
>
> def __init__(self):
> self.__subnodes = {}
> self.__value = ""
>
> def __getattr__(self,item):
> if item == "value":
> return self.__value
> elif item.startswith("_"):
> raise AttributeError, item
> elif item not in self.__subnodes:
> self.__subnodes[item] = xmlnode()
> return self.__subnodes[item]
>
> def __setattr__(self,item,value):
> if item.startswith("_"):
> super(xmlnode,self).__setattr__(item,value)
> elif item == "value":
> assert isinstance(value,(str,unicode))
> self.__value = value
> else:
> assert isinstance(value,(str,unicode))
> if item not in self.__subnodes:
> self.__subnodes[item] = xmlnode()
> self.__subnodes[item].value = value
>
> def __delattr__(self,item):
> if item.startswith("_"):
> super(xmlnode,self).__delattr__(item)
> elif item == "value":
> self.__value = None
> else:
> try:
> del self.__subnodes[item]
> except KeyError:
> raise AttributeError, item
>
> def toXML(self,name):
> rv = ["<%s>" % name]
> for sname, sitem in self.__subnodes.iteritems():
> rv.append(sitem.toXML(sname))
> if self.__value is not None:
> rv.append(self.__value)
> rv.append("</%s>" % name)
> return "".join(rv)
>
> This implements a simple XML-tree builder with a very specific
> output format
> (tags contain only one string value, which always comes after any
> subtag
> that they might contain, and subtag order is random). The "tricky"
> part is
> in the __*attr__ logic to get subnodes.
>
> Example of usage:
>
>>>> a = test.xmlnode()
>>>> a
> <test.xmlnode object at 0x2aaaaab57890>
>>>> a.value = "test"
>>>> a.toXML("root")
> '<root>test</root>'
>>>> a.book = "welcome"
>>>> a.anotherbook = "bye"
>>>> a.toXML("root")
> '<root><anotherbook>bye</anotherbook><book>welcome</book>test</root>'
>>>> a.book.value
> 'welcome'
>>>> a.book.anotherbook.somethingelse.thisislong = "test"
>>>> a.toXML("root")
> '<root><anotherbook>bye</
> anotherbook><book><anotherbook><somethingelse><thisislong>test</
> thisislong></somethingelse></anotherbook>welcome</book>test</root>'
>>>>
>
> HTH!
>
> --- Heiko.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051208/03240382/attachment.html>
More information about the Python-list
mailing list