Python Forum
Help with Exception to error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Help with Exception to error (/thread-8907.html)



Help with Exception to error - harveyl12 - Mar-12-2018

Hi,

I am new to Python and trying to help a friend.

I need to use a try exception to fix the error here below.

Task exception was never retrieved
future: <Task finished coro=<notification_loop() done, defined at chesterfieldbot.py:167> exception=KeyError('iv_atk',)>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "chesterfieldbot.py", line 201, in notification_loop
hundredsarray = getNewHundreds(pokearray)
File "chesterfieldbot.py", line 139, in getNewHundreds
if (getIVPercent(newArray[i]) != 100.0):
File "chesterfieldbot.py", line 20, in getIVPercent
if (poke['iv_atk'] is None or poke['iv_def'] is None or poke['iv_sta'] is None):
KeyError: 'iv_atk'

within the below code. Any help will be appreciated as I'm not too sure how I should be implementing this. Please let me know if more details are needed as I am new here and a noob.
code -> https://python-forum.io/Thread-Help-with-Exception-to-error?pid=42111#pid42111


RE: Help with Exception to error - harveyl12 - Mar-13-2018

please delete thread


RE: Help with Exception to error - sparkz_alot - Mar-13-2018

We do not normally delete threads/posts. If you have a specific reason, you can post it here Private Inquiries, this is viewable only by you and the admins/mods.


RE: Help with Exception to error - harveyl12 - Mar-13-2018

posted in there, please read and take this thread down or remove the code...!!!


[split] Help with Exception to error - harveyl12 - Mar-13-2018

Hi,

I am new to Python and trying to help a friend.

I need to use a try exception to fix the error here below.

Task exception was never retrieved
future: <Task finished coro=<notification_loop() done, defined at chesterfieldbot.py:167> exception=KeyError('iv_atk',)>
Traceback (most recent call last):
File "/usr/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "chesterfieldbot.py", line 201, in notification_loop
hundredsarray = getNewHundreds(pokearray)
File "chesterfieldbot.py", line 139, in getNewHundreds
if (getIVPercent(newArray[i]) != 100.0):
File "chesterfieldbot.py", line 20, in getIVPercent
if (poke['iv_atk'] is None or poke['iv_def'] is None or poke['iv_sta'] is None):
KeyError: 'iv_atk'

I have listed the code sections below by the lines in which are mentioned in the above error. Any help will be appreciated as I'm not too sure how I should be implementing this. Please let me know if more details are needed as I am new here and a noob.

I just need to try and add a try exception or some exception to these to keep them running and alive rather than throwing an error.

Line 167
#This runs the loop to post notifications about rare pokemon.
async def notification_loop():
    while not bot.is_closed():
        await bot.wait_until_ready()
        global desiredPokemon, savedRares, savedHundreds
        payload = { 
            'api-key': '*API KEY HERE*',
            'area': 'chesterfield',
            'type': 'pokemon',
            'fields': 'id,pokemon_id,lat,lon,iv_atk,iv_def,iv_sta',
        }
Line 201
 if (len(pokearray) == 0 or seriousError):
            raresarray = []
            hundredsarray = []
        else:
            raresarray = getNewRares(pokearray)
            hundredsarray = getNewHundreds(pokearray)
Line 139
or i in range(0, len(newArray)):
            if (getIVPercent(newArray[i]) != 100.0):
                indexToRemove = indexToRemove + [i]
    indexToRemove = sorted(indexToRemove, reverse=True)
    for index in indexToRemove:
        newArray.pop(index)
Line 20
def getIVPercent(poke):
    if (poke['iv_atk'] is None or poke['iv_def'] is None or poke['iv_sta'] is None):
        return 0
    else:
        return ((float(poke['iv_atk'])+float(poke['iv_def'])+float(poke['iv_sta']))/45.0)*100.0

Posted above in more detail :)


RE: Help with Exception to error - micseydel - Mar-14-2018

You only need to change the last block of code you provided. Wrap the four lines within the function in a try, and then catch the KeyError.

I suspect that's not really what you want though. For one thing, you can replace code like
poke['iv_atk']
with
poke.get('iv_atk', None)
That aside, I find it weird that a Pokemon would not have those IVs. You probably should prevent that input being provided to that function, rather than making the function tolerant of bad input.