08-23-2019 10:07 AM
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.
Solved! Go to Solution.
08-27-2019 07:54 AM
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"})
08-26-2019 01:14 PM
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.
08-27-2019 06:57 AM
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'
08-27-2019 07:54 AM
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"})
11-13-2019 09:58 AM
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:' )
11-19-2019 12:57 AM
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.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide