Updates to spltools, downloading settings and identifying the set of a card.

in #hive-1332316 days ago

Image created with Bing image generator. I dont know what those arms behind this guy is, but I thought it was funny 😄

This is first update to spltools following the initial commit detailed in this post. spltools is a python package for working with Splinterlands data.


https://github.com/Kalkulus2/spltools

Short summary of updates

  • Adapted to snake_case to comply with PEP-8. Thanks to @beaker007 for informing me that this is the naming convention.
  • Changed the get_card_data() function to return a dictionary of cards, the old version which returns a list is now in get_card_data_raw()
  • Added a spltools/settings.py to hold constants that will be used in multiple places.
  • Added a new function to download Splinterlands' settings
  • Added a function to check if a card belongs to a set

Below I will go into details about the new content.

separator.png

Function: get_card_data() update

I renamed getCardData() to get_card_data_raw(), and modified it to use BASE_URL instead of hard coding the url here.


image.png

Additionally, I created a new function get_card_data(), which transforms the list of cards obtained by get_card_data_raw() into a dictionary, where the keys are the card ids and the entries are the cards.

The reason this is useful is that Splinterlands sometimes releases cards in a different order than their ids would imply, and this breaks any simple lookup by index in the list. I always want to be able to do calls like:

card = card_data[id-1] # List
 
or 

card = card_data[id] # Dictionary

Transforming to a dictionary is more robust since I don't have to think about filling in missing entries like I would have to do to make the list work as I want. The get_card_data() function looks like this:


image.png

separator.png

Function get_splinterlands_settings()

This function is similar to get_card_data(). It downloads information from https://api2.splinterlands.com/settings, and returns it as a dictionary.


image.png

separator.png

Function in_set():

A new function for checking if a card (by id) belongs to a given set. This function is located in spltools/carddata/tools.py.


image.png

The docstring explains the input parameters, so we won't go over that. The if/else statement shown at the bottom of the image checks which type of input we have given. I wanted the function to work both by set name and by set id. It also checks that the set specification is valid. The rest of the function identifies which set the card belongs to:


image.png

First, we check is there is a "," in the editions field. This is only true for Alpha/Beta cards, so we can identify them by that.

If the card is not a core Alpha/Beta card, we check the other core sets. If it is in any of those, the set_ variable matches the 'editions' field, which we have stored as an integer in the card_edition field.

If we still have not found the card, then we must use less clean logic to check which type it is. Promo and Rewards are contained in the same editions for all sets, and sometimes separated by the tier field. However, both Alpha and Beta and beta promo cards have no tier, so we have to resort to hard coding by their id.

We also have the Soulbound rewards, which are contained in their own editions (10 and 13 for Chaos and Rebellion, respectively).

separator.png

in_set() example script:

This script is in examples/print_card_sets.py. The purpose of this script is to test the in_set function, and we do this by going through each card and checking which set it belongs to. We test all sets for each card, and fill in table cell based each result.


image.png

First the card data is obtained with the get_card_data function. Next, I print a table header. I store the column names in the c1-c7 variables, and then Python's fstring functionality to transform them into strings. Next we loop over the cards in card_data. card_data.items() returns pairs of key-value from the card_data dictionary, and we access them with the card_id and card variables.

Then we test the card with each set, and store the results in the c2-c7 variables. Finally, we use the same print code that we used in the header, to make sure than the columns always match the header in appearance.

The beginning of the output of the script looks like this:


image.png

separator.png

Final words

Thanks for your attention. I hope that the spltools package can be useful for the Splinterlands community.

spltools update posts:

Initial spltools commit


If you have not yet joined Splinterlands please click the referral link below to get started.



Join Splinterlands


Best wishes
@Kalkulus

separator.png

Sort:  

Thanks for sharing! - @alokkumar121

Loading...

This post has been supported by @Splinterboost with a 20% upvote! Delagate HP to Splinterboost to Earn Daily HIVE rewards for supporting the @Splinterlands community!

Delegate HP | Join Discord

Congratulations @kalkulus! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You made more than 200 comments.
Your next target is to reach 300 comments.

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:

Our Hive Power Delegations to the September PUM Winners
Feedback from the October Hive Power Up Day
Hive Power Up Month Challenge - September 2024 Winners List


This post has been supported by @fallen.angels guild!
Delegate Tokens and HP to Fallen Angels to earn weekly rewards!
Delegate | Join to the guild

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.