cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements
243
Views
10
Helpful
1
Replies
Fantolino
Beginner

How to access the "from" clause in python

I need to access to the configuration of policy statements of juniper devices. The XPATH is:

/devices/device[name='r-xx22']/config/junos:configuration/policy-options/policy-statement[name='GANDALF-A2-IMPORT']/term[name='1']/from/protocol [ bgp ]

I tried to access to that configuration in python using the standard MAAGIC format:

print root.devices.device['r-xx22'].config.junos__configuration.policy_options.policy_statement['GANDALF-EXPORT'].term["1"].from['bgp']
^
SyntaxError: invalid syntax
>

I guess the problem is that form is a python keyword: how can I overcome this problema?

1 ACCEPTED SOLUTION

Accepted Solutions
ramkraja
Cisco Employee

Similar to how you referred to the top-level 'configuration' tag in the Junos namespace as "junos__configuration", you can do "junos__from" instead of just "from". You can even similarly 'qualify' every tag in the path, if you really want to. ie, your path above can also be written as

root.ncs__devices.ncs__device['r-xx22'].ncs__config.junos__configuration.junos__policy_options.junos__policy_statement['GANDALF-EXPORT'].junos__term["1"].junos__from.junos__protocol

Note that the last tag (protocol) is missing in your Maagic example.

Finally, 'protocol' seems to be a leaf-list, and you cannot index the elements of a leaf-list maagic - so, doing "protocol['bgp']" would result in an error. You can access the elements of a leaf-list by iterating over that element. So, something like

protocol = <the path above>..
for elem in protocol:
    print(elem)

/Ram

View solution in original post

1 REPLY 1
ramkraja
Cisco Employee

Similar to how you referred to the top-level 'configuration' tag in the Junos namespace as "junos__configuration", you can do "junos__from" instead of just "from". You can even similarly 'qualify' every tag in the path, if you really want to. ie, your path above can also be written as

root.ncs__devices.ncs__device['r-xx22'].ncs__config.junos__configuration.junos__policy_options.junos__policy_statement['GANDALF-EXPORT'].junos__term["1"].junos__from.junos__protocol

Note that the last tag (protocol) is missing in your Maagic example.

Finally, 'protocol' seems to be a leaf-list, and you cannot index the elements of a leaf-list maagic - so, doing "protocol['bgp']" would result in an error. You can access the elements of a leaf-list by iterating over that element. So, something like

protocol = <the path above>..
for elem in protocol:
    print(elem)

/Ram

View solution in original post