9
\$\begingroup\$

I'm working on a program that uses the RIOT API League of Legends for collecting player data and calculate how skilled a player is by using a grading system (as of right now it just the average of the last ten games for each skills). Realize I used a lot of class, what is a better way to do it?

import requests



#ASKING USER FOR SUMMONER NAME


sumName = input('Enter summoner name:')


#COLLECTING DATA TO BE INSERTING FOR MATCHLIST DATABASE


url=('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/'+(sumName)+'?api_key='+(key))


response=requests.get(url)

accId=(response.json()['accountId'])


#COLLECTING DATA FOR THE NEXT DATABASE API


url2=('https://na1.api.riotgames.com/lol/match/v4/matchlists/by-account/'+(accId)+'?queue=420&endIndex=20&api_key='+(key))
response2=requests.get(url2)


i=0
GAMEID = []
Idgame=20

#COLLECTS GAME ID'S FOR NEXT DATABASE FOR 20 GAMES


while Idgame>0:
    GAMEID.append(response2.json()['matches'][i]['gameId'])
    i=i+1
    Idgame=Idgame-1















#COLLECTING DATA FROM GAME 1


class GAME1():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[0])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0


    #THIS COLLECT THE ID NUMBER OF THE PLAYER NAME THAT WAS INSERTED


    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
            
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']






#COLLECTING DATA FROM GAME 2    


class GAME2():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[1])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 3


class GAME3():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[2])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']






#COLLECTING DATA FROM GAME 4


class GAME4():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[3])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 5


class GAME5():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[4])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 6


class GAME6():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[5])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 7


class GAME7():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[6])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 8


class GAME8():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[7])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 9


class GAME9():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[8])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 10


class GAME10():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[9])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']








#COLLECTING DATA FROM GAME 11


class GAME11():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[10])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 12


class GAME12():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[11])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 13


class GAME13():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[12])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 14


class GAME14():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[13])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 15


class GAME15():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[14])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 16


class GAME16():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[15])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 17


class GAME17():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[16])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 18


class GAME18():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[17])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 19


class GAME19():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[18])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']







#COLLECTING DATA FROM GAME 20


class GAME20():
    url3=('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(GAMEID[19])+'?api_key='+(key))
    response3=requests.get(url3)
    Loop=0
    index=0
    while Loop<=10:

        if response3.json()['participantIdentities'][index]['player']['summonerName']!=sumName:
            Loop= Loop+1
            index=index+1
        elif response3.json()['participantIdentities'][index]['player']['summonerName']==sumName:
            break
    kills=response3.json()['participants'][index]['stats']['kills']
    deaths=response3.json()['participants'][index]['stats']['deaths']
    timer=response3.json()['gameDuration']
    assists=response3.json()['participants'][index]['stats']['assists']
    visions=response3.json()['participants'][index]['stats']['visionScore']
    csTotal=response3.json()['participants'][index]['stats']['totalMinionsKilled']
            



#Object from each game class


game1= GAME1()
game2= GAME2()
game3= GAME3()
game4= GAME4()
game5= GAME5()
game6= GAME6()
game7= GAME7()
game8= GAME8()
game9= GAME9()
game10= GAME10()


#Calcuating the average of 10 games for each stat


killsAvg= (game1.kills+game2.kills+game3.kills+game4.kills+game5.kills+game6.kills+game7.kills+game8.kills+game9.kills+game10.kills)/10
assistsAvg=(game1.assists+game2.assists+game3.assists+game4.assists+game5.assists+game6.assists+game7.assists+game8.assists+game9.assists+game10.assists)/10
deathsAvg=(game1.deaths+game2.deaths+game3.deaths+game4.deaths+game5.deaths+game6.deaths+game7.deaths+game8.deaths+game9.deaths+game10.deaths)/10
visionsAvg=(game1.visions+game2.visions+game3.visions+game4.visions+game5.visions+game6.visions+game7.visions+game8.visions+game9.visions+game10.visions)/10
csAvg=(game1.csTotal+game2.csTotal+game3.csTotal+game4.csTotal+game5.csTotal+game6.csTotal+game7.csTotal+game8.csTotal+game9.csTotal+game10.csTotal)/10

print('His average kills is '+str(killsAvg)+' in the last 10 games')
print('His average assists is '+str(assistsAvg)+' in the last 10 games')
print('His average deaths is '+str(deathsAvg)+' in the last 10 games')
print('His average visions is '+str(visionsAvg)+' in the last 10 games')
print('His average csing is '+str(csAvg)+' in the last 10 games')
\$\endgroup\$
6
  • \$\begingroup\$ I don't see the variable key defined anywhere? \$\endgroup\$
    – akozi
    Commented Aug 6, 2020 at 15:20
  • 1
    \$\begingroup\$ Please apply the black code formatter: github.com/psf/black \$\endgroup\$ Commented Aug 6, 2020 at 18:42
  • 1
    \$\begingroup\$ @MartinThoma Perhaps next time, not now answers have arrived. \$\endgroup\$
    – Mast
    Commented Aug 7, 2020 at 5:51
  • 3
    \$\begingroup\$ The current question title of your question is too generic to be helpful. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$
    – BCdotWEB
    Commented Aug 7, 2020 at 7:06
  • 1
    \$\begingroup\$ The question is not about the formatting and the answers I have seen don't focus on that part. You just make it harder for people to understand your code. \$\endgroup\$ Commented Aug 8, 2020 at 6:11

4 Answers 4

22
\$\begingroup\$

Classes

Why do you need a separate class for each game whose data you request? This is very inflexible, should you need to request the data for more or fewer games. Just use one class to represent a game. It looks like all the attributes in your game classes are identical except for the key or index used to access GAMEID. Therefore, just use a range based loop to get as many instances of the same class as you need, storing the instances in a data structure such as a list. This is exactly the motivation for having a class in the first place; you bundle data and functionality together so you can reuse it.

game1= GAME1()
game2= GAME2()
game3= GAME3()
game4= GAME4()
game5= GAME5()
game6= GAME6()
game7= GAME7()
game8= GAME8()
game9= GAME9()
game10= GAME10()

This whole thing should really be a loop.

Names

Also, naming. names like url2 are not very descriptive. What exactly is the url? The names should be clear and unambiguous. In this case it's not that big of a deal since you're only using them once or twice.

Functions/Methods

Your code doesn't have a single function in it. This is not good. Just look at that block of code that you copy-pasted for all 20 of your classes. If you had defined it as a function once you could just call that function 20 times. By using a loop that function would have to appear in code just one time.

That whole block at the bottom should be in a function too.

Ideally the only top level execution in your program should be this:

if __name__ == '__main__':
    main()

or

if __name__ == '__main__':
    # body of what would have been main()

to encapsulate all functionality in one place, after all the definitions, and to make sure that if the module is imported (namespace isn't __main__), it won't be executed.

Requests

after a statement like r = requests.get(), call r.raise_for_status() since it will notify you if the request fails. also, instead of concatenating arguments into the url string, call get() with two parameters. One being the API URL without your specific queries, and the other being a dictionary mapping keywords to your queries.

r = requests.get('https://...', {'keyword' : query})
r.raise_for_status()

Style

Too much white space. Refer to the Python style guide.

\$\endgroup\$
12
\$\begingroup\$

Abstracting classes

In your question you asked how to abstract classes so I thought I would show a concrete example of how this is done. Here is a minimal example of how you could abstract your game function.

class GAME():
    def __init__(self, ID):
        url = ('https://na1.api.riotgames.com/lol/match/v4/matches/'+str(ID)+'?api_key='+(key))
        response = requests.get(url)
        Loop = 0
        index = 0

        # THIS COLLECT THE ID NUMBER OF THE PLAYER NAME THAT WAS INSERTED

        while Loop <= 10:

            if response.json()['participantIdentities'][index]['player']['summonerName'] != sumName:
                Loop = Loop+1
                index = index+1
        elif response.json()['participantIdentities'][index]['player']['summonerName'] == sumName:
            break
            
        self.kills = response.json()['participants'][index]['stats']['kills']
        self.deaths = response.json()['participants'][index]['stats']['deaths']
        self.timer = response.json()['gameDuration']
        self.assists = response.json()['participants'][index]['stats']['assists']
        self.visions = response.json()['participants'][index]['stats']['visionScore']
        self.csTotal = response.json()['participants'][index]['stats']['totalMinionsKilled']

# Object from each game class


game1 = GAME(GAMEID[0])
game2 = GAME(GAMEID[1])
game3 = GAME(GAMEID[2])
game4 = GAME(GAMEID[3])
game5 = GAME(GAMEID[4])
game6 = GAME(GAMEID[5])
game7 = GAME(GAMEID[6])
game8 = GAME(GAMEID[7])
game9 = GAME(GAMEID[8])
game10 = GAME(GAMEID[9])

The __init__ function is ran on the creation of the class instance, you can see that it requires an ID to be handed when it is ran. You seem unfamiliar with some of pythons class syntax so to explain the self.var notation, just know that if a variable has self before it, then that variable will be accessible outside of the class. Whereas the variables like Loop, index, and response are not.

I still think it would be useful to read through some examples of how classes can be implemented but hopefully, this example shows how their purpose can be useful.

https://www.w3schools.com/python/python_classes.asp

\$\endgroup\$
2
  • \$\begingroup\$ Wouldn't it be better to make a list with the games, instead of having 10 lines? Also, according to pep8online.com, the code has several PEP8 issues. \$\endgroup\$ Commented Aug 7, 2020 at 15:12
  • 1
    \$\begingroup\$ Definitely, in this case i was trying to take a direct example with as few changes as possible to demonstrate the point. I was planning to later come up with an example of some other recommendations or see if someone else responded in the downtime, which it seems @ivo did very well :-) \$\endgroup\$
    – akozi
    Commented Aug 7, 2020 at 22:22
8
\$\begingroup\$

The other reviewers have already explained how you should re-organize your classes, but I want to expand a bit on how you create them and how it can save you a lot of typing.

So we assume that you use the classes created by akozi. Then we can put the creation of these classes inside a list comprehension.

nr_games=10
games=[GAME(GAMEID[i]) for i in range(nr_games)]

The notation might be new, but the result is just that you have a list containing your 10 games. If you don't like list comprehensions, then you can always replace them with for loops.

This is much easier to manage and manipulate. As a result your following steps become much easier as well.

killsAvg= sum( game.kills for game in games ) / nr_games
assistsAvg= sum( game.assists for game in games ) / nr_games
...

It might seem like this mainly saves you a lot of typing, but it has some other advantages. It is for example a lot easier to adjust and maintain, for example when changing the nr_games or when adding a new score type. Additionally, imagine that there is a typo somewhere in the code, then which version would you prefer to check?


Generally speaking, whenever you find that you have to do a lot of repeated typing/copy-pasting, stop for a moment and think whether there is a cleaner way to do things. Often times a lot of the typing can be solved by using data structures such as lists and dictionaries.

\$\endgroup\$
3
  • \$\begingroup\$ instead of nr_games=10, why not nr_games = count(GAMEID)? Also, wouldn't no_games be prefered, as "no." is the abbreviation of "number"? \$\endgroup\$ Commented Aug 7, 2020 at 15:07
  • \$\begingroup\$ You probably mean nr_games = len(GAMEID)? But yeah, that's definitely be an option. I didn't do it here because the original code has len(GAMEID) as 20 but only takes the average of the 10 first games. Personally I read no_... as 'not/no' instead of 'number'. So I would find it a bit confusing even though it might be more correct. \$\endgroup\$ Commented Aug 10, 2020 at 7:14
  • 1
    \$\begingroup\$ That's a good point, about no_games, didn't think about that. Regarding the other point, you're right, it does point to 20 games. However, in the code, you will notice that only GAME1() to GAME10() were used. All the calculations use 10 games, and average of those 10 only. However, 20 game ids are fetched anyway. Maybe it's intended to have more games later? Maybe O.P. got tired of writting everything for the average and decided to ask if there was a better solution? \$\endgroup\$ Commented Aug 10, 2020 at 8:44
6
\$\begingroup\$

One of the more costly bits nobody has mentioned:

Cache the result of response.json(). Cache other frequently used things.

I.e. write:

        decoded = response.json()
        while Loop<=10:
            if decoded['participantIdentities'][index]['player']['summonerName']!=sumName:
                Loop = Loop+1
                index = index+1
            elif decoded['participantIdentities'][index]['player']['summonerName']==sumName:
                break
            
        stats = decoded['participants'][index]['stats']
        self.kills=stats['kills']
        self.deaths=stats['deaths']
        self.timer=decoded['gameDuration']
        self.assists=stats['assists']
        self.visions=stats['visionScore']
        self.csTotal=stats['totalMinionsKilled']

That while loop should also be revised. Don't have two index variables. Don't limit yourself to 10 participants. If possible, use an appropriate python construct to find the index. Perhaps something like:

    index = [ a['player']['summonerName'] for a in decoded['participantIdentities'] ].index(sumName)
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.