Open mtimm opened 8 years ago
getattr() will work:
from cobra.mit.access import MoDirectory
from cobra.mit.session import LoginSession
ls = LoginSession('http://10.1.1.200', 'admin', 'p@ssw0rd)
md = MoDirectory(ls)
md.login()
encap = md.lookupByClass("fvnsEncapBlk")
encap[0].from
#>>> *** SyntaxError: invalid syntax (<stdin>, line 1)
getattr(encap[0], 'from')
#>>> 'vlan-1500'
Hi Joe,
I included the getattr workaround in my original description of the problem.
Mike
Hi Mike,
Sorry, I read that too quickly the first time. Restating the problem, we cannot access object attributes through dot-notation because they conflict with Python reserved keywords which causes the parser to raise a syntax error. So we either have to change the attribute names or use something other than dot notation.
Solution #1 - obvious and likely a bad idea - change the attribute names:
@property
def From(self):
return self._from
Solution #2 - using something other than dot-notation (or getattr() function directly) - e.g ElementTree-style access
# getattr
encap[0].attrib['from']
# setattr
encap[0].update({'desc': 'outside encap'})
I could write something similar to #2 - we just need to decide what the syntax should look like.
Thanks, Joe
When we generate the model for cobra we explicitly change the attribute names that overlap with python reserved words to include an at the end, in this case the model should have from. However, it doesn't appear that we carry this forward inside cobra so when we instantiate the property we do so with the original name which is in propMeta, I haven't looked into why this is though.
Example:
We need a generic solution to access an attribute that overlaps with a python builtin/reserved keyword or to document how to access attributes like this in a more clear way.
PropMeta shows the original attribute and not the one with an _ appended to it: