#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the top-scores method
0:00 All right, let's do the top scores.
0:02 Over here in Postman we get, these are the top scores
0:08 as HTML, not what we want.
0:10 So let's write these.
0:11 Go ahead and put a little bit of code here like before
0:14 We're going to go get all the players
0:15 and we're going to compute the wins.
0:17 We're going to store the player as JSON
0:20 and the score as the number of wins for that player.
0:23 So this is a list of dictionaries that contains
0:28 two things, the player and the score.
0:30 And there's no particular order to this,
0:32 but because it contains the score,
0:35 what we can do is simply go down here
0:36 and say we're going to sort, give it the function
0:39 that's going to say, go get the score for each one
0:42 and sort in reverse order with that negative there.
0:47 So now we want a return flask.jsonify.
0:51 Do you want to return all of them?
0:52 What if there's 100,000 players?
0:54 Not a great idea, so let's just return the top 10,
0:57 and we can use a slice.
0:59 So it'll construct here, if we haven't talked
1:01 about it previously we'll say,
1:02 given a large array, return either
1:04 the whole array or just the first 10
1:06 if it's larger than 10.
1:09 Now this is not going to return anything too amazing yet.
1:14 It's given our two players, but they both have score,
1:17 actually our three players, we all have the score
1:19 0, 0, and 0.
1:20 But at least you can kind of see that it's working and then
1:23 what do you think, you can't say the sort's wrong,
1:25 because it is sorting, but as we start playing,
1:27 you'll see the highest winning players
1:29 appearing at the top here, of course.