descriptors for container items
... or something like that. I have an XMLish data structure whose nodes' __get/set/del item__ methods resolve as: node['foo'] -> node.children['foo'] node['@bar'] -> node.attributes['bar'] so you can say: countries['us']['Colorado']['Denver']['@population'] This is going to be used in user-input formulae, so I'm willing to do a lot of work for minor beautifications. I'd like to be able to say (I know, the quotes are still ugly, but at least you save a bracket): countries/'us'/'Colorado'/'Denver'/'@population' That's easy to do with a __div__ method, but it only works for getting, not setting or deleting. I'd appreciate any thoughts on this problem. I keep thinking descriptors might be involved somehow in the solution, but I may be on a completely wrong track. -- http://mail.python.org/mailman/listinfo/python-list
descriptors for container items
> I personally would first try to dump the quotes and use standard > attributes -- countries.us.Colorado... -- and the __get/set/delattr__ > methods. If I do that, the attributes (that was a stupid name for me to choose) and children would have to not share any names with each other, with the object's regular attrs, or python keywords. I toyed with the idea of having __div__ create a descriptor, assign it to a temporary attr, and access that. But then I realized that the children should never be directly assigned to anyways. I can live with one set of brackets at the end of the expression. __methods__ are delightfully evil :-) -- http://mail.python.org/mailman/listinfo/python-list
descriptors for container items
> > If I do that, the attributes (that was a stupid name for me to > choose) > > and children would have to not share any names with each other, > > Since multiple objects can indeed have duplicate attribute names, and > such > duplication is rampant in Python, I am not sure what you mean. felons['@class'] = 'capital' felons['class'].do_something() felons.class -> SyntaxError I thought I had my previous example down to: countries/'us'/'Colorado'/'Denver'['@population'] , but of course that tries to subscript a string with a string; which might be an interesting idiom for substring searching, as long as it never returned -1. -- http://mail.python.org/mailman/listinfo/python-list