Python for STEEM #2: JSON

in busy •  23 days ago  (edited)

This is meant as the second part to a series of tutorials.
The goal is to produce a hands-on guide for people who want to jump straight into developing tools for STEEM.

The native way for the STEEM blockchain to handle data is JSON.
To get a feeling for what that looks like, I will look at a single block.

Block object

from beem.blockchain import Blockchain

blockchain = Blockchain()
print(blockchain.get_current_block())

This will return the block as a 'Block' object, like it is defined in beem.
https://github.com/holgern/beem/blob/master/beem/block.py

Printing the object to screen, should result in something like this:

<Block 3111111>

If you are like me, you will confuse the Block object with a block's content frequently. And no, you can not just stringify str() the whole object to get to its details.

While it might be confusing initially, the methods which the Block object supports are very helpful. If you are a bit advanced, you can look up the supported methods in beem's block.py
If you don't understand how that works: I will get to that in a later chapter.

Transactions

For now, we can just use Block.transactions, which will return a list of all transactions in a block. The following script gets the current block and then iterates through all transactions in the block and prints them:

from beem.blockchain import Blockchain

blockchain = Blockchain()
current_block = blockchain.get_current_block()
for i in current_block.transactions:
   print(i,'\n')

Coming from other languages, it is still Python magic to me, how I don't have to instanciate the Block class first, but 'import' all its features automatically.
While I don't fully understand it, this works.

You might have noticed by now: If you were looking for a good Python guide - this isn't it.

Anyways, the above script should result in something like this.

image.png

In an earlier, similar tutorial, I streamed these transactions as they happened, for an even better MATRIX movie effect. Since then, the volume of transactions has increased substantially and I am not even going to try this again.

As you can see, this is for the most part JSON or at least similar.

JSON Navigation

For lack of a better word, this is how you navigate through the different JSON indizes.

from beem.blockchain import Blockchain

blockchain = Blockchain()
current_block = blockchain.get_current_block()
for i in current_block.transactions:
   print(i['transaction_num'],'\n')

The result is not very informative.
I picked the index 'transaction_num' only because every transaction has a number within a block.

Congratulations

This was short, but should give you enough to experiment with for now.
Looking at the raw data of a block and comparing it with steemd.com should give you further insights.

At this point, I am not sure, who I am writing this for.

Please let me know, if this is useful at all and what else you would like to learn.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Coming from other languages, it is still Python magic to me, how I don't have to instanciate the Block class first, but 'import' all its features automatically.
While I don't fully understand it, this works.

get_current_blockfunction instantiate it for you. :)

You might have noticed by now: If you were looking for a good Python guide - this isn't it.

😁

Like a blind man talking about color, aye ? :)

Maybe some basic python tutorials to better navigate through the steem blockchain? :)

Did you try the above ?

Thanks for following me. Cheers