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

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



0
0
0.000
11 comments
avatar

Hey Kalkulus,

Already an update again 👍.
Not sure where and when you want to use it because this information is also in the get_card_data_raw()

You can also consider using enums to increase readability in stead of the numbers example:

class ExtendedEnum(Enum):

    @classmethod
    def list_names(cls):
        return list(map(lambda c: c.name, cls))

    @classmethod
    def list_values(cls):
        return list(map(lambda c: c.value, cls))

class Edition(ExtendedEnum):
    alpha = 0
    beta = 1
    promo = 2
    reward = 3
    untamed = 4
    dice = 5
    gladius = 6
    chaos = 7
    rift = 8
    soulbound = 10
    rebellion = 12
    soulboundrb = 13

usages:

Edition(2).name
Edition.soulbound.value
Edition.list_names()
Edition.list_values()
0
0
0.000
avatar

Thanks! I will consider adding an Enum for the editions. I've not used Enums much in the past, since dictionaries do pretty much the same, but I see the argument for using it for readability.

I split the get_card_data and get_card_data_raw functions because I've found that I would often loop over cards in the list and get some data from each card to do other stuff. The problem was that sometimes an id would be missing in the list, and then my scripts would continue to run fine, but introduce bugs in lookups by id later on, since I typically did not track that all the way through my functions. Using a dict instead will give an error if I try to index an id that does not exist, which will remind me to be careful about data processing further down the line.

0
0
0.000
avatar

I understand that, hope that is clear for the user...

My comment was more about the in_set function.

This is also the reason i often directly put it into a pandas data frame.

Will help with query and filtering data without constant for loops, not always that easy and assume this in not something you want in your library but the caller who is using it.

0
0
0.000
avatar

I'm allergic to pandas, it just doesn't sit right with me. I'll use numpy instead whenever I need more advanced filtering, but I'll try to limit my use of it in this package to keep dependencies as small as possible.

0
0
0.000
avatar

Anyway, thanks again for the feedback, its much appreciated. Next time I'll try to add @commentrewarder so that I can give back something for the feedback (my vote here wont pass the dust treshold alone)

0
0
0.000
avatar
(Edited)

No problem, just love to see others using python and sharing their knowledge and journey.

0
0
0.000
avatar

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
0
0
0.000
avatar

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. 
 

0
0
0.000