pythonpython-3.xcisco-axl

Working on AXL scripts for CUCM - why does this code handle only the last item?


I am trying this code and its working fine:

phone_list = resp['return'].phone
for phone in phone_list:
    x = (phone.name)
    print(x)

try:
    resp = service.removePhone(name = x)    
except Fault:
    show_history()

print(x) gives 3 values: SEP003C53B8F073, SEP001FBB3669EA, SEP003A33B8F041

The problem is when I pass this to the try block (removePhone) below to delete these 3 phones: it only deletes the first one.

I don't have any idea on how to assign variables to separate output items. Please let me know ho can I achieve this.

This is a protocol of the execution:

>>> phone_list = resp['return'].phone
>>> for phone in phone_list:
...     x = (phone.name)
...     print(x)
...
SEP001B53B8F075
SEP001FCA3669EA
>>>
>>>
>>> try:
...     resp = service.removePhone(name = x)
... except Fault:
...     show_history()
... else:
...     print('Deleted Phone:')
...     print( resp )
...
Deleted Phone:
{
    'return': '{C979366C-491D-4F12-B058-ECB789AA326A}',
    'sequence': None
}

This code just deleted the second phone (SEP001FCA3669EA) from the server. The first phone is still there.


Solution

  • The way your code is written, the for loop does not include the try block with the call to removePhone. As you can see, the try block is only entered after the loop is done.

    After the execution of the loop, the value of x will be the last value from the loop. Consequently only the last phone is removed.

    I think that you want to execute the try block for each iteration of the loop. For that, you have to indent it so that it becomes part of the loop:

    for phone in phone_list:
        x = (phone.name)
        print(x)
    
        try:
            resp = service.removePhone(name = x)    
        except Fault:
            show_history()