I used to use reduce and getattr functions for calling attributes in a chain way like "thisattr.thatattr.blaattar" IE:
reduce(getattr, 'xattr.yattr.zattr'.split('.'), myobject)
Works perfectly fine, however now I have a new requirement, my strings can call for a specific number of an attribute as such: "thisattr.thatattr[2].blaattar"
reduce(getattr, 'xattr.yattr[2].zattr'.split('.'), myobject)
Now it doesn't work, I get xattr object has no attribute 'yattr[2]'
error.
What would be an elegent solution to this, which works for either way ?
Regards
And later you could wish to call some method rather than getting attribute. Re-implementing parts of python approach quickly will become a nightmare. Even current requirement of getattr/getitem support cannot be solved as one-liner.
Instead, you could just use python itself to interpret python,
# Create some object for testing
>>> class A(object):
... b = None
...
>>> a = A()
>>> a.b = A()
>>> a.b.b = A()
>>> a.b.b.b = [A(), A(), A(), A()]
>>> a.b.b.b[1].b
>>> a.b.b.b[1].b = "Some result"
>>>
>>> ctx = {'obj':a, 'val':None}
>>> exec("val = obj.{0}".format('b.b.b[1].b')) in ctx
>>> ctx['val']
'Some result'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With