Python Code in Dynamo - testing?

Hello,

maybe i’m asking a very stupid question.
Is there an option in dynamo, to validate python code in steps, like on this page?

https://tinyurl.com/y9357aev

I have the problem, that my code ist working if i try it in “Thonny”. If the code runs in Revit/Dynamo… the same code… doesn’t work. Most in the case the problems are in the “list” which contain no “0” at the beginning.

I know already the PRW (Debugging Python Code), but as you see, it’s not the same as i am searching for. And the 2018.1 version is not available.

In one question -> how do you get your “problems” out of your code?

I don’t think there is a way to debug python besides the post you mentioned. Usually the python warnings/errors are enough to get me started when I run into a problem. Other options you can try though would be to comment out lines of your code and troubleshoot it line by line. Sometimes when I’m not sure my variables are setup properly I’ll use my python code to output all the “pieces” of my code before actually committing an action.

If you show us the code and the errors you get we might be able to help out.

I have tried and failed to create a full debugger. This has been done successfully for the python node in grasshopper, but I ran into road blocks in dynamo, the main one being the python dynamo engine does not run with the FullFrames option. This prevents any debugger from acquiring the execution stack frame (_get_frame() call). I have created an issue about it here

The Rpw Console gives the abilities to inspect your code, but it does not allow you to step through it. This has been sufficient for me in most cases: just add a break point right before where you are getting the error an inspect your variables, and repeat until you have not more errors.

That said, the specific error you described doesn’t necessarily need a debugger.
Better coding practices can easily handle and prevent this type, for example:

Let’s say the list can be one of these:

some_list = []  # or
some_list = [1,2,3]

Instead of using some_list[0] use python’s iteration features:

for item in some_list:
    first_item = item  #This is the first item in the list
    # Do something if list has an item
    break
else:
    first_item = None
    # List did not break, so else code is executed.

Another alternative is to use try except:

try:
    first_item = some_list[0]
except IndexError:  # Index[0] doesn't exist
    first_item = None  # List is empty
else:
    # do something with first_item

I hope this helps!

3 Likes

You can always comment out sections of code to test parts of the process too. It’s cumbersome and kinda ham fisted but it’s an old school mentality that certainly helps.

you can always use “try” and “expect”…it helps if your code crash Dynamo and Revit

Edit: ups! Gui_Talarico mention this before

Hey guys !!!

Thank you very much for your answeres :smile:

@Gui_Talarico
Thank you for your tutorial – damn… there is still so much to learn for me…