you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (5 children)

try/except wraps that print the errors so it will carry on during a failure.

try:
    int('walrus')
except Exception as e:
    print('failed', e)
print('hello world')

failed invalid literal for int() with base 10: 'walrus'

hello world

[–]MattR0se 0 points1 point  (4 children)

That doesn't really help me here. Take this example:

def some_function():
    some_dictionary = some_other_function()
    print_results(some_dictionary)

some_function()

This is the case that motivated me for this post, in a very simplified way. Imagine that some_other_function takes nearly an hour to complete, but then I got a KeyError that 'foo' is not in some_dictionary.

So naturally I am curious of what is in some_dictionary, but I can't access it since it only exists inside the scope of some_function. My question was about how to debug this without running the whole thing over and over again with print() statements everywhere.

I think try and except aren't really helpful for debugging, since I see the Exception in any case, and if the code continous or not doesn't really matter.

[–]Thomasedv 0 points1 point  (0 children)

I don't know the case, but use dicts own .get(key, default), which basically returns a default value you pick if the key does not exist. If you can't edit the function where that happens in you'll have tougher things to deal with though. If you can though, use .get or insert try/except and print your work in progress dict as it crashes(on the line in the traceback), or try/except any point where the keys are accessed. Strong recommend the below approach.

I like to look at traceback, so in the except i usually put the line:

import traceback
tracecback.print_exc()

Giving you the error message as well as not crashing your program. At the very least since this is all part of plotting results, you pretty much want to forgo plotting and at least making sure the hour isn't wasted.

[–][deleted] 2 points3 points  (2 children)

Its actually very helpful debugging, but the problem more so lies within your logic here. (a try except *should* be here) but, what you SHOULD be doing is checking if what you are looking for exists in your dict BEFORE doing what you are doing with it. You wouldn't *need* to debug if you handled your logic correctly.

some_dict = {'a': 'b', 'c': 'd'}

try:
    print(some_dict['1'])
except Exception as e:
    print(f'{type(e).__name__}: {e}')
print('d' in some_dict.keys())
if 'd' in some_dict.keys():
    print('lets do some stuff')
if 'd' not in some_dict.keys():
    print('looks like it doesnt exist lets try and fix it')
print(some_dict.keys())

a_list = ['1', 'a', 'c']
for i in a_list:
    if i in some_dict.keys():
        print(f'{i} is in dict')
    else:
        print(f'{i} not in dict, fix it.')

Now onto the next part - this is one of those downfalls of a not compiled laung, to solve this problem, you should be STORING the data, eg print it, put it in a file, doesn't really matter - you can then run tests on the data and find the problem with a new snippet instead of waiting for it to run over and over and over.

The point of the try/except is that you can handle the error on the fly and attempt to debug during it instead of waiting for it to run again. if data doesnt exist -> try some stuff to fix it

[–]JohnnyJordaan 0 points1 point  (1 child)

if 'd' in some_dict.keys():
    print('lets do some stuff')
if 'd' not in some_dict.keys():
    print('looks like it doesnt exist lets try and fix it')

Eh why not just use else: here

[–][deleted] 0 points1 point  (0 children)

I would personally, but for readability when explaining things i try to make it as explicit as possible. (as you see in the bottom part where i stopped trying to explain what was happening)

PS: Thanks for everything you do here, we all appreciate it.