cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1203
Views
0
Helpful
5
Replies

Looping through list of device and associated speed dial

I fetched a list of devices and pass this into a subsequent getPhone request, with some logic to grab the phone name and speed dial info. My loop may be defective as i'm only getting the last item.
here's a snippet of the code:

 

'for device in dictOfNameDesc:
service = client.create_service(binding, location)
resp = service.getPhone(name=device)


phoneNameAnd_SD = [{'deviceName' : resp['return'].phone.name}, {'speeddials' : resp['return'].phone.speeddials.speeddial[0].dirn}]

 

justNameAndSpeed = []

for dict_item in phoneNameAnd_SD:
for key in dict_item:
justNameAndSpeed.append(dict_item[key])


print ('Name and SD is', justNameAndSpeed)


any assistance is appreciated.

 

 

1 Accepted Solution

Accepted Solutions

In which case you are trying to access an object variable which itself has a value of None. ie phone.speeddials = None.

 

Perhaps add a conditional to manage against this scenario:

if resp['return'].phone.speeddials is not None:
    phoneNameAnd_SD.append({'deviceName' : resp['return'].phone.name, 
                            'speeddials' : resp['return'].phone.speeddials.speeddial[0].dirn})
else:
    phoneNameAnd_SD.append({'deviceName' : resp['return'].phone.name, 
                            'speeddials' : "UNKNOWN"})

View solution in original post

5 Replies 5

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

It looks like phoneNameAnd_SD should be a list of dictionaries, however your first for loop keeps instantiating the variable with a new list (and single dictionary) on each itteration.

The first loop needs to look like:

phoneNameAnd_SD = []
for device in dictOfNameDesc: service = client.create_service(binding, location) resp = service.getPhone(name=device) phoneNameAnd_SD.append({'deviceName' : resp['return'].phone.name, 'speeddials' : resp['return'].phone.speeddials.speeddial[0].dirn})

cheers,

Seb.

Hi Seb, 
thanks for your response. I tried that but now get 'speeddials' : resp['return'].phone.speeddials.speeddial[0].dirn})
AttributeError: 'NoneType' object has no attribute 'speeddial'

In which case you are trying to access an object variable which itself has a value of None. ie phone.speeddials = None.

 

Perhaps add a conditional to manage against this scenario:

if resp['return'].phone.speeddials is not None:
    phoneNameAnd_SD.append({'deviceName' : resp['return'].phone.name, 
                            'speeddials' : resp['return'].phone.speeddials.speeddial[0].dirn})
else:
    phoneNameAnd_SD.append({'deviceName' : resp['return'].phone.name, 
                            'speeddials' : "UNKNOWN"})

hi -- i've made progress but one additional question:

in looping through a list of devices i may run into a scenario where a device may have been deleted. how do i account for this case. basically, i want to print or store the error but still proceed through the list.

 

thanks

say the current logic is 
try:
resp = service.getPhone(name=device)

except Fault as err:
print('Zeep error: xxxx: {err}'.format( err = err ) )
else:
print( 'getPhone response:' )

 

hmmm, the problem you will have is that a try block will exit once an error is encountered, so a for loop in a try block may never complete. Instead place the try block in a second method and have the for loop call that method and handle the returned values. Something like this:

def method1():
    for device in devices:
        resp = method2(device)
        
        if resp is not None:
           ...continue...

def method2(device):
    try:
       ...blah...
       return somethingGood
    except:
       ...foo...
       return None

cheers,

Seb.