Click here to Skip to main content
15,898,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a python class and added a `__repr__` function that will present to parameters (`name`, that is a string, and `dist`, that is an integer) of the object. I have a function that adds an object to the class and prints it. The code works, and the objects are printed by an alphabetical order of the names, because they come in to the function in that order. I want to print the objects sorted by their `dist` parameter. How do I do that?

Here is the full code:

Python
import requests
    import json
    from statistics import mean
    
    class Player():
        dist = 0
        name = ""
        ID = 0
        def __init__(self, ID, name):
            self.name = name
            self.ID = ID
            return
        def __repr__(self):
            return self.name + " " + str(self.dist)
        
    def find_id(a):
        people = []
        for x in a:
            people.append(Player(x[0], x[1])) #add name as key and ID as value to playoff_name dictionary
        return people
    
    def find_dist(a):
        list1 = []
        for x in a:
            list1.append(x[16])#add defender distance to list
        return list1
    
    def Shots(Id, playoff_names, playoff_dict):
        print("another one")
        url2 = 'http://stats.nba.com/stats/playerdashptshotlog?DateFrom=&DateTo=&GameSegment=' + \
               '&LastNGames=0&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&Period=' + \
               '0&PlayerID='+ str(Id) + '&Season=2014-15&SeasonSegment=&SeasonType=Playoffs&TeamID=0&' + \
               'VsConference=&VsDivision=' #use url for every player with id
        response1 = requests.get(url2)
        response1.raise_for_status() # raise exception if invalid response
        shots1 = response1.json()['resultSets'][0]['rowSet']
        if not len(shots1):
            return
        for name, number in playoff_name.items():
            if Id == number:
                list1 = find_dist(shots1)
                list1 = sum(list1) / float(len(list1))
                try:
                    playoff_dict[name] = list1 #add name as key and average (saved in distance) as value to playoff_dict
                except:
                    print(name, list1)
                    return
                list1 = []#empty distance for next player
        return playoff_dict
    
    #-------------------------------Code starts here
    shots_url = 'http://stats.nba.com/stats/leaguedashplayerbiostats?College=&Conference' + \
          '=&Country=&DateFrom=&DateTo=&Division=&DraftPick=&DraftYear=&GameScope=&' + \
          'GameSegment=&Height=&LastNGames=0&LeagueID=00&Location=&Month=0&Opponent' + \
          'TeamID=0&Outcome=&PORound=0&PerMode=PerGame&Period=0&PlayerExperience=&' + \
          'PlayerPosition=&Season=2014-15&SeasonSegment=&SeasonType=Playoffs&ShotCl' + \
          'ockRange=&StarterBench=&TeamID=0&VsConference=&VsDivision=&Weight='
    
    # request the URL and parse the JSON
    response = requests.get(shots_url)
    response.raise_for_status() # raise exception if invalid response
    shots = response.json()['resultSets'][0]['rowSet']
    list1 = []
    
    players = find_id(shots)
    #print(playoff_name)#check
    
    distance = []
    for player in players:
        url2 = 'http://stats.nba.com/stats/playerdashptshotlog?DateFrom=&DateTo=&GameSegment=' + \
               '&LastNGames=0&LeagueID=00&Location=&Month=0&OpponentTeamID=0&Outcome=&Period=' + \
               '0&PlayerID='+ str(player.ID) + '&Season=2014-15&SeasonSegment=&SeasonType=Playoffs&TeamID=0&' + \
               'VsConference=&VsDivision='
        response1 = requests.get(url2)
        response1.raise_for_status()
        shots1 = response1.json()['resultSets'][0]['rowSet']
        if len(shots1):   
            list1 = find_dist(shots1)
            average = sum(list1) / float(len(list1))
            player.dist = average
            list1 = []
            print(player)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900