Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Cannot access game attributes in a loop #19

Open
cmnemoi opened this issue Jun 20, 2021 · 0 comments
Open

Cannot access game attributes in a loop #19

cmnemoi opened this issue Jun 20, 2021 · 0 comments

Comments

@cmnemoi
Copy link
Contributor

cmnemoi commented Jun 20, 2021

So, given this code (for reproducibility) :

import srcomapi, srcomapi.datatypes as dt
import pandas as pd

api = srcomapi.SpeedrunCom(); api.debug = 1
test_df = pd.DataFrame(['Castlevania: Aria of Sorrow','Pokémon: Yellow'],columns=['Game'])
nb_games = len(test_df)

lb_per_game = []

#getting the game for each name
for i in range(nb_games):
    try: 
      test_df.loc[i,'SRC_game'] = api.search(srcomapi.datatypes.Game, {"name": test_df.loc[i]['Game']})[0]
    except IndexError:
      pass

#collecting leaderboards for each game
for i in range(nb_games):
    lb = {}
    for category in test_df.loc[i,'SRC_game'].categories:
      if not category.name in lb:
        lb[category.name] = {}
      if category.type == 'per-level':
        pass
      # if category.miscellaneous:
      #   pass
      else:
        lb[category.name] = dt.Leaderboard(api, data=api.get("leaderboards/{}/category/{}?embed=variables".format(test_df.loc[i,'SRC_game'].id, category.id)))
    lb_per_game.append(lb)

This piece of code works :

platform = lb_per_game[0]['Soma Any%'].game.__getattr__('platforms')[0]
 print(platform)

But this one does not :

for i in range(2):
  game = lb_per_game[0]['Soma Any%'].game
  platform = game.__getattr__('platforms')[0]
  print(platform)

It raises a Type Error at the second time you enter the loop on second line :

object of type 'Platform' has no len()
  File "C:\Users\charl\OneDrive\Documents\Projects\OWREstimator\srcdata.py", line 68, in <module>
    platform = game.__getattr__('platforms')[0]

By inspecting the debugger, it seems the game.platforms attribute equals to this at this moment :
'Traceback (most recent call last):\n File "c:\\Users\\charl\\.vscode\\extensions\\ms-python.python-2021.6.944021595\\pythonFiles\\lib\\python\\debugpy\\_vendored\\pydevd\\_pydevd_bundle\\pydevd_resolver.py", line 193, in _get_py_dictionary\n attr = getattr(var, name)\n File "C:\\Users\\charl\\miniconda3\\lib\\site-packages\\srcomapi\\datatypes.py", line 46, in __getattr__\n if type(self.data[attr]) is list and len(self.data[attr]) > 0 and len(self.data[attr][0]) == 8:\nTypeError: object of type \'Platform\' has no len()\n'
(same for all not null attributes)

@cmnemoi cmnemoi changed the title Canaccess game attributes in a loop Cannot access game attributes in a loop Jun 20, 2021
@cmnemoi cmnemoi closed this as completed Jun 20, 2021
@cmnemoi cmnemoi reopened this Jun 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant