#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the top-scores method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, let's do the top scores. Over here in Postman we get, these are the top scores as HTML, not what we want. So let's write these.
0:12 Go ahead and put a little bit of code here like before We're going to go get all the players and we're going to compute the wins.
0:18 We're going to store the player as JSON and the score as the number of wins for that player. So this is a list of dictionaries that contains
0:29 two things, the player and the score. And there's no particular order to this, but because it contains the score, what we can do is simply go down here
0:37 and say we're going to sort, give it the function that's going to say, go get the score for each one
0:43 and sort in reverse order with that negative there. Okay? So now we want a return flask.jsonify. Do you want to return all of them?
0:53 What if there's 100,000 players? Not a great idea, so let's just return the top 10, and we can use a slice.
1:00 So it'll construct here, if we haven't talked about it previously we'll say, given a large array, return either the whole array or just the first 10
1:07 if it's larger than 10. Now this is not going to return anything too amazing yet. It's given our two players, but they both have score,
1:18 actually our three players, we all have the score 0, 0, and 0. But at least you can kind of see that it's working and then
1:24 what do you think, you can't say the sort's wrong, because it is sorting, but as we start playing, you'll see the highest winning players
1:30 appearing at the top here, of course.


Talk Python's Mastodon Michael Kennedy's Mastodon