cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1694
Views
2
Helpful
2
Replies

Python List traversal examples ?

mdeprete
Cisco Employee
Cisco Employee

Hi team,

I'm trying to write a script in Python (2.7.10) to print the list of all devices configured in NSO (4.4.2),  but couldn't find an example to get me started. I'm running into a (probably simple) Python issue ...

The root.devices.device is of type ncs.maagic.List. So, I tried to iterate through the list to get the name of each device :

import ncs

with ncs.maapi.Maapi() as m:

    with ncs.maapi.Session (m, 'admin', 'admin'):

        root = ncs.maagic.get_root(m)

        for dev in root.devices.device:

            print type(dev)    # print dev.name gives the same error ...

I get the following error message :

Traceback (most recent call last):

  File "ncs_test.py", line 6, in <module>

    for dev in root.devices.device:

  File "/Users/mdeprete/nso/4.4.2/src/ncs/pyapi/ncs/maagic.py", line 1047, in __iter__

    return ListIterator(self)

  File "/Users/mdeprete/nso/4.4.2/src/ncs/pyapi/ncs/maagic.py", line 1229, in __init__

    self._cursor = l._backend._cursor(l._path, l._key_enum_cs_nodes())

TypeError: _cursor() takes exactly 2 arguments (3 given)

Exception AttributeError: "'ListIterator' object has no attribute '_cursor'" in <bound method ListIterator.__del__ of <ncs.maagic.ListIterator object at 0x10c4e0c50>> ignored

I tried other List objects, and get the same error (like the resource pools for instance). If I use device['ios0'] for instance, then I can get access to all object data (config, address,...). So at least connectivity to my NSO is fine ...

I've looked at the manual, but there's no clear example on how to get all names of such a list. Is there any example out there I could use ?

Cheers,

Marc

1 Accepted Solution

Accepted Solutions

Jan Lindblad
Cisco Employee
Cisco Employee

The only thing missing here to make this work is a transaction object:

with ncs.maapi.Maapi() as m:

  with ncs.maapi.Session (m, 'admin', 'admin'):

    with m.start_write_trans(ncs.RUNNING) as t:

      root = ncs.maagic.get_root(t)

      for dev in root.devices.device:

        print dev.name

        print type(dev)    # print dev.name gives the same error ...

And the system was trying to tell you "ncs.maagic.BackendError: Not a Transaction backend", but due to a bug you got that other (highly cryptic) message instead.

Thanks for finding and reporting this!

View solution in original post

2 Replies 2

Jan Lindblad
Cisco Employee
Cisco Employee

The only thing missing here to make this work is a transaction object:

with ncs.maapi.Maapi() as m:

  with ncs.maapi.Session (m, 'admin', 'admin'):

    with m.start_write_trans(ncs.RUNNING) as t:

      root = ncs.maagic.get_root(t)

      for dev in root.devices.device:

        print dev.name

        print type(dev)    # print dev.name gives the same error ...

And the system was trying to tell you "ncs.maagic.BackendError: Not a Transaction backend", but due to a bug you got that other (highly cryptic) message instead.

Thanks for finding and reporting this!

And that was it, just one line, and it works perfectly now

Thanks Jan !

Cheers,

Marc