Part 1: Coding on Hive With Python - Setting Up

in #programming6 months ago

This is the first post in series about coding with Hive APIs and working with Hive blockchain data. While there is interest in using other languages, I've decided to start with Python. There are some great Python modules for Hive and Python is noob-friendly.

This first post will be the most boring. It's necessary to get these setup steps out of the way before we can do some fun stuff. Let's get to it.

What we need to Set Up

To succeed with Python you need 2 pieces of software:

  • A text editor
  • The python interpreter

Any text editor will do. To have a better experience use an editor with syntax highlighting such as Visual Studio Code. VSCode is free, open-source, cross-platform, and often recommended. Syntax highlighting improves the coding experience because it makes it easier to spot simple errors and saves a lot of time.

Installing the Python Interpreter

The python interpreter is the piece of software that interprets and executes the Python code. It can be run in interactive mode or given a file as input. Input files typically use the '.py' file extension. The code examples for blogs will use Python version 3, so I recommend installing version 3.

Installing Python on Linux

The easiest way to install Python on Linux is to invoke the native package manager. For RedHat/CentOS the command is yum install python3. For Ubuntu/Raspbian the command is apt-get install python3.

Installing Python on macOS

The easiest way to install Python on macOS is using Homebrew - "The Missing Package Manager for macOS". Get started with Homebrew using the instructions found here: https://brew.sh/

Once Homebrew is good to go, invoke it from Terminal to install python like so: brew install python3

Installing Python on Windows

The easiest way to install Python on Windows is using Chocolatey - "The Package Manager for Windows". Get started with Chocolatey using the instructions found here: https://chocolatey.org/install.

Once Chocolatey is good to go, invoke it from PowerShell or Cmd prompt like so: choco install python

Alternatively, you can download a Python installer from python.org, install it, and add Python to your PATH. Chocolatey does all of this for you.

Checking your Python install

Once you've completed installing Python version 3, a quick check confirms everything is in order.

From your Terminal, run the command python3 --version. On Windows Cmd prompt / PowerShell run the command python --version. If all goes well, the Python executable is found and it spits out something like Python 3.9.5. If your OS can't find Python make sure it's on your environment PATH.

Bonus: a Pythonic Poem

Now that you have Python installed, fire up the interpreter by typing python into your terminal. It will spit out some version info and help text and then greet you with >>>, indicating it's waiting for your code to interpret.

In the next post, we'll dive into modules. Modules are like add-ons that can do all sorts of things including fetching data from the Hive blockchain. Python loads a module when given the keyword import. For now, type import this and hit enter for a nice surprise.

"Flat is better than nested." - Tim Peters, the Zen of Python


p.s. Thanks for reading. Please let me know if anything is unclear or incorrect. This is intended to be a living document to be improved and maintained over time.

Sort:  

Welcome to HIVE. I look forward to what you have to say about coding for HIVE. I think the HIVE Tutorial on hive.io builds in too many dependencies.

Since you are writing about coding in HIVE. I assume that you are already familiar with the platform. I checked your account on HiveBlocks.com

https://hiveblocks.com/@learncode

HiveBlocks.com said that you had 0 HIVE POWER. Hive Power determine the resource credits for your account and you need resource credits so that you write posts and comments.

To get over this problem, I gave your account a 15 HP delegation. That should give you enough credits to write 16 posts. The credits refresh over a 5 day period; So, this should be enough to power the account.

Hi @yintercept,

Thank you for the HP delegation. That is very kind of you.

I have used developers.hive.io as a reference many times. I found that I still spend time to fiddle and figure out how to achieve certain things. So I am looking forward to sharing some of the examples on this blog.

I will also cover HiveEngine, which has its own set of APIs.

What is your primary source on the HiveEngine API?

PS: I've read through the code on PyPi. Since I am not a Python programmer, I never felt comfortable downloading the code and running the script.

https://pypi.org/project/hiveengine/

Yep, the hiveengine PyPi module is what I've been using for fetching HiveEngine token information. There is some limited documentation available. However, when getting started, I needed to ask another developer for help.

Thanks so much for this. This could give many people the confidence to 'have-a-go' with the subsequent result that we might have no end of exciting new toys to play with on the chain!
WIll be following closely and best wishes to you :-)

Thanks for your kind words. I'm looking forward to sharing useful knowledge here.

Such a great thing you are doing this! Inever coded before and I will probably.just leave that to peeps who can do it much better than me but I will give it a try to understand all the basics.
!LUV

Thanks for reading and thanks for the LUV token

Ok. I've already got Visual Studio Code installed. It's probably not the latest version though as it was installed a couple of years ago. Not sure if that matters.

I did a search for Python on my computer and found an app (in Windows) Python 3.8 (64-bit).

What do you mean by "terminal"?

When I did the cmd prompt I got this:

image.png

No mention of python and no idea what this means . . .

If your OS can't find Python make sure it's on your environment PATH.

However, it probably isn't important because, when I click on the Python 3.8 app I got a popup so I typed in import this s per your instructions and then got The Zen of Python, by Tim Peters so I'm think I'm all set. 😊


Posted via proofofbrain.io

Cool! Glad you got it working.

It shouldn’t be necessary to prefix the command with “cmd”.

Connect

Trade


@learncode! This post has been manually curated by the $PIZZA Token team!

Learn more about $PIZZA Token at hive.pizza. Enjoy a slice of $PIZZA on us!

Congratulations @learncode! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 10 upvotes.
Your next target is to reach 50 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Day - June 1st 2021 - Hive Power Delegation
Support the HiveBuzz project. Vote for our proposal!

I'm happy I didn't miss this first part. I was waiting for it for some time ;-)

I’m happy you found it! Thanks for reading. Please let me know if I can make anything more clear.

I have plenty of stuff already installed and it seems that visual code isn't finding the correct path lol. Lot to learn :-). Thanks for doing that!

I see that you have the second post on this. Can this be done using .net like C#?

I haven't done any C#, but there may be an unofficial library for Hive out there somewhere.

Thank you for this series! As they say, it's all greek to me, but I think it's time for me to start learning.

Thanks for reading! Please don't hesitate if you have any questions

I'll be reading your posts. I've done a bit of Python programming around Hive, but there is always more to learn.

!PIZZA

Thanks Steve! Happy to share some knowledge.

Connect

Trade


@learncode! I sent you a slice of $PIZZA on behalf of @steevc.

Learn more about $PIZZA Token at hive.pizza

@learncode, you were given LUV from @solymi. Info: https://peakd.com/@luvshares or check wallet: https://hive-engine.com

Ok! Got it set up and running! I just couldn't open from the power shell with just python (it opened the winstore and asked me if I wanted to install) but when i click on the program it runs.

Loved the poem, specially "Errors should never pass silently, Unless explicitly silenced"

Gonna run for part 2! !PIZZA

Nice!!! Glad you got it up and running.

Connect

Trade


@learncode! I sent you a slice of $PIZZA on behalf of @fireguardian.

Learn more about $PIZZA Token at hive.pizza (2/20)