0

hi im using data from an API that comes out in this format ( spans large amounts of lines example given just shows 2 )

['347759', '98', '50752599'] 
['426291', '84', '3092962']

each line represents a different skill in a game, and each piece of data in the line represents the players rank in that skill, their level in that skill, and their total XP in that skill for example if the first skill is mining and the second skill is smithing the formatted data would be

Mining : rank=347759 , level = 98 , xp = 50752599

Smithing : rank=426291, level = 84, xp = 3092962

im using http requests to fetch this data and it spans many lines, currently im using this code ( see below ) to seperate out the data into the seperate integers on each line

line_count = 0
for line in data_processed:
    if line != '':
        skill_data = line.split(',')
        player.mining.rank = skill_data[0]
        player.mining.level = skill_data[1]
        player.mining.xp = skill_data[2]
        line_count = line_count + 1

obviously the issue with this is that when it moves onto the next line in the file ( the second trio of ints ) instead of player.mining.rank = skill_data[0] etc i will instead need player.smithing.rank = skill_data[0] and so on for 24 different lines (as there are 24 different skills in the game).

how would i dynamically switch from appending one class object such as 'mining' (Derived from 'Skills' class and declared under a 'Player' object) to another class object such as 'smithing'

any suggestions welcome - Comment if you need more explanation or context - i will reply quickly

i know i could use this (see below) but the problem is that i have multiple players calling from multiple http requests so it would span hundreds and hundreds of lines of code all very similar with just object names being different

if line_count == 1: player.mining.rank = skill_data[0] and if line_count == 2: player.smithing.rank = skill_data[0]

class declarations

for each player the object is their character name for example Conial = Player(...)

Zephyr
  • 11,891
  • 53
  • 45
  • 80
m e m e
  • 105
  • 2
  • 11
  • 1
    Can you also post the classes you mentioned that you're converting your data to? I.e. `Player`, `Skills`, etc. – kingkupps Jul 11 '20 at 18:56
  • edited and added photo and another line of text for you – m e m e Jul 11 '20 at 19:03
  • How do you know which line corresponds to which skill when you receive data from your API? – kingkupps Jul 11 '20 at 19:33
  • under the specification for the API it shows that the data returned contains Rank Level Experience for each skill 1, then Rank Level Experience for skill 2, and so on for all skills, seperated by \n inbetween skills and seperated by ' , ' for each value - an example is given on the api spec which can be found here https://runescape.wiki/w/Application_programming_interface#Old_School_Hiscores and then clicking old school hiscores under the contents tab – m e m e Jul 11 '20 at 19:37

1 Answers1

1

You can dinamically obtain an object's attribute based on a string

That said, lets imagine you have the list of the player skills as strings:

skills = ['mining', 'crafting', 'fishing', ... ]

You can change your code a little bit:

line_count = 0
for line in data_processed:
    if line != '':
        skill_data = line.split(',')
        playerSkill = getattr(player, skills[line_count])
        playerSkill.rank = skill_data[0]
        playerSkill.level = skill_data[1]
        playerSkill.xp = skill_data[2]
        
        line_count = line_count + 1
Codigo Morsa
  • 820
  • 7
  • 14
  • so above the for loop, if player = player1 was defined, would playerSkill.rank be a pointer to the memory location of player1.mining.rank ( mining being an example of the skill based on line_count ) – m e m e Jul 11 '20 at 19:29
  • Yes. Runescape btw? – Codigo Morsa Jul 11 '20 at 19:36
  • 1
    yes its runescape :) , i will test your answer and if it works then i will let you know and accept answer – m e m e Jul 11 '20 at 19:38
  • 1
    brilliant, this has worked absolutely perfectly, thankyou very much, my goal is to make a discord webhook that will show when someone gains a level etc or completes a clue scroll – m e m e Jul 11 '20 at 19:52