Debugging Python in VS Code

in #programming4 months ago

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')

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
<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.

Securely chat with me on Keybase

Why you should vote me as witness


This good

I downloaded VSCode yesterday for the first time to start doing some python email stuff. Loving it. Definitely going to pick up and look at working with Hive.

VS Code is really amazing and the only thing Microsoft has done right in many years.

You know I've beaten up on MS for years too, but I have to say whilst the interface on Team sucks balls, the actual quality of the sound and video during calls is amazing.

Australia is using MS Teams for all court cases and it was flawless the other night.

This VSCode dev environment, synced with github, is pretty darn amazing.

I lost all respect for Microsoft after Windows 10.

I've heard that VS Code is good and I see it is available for Linux. I have used Visual Studio a fair bit for work and that is a powerful IDE. I need to check this out.

I am still using Pycharm. It is getting more and more confusing... I probably will need to dig into VS soon! Thank you for sharing. I am also wondering if beem can help extract attributes such as #followers/followings, etc.

Upvoted by GITPLAIT!

We have a curation trial on you can earn a passive income by delegating to @gitplait
We share 80 % of the curation rewards with the delegators.

To delegate, use the links or adjust 10HIVE, 20HIVE, 50HIVE, 100HIVE, 200HIVE, 500HIVE, 1,000HIVE, 10,000HIVE, 100,000HIVE

Join the Community and chat with us on Discord let’s solve problems & build together.

Thank you for sharing this amazing post on HIVE!
  • Your content got selected by our fellow curator @tibfox & you just received a little thank you via an upvote from our non-profit curation initiative!

  • You will be featured in one of our recurring curation compilations and on our pinterest boards! Both are aiming to offer you a stage to widen your audience within and outside of the DIY scene of hive.

Join the official DIYHub community on HIVE and show us more of your amazing work and feel free to connect with us and other DIYers via our discord server!

If you want to support our goal to motivate other DIY/art/music/homesteading/... creators just delegate to us and earn 100% of your curation rewards!

Stay creative & hive on!
 4 months ago Reveal Comment