First commit to spltools, a python package for working with Splinterlands data.
I have spent quite a bit of time working with Splintelands data over the last years. This has included mining battle data, analyzing card stats, guild data etc. Something I have long been thinking about is to compile these various scripts, functions and classes into a python package, in case someone else could have use for it.
I finally pulled the trigger and created a Github repository for it. I named it spltools, because I am not particularly creative with these things. Anyway, the link to the repo is:
https://github.com/Kalkulus2/spltools
For now, it only contains two things.
- A function to download Splinterlands card information
- A guild class
I plan to slowly add in new functionality to this package over time. Updates will likely be accompanied by a Hive post.
Downloading card details: getCardData()
The main content of the package currently is the getCardData function, which downloads information about all Splinterlands cards from https://api2.splinterlands.com/cards/get_details. (Some browsers, like Firefox, allow you to view the data in that url conveniently in the browser).
The function is very simple. There is no input argument, and all it does is to ask splinterlands for the card information data, and then use the json library to build a python dictionary from the returned data. If the data request is successful, we get the card information in the carddata variable.
I then add an id filter with a list comprehension, to remove all the Soulkeep cards from the data. The Soulkeep cards have ids larger than 10000, while Splinterlands cards currently go up to 706.
Here we have the name, id, color etc. The stats field is another dictionary with keys "attack", "ranged", etc, which are lists containing the card stats at each level.
We also get information about the card distribution. Goblin Shaman has 185254 total printed.
Example usage
I have a goal with this package to provide examples for most of the functions and classes it contains. To start with, I have made this tiny program to call the getCardData function. This script, printAllNamesAndIds.py, can be found in the examples folder:
This program imports the getCardData function, calls it, and then loops over the cards and prints their names and ids. I am a big fan of the fstring functionality in Python, which allows me to easily insert the names and ids into the output string . The output looks like this:
Final words
If you have not yet joined Splinterlands please click the referral link below to get started.
Join Splinterlands
Best wishes
@Kalkulus
Hi @kalkulus,
Nice tool also a big fan of python 😍.
question why do you use the
with urllib.request.urlopen("https://api2.splinterlands.com/cards/get_details") as request:
I like to use this strucure:
Not sure why one is better than the other.
Some feedback:
For Python, the PEP 8 style guide recommends using snake_case for function and variable names.
I would recommend using a base_url to abstract the url.
Might consider a Retry mechanism when users are overloading the spl API. Not sure if it needs in this code or the caller?
Keep up the great work 💪
Here is my spl api i use for my statistics tool.
https://github.com/gamerbeaker007/splinterlands-statistics/blob/main/src/api/spl.py
Awesome, thanks for the feedback.
I was not aware that snake_case was a PEP. I have just at some point gotten used to CamelCase or mixedCase so those are my defaults. I will adapt to snake_case for Python then.
The HTTPAdapter setup is appropriate when you call the API repeatedly right? I've not tried using that class but it looks simple enough. When I get to calls like that I might just steal your code ;)
Especially when you get to code to retrieve all balances for example.
This needs more than one call to the spl api at this point you need to address a back-off/retry strategy. Also if for some reason the spl api gives a other return then successful like 502.
No problem with using my code as inspiration or copy/paste 😁, I'm also not official developer just learning as wel.
I also like camelcase more for my java programs 🤣.
This is what chatgpt think PEP8 should look like for class functions and variables.
But using classes and going object oriented (OOP) with python is another great topic on what todo with it😁.
Congratulations @kalkulus! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)
Your next target is to reach 22000 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 our last posts:
Thanks for sharing! - @rehan12
👍
!BEER
!BBH
!WINE
View or trade
BEER
.Hey @kalkulus, here is a little bit of
BEER
from @eii for you. Enjoy it!Learn how to earn FREE BEER each day by staking your
BEER
.@kalkulus! @eii likes your content! so I just sent 1 BBH to your account on behalf of @eii. (2/5)
(html comment removed: )