Going to show you some examples of debugging Python with VS Code using the simple code here.
from beem import Hive
from beem.account import Account
hive = Hive()
me = Account('themarkymark')
print(me)
This code simply connects to the blockchain via Beem using the Hive object and creates an account object for the account 'themarkymark'. Nothing complicated.
If you run it, you get the following output.
(base) C:\Users\mal\Desktop>python debugging.py
<Account themarkymark>
Nothing unexpected here. Let's say we want to use the debugger to look at some details of the hive connection.
First click the run in the left hand side.
You will get a dialog asking if you want a custom launch file, for now let's just run & debug without one.
Select the first option to debug the current python file.
You will notice the bottom bar goes red, and then nothing special happens. That's because we haven't setup a breakpoint, let's do that now. A breakpoint tells the debugger you want to pause execution here and inspect the environment. Let's set a breakpoint right before the print statement.
Click right to the left of the 7 and a red dot will appear, let's try run & debug again.
At this point you can see the bar is red again, but the code execution is now paused.
On the left side you have three sections now, Variables, Watch, and Call Stack.
The variables section is what you will use most, it will allow you to inspect all current variables. Watch section will allow you to predefine variables or expressions you want to monitor during execution. Finally the call stack will display the current call stack.
Let's inspect the Hive local object.
Here you can get some information such as the current node, current hard fork, as well as a lot more information if you dig deep enough. There is a lot of information in the hive connection object.
You can use this menu bar in the top middle to stop your code, step forward one line, or continue.
There are two ways to step forward in a debugger, step into and step over. Step into will go into any function that is called and step each line of that function, step over will just move you to the output of functions.
Let's add a couple of variables, and inspect them as they are created.
from beem import Hive
from beem.account import Account
hive = Hive()
me = Account('themarkymark')
vesting_shares = me['vesting_shares']
received_vesting_shares = me['received_vesting_shares']
delegated_vesting_shares = me['delegated_vesting_shares']
Set up a break point at line 8 and run & debug.
Your code should stop right before we start assigning variables.
Let's hit the step into button to move one line forward.
You will notice our variables updated and we now have a vesting_shares variable with the amount of vesting shares on my account.
Hit step into two more times.
The 2nd time you will notice we got a receiving_vesting_shares variable and on the third it ended. That's because the debugger got to the end of the code and exited.
This should give you enough information to do some basic debugging, in a future post I'll cover how to setup a launch file and watches.
