#100DaysOfWeb in Python Transcripts
Chapter: Day 50: Responder framework
Lecture: Adding a data backend

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, in order to actually implement these methods we need some data so I'm going to give you the data that actually powers that search
0:07 that movie service that I showed you at the beginning so I'm going to just drop that in here, going to be a data folder
0:13 and in the data folder we're going to have a movie.csv super-exciting, right and we're going to have this DB, database.py
0:21 and it's going to return this named tuple which looks like that so it could be a class but we just got this simple thing
0:27 that we're reading from that CSV so the main important thing here is that we need to call global_init.
0:34 It's going to go find where the actual CSV file is. It's going to iterate over the rows and convert them into these movie objects
0:43 and then put them into a lookup, by IMDb code so we can kind of index into our in-memory database here so we can ask it things like
0:51 search for them by title and get a list back search by keyword and get a list back or just get an individual movie
0:58 out of our little fake database here. Okay, so that's super-easy. We come over here, and we're going to say from data import db.
1:07 Now this one, this one looks pretty easy. Actually, let's do the IMDb one. This one looks easiest, I think so we'll say movie equals db.find_by_imdb
1:17 and we're going to give it IMDb number. If this is none or it doesn't exist we're just getting None back for the movie
1:23 so we could do something like check whether or not the movie's there, return a 404. For now I'm just going to try to return the movie
1:31 just the details of the movie. I don't think it's going to work but let's find out what happens. Oh, I think we forgot to call the global_init.
1:37 This is probably going to fail, but let's try. Yep, it got us nothing back and it should have given us something.
1:43 It didn't crash, it just said there's no data found and the reason is somewhere along the way maybe in the startup here, I'm not sure
1:50 we need to call db.global_init once. Okay, try again, server error. Okay, remember I told you I didn't think this was going to work. What's wrong?
1:57 This object is not JSON serializable so the important thing to note here is the entire object graph like if it's the list, everything the list contains
2:07 and the items within that list point to, and so on have to be JSON serializable. This movie is a class. It's not, so we need some way to convert it
2:16 to JSON. If we look over here actually wrote a method somewhere here at the top movie_to_dict, right, so it's going to create a dictionary
2:25 and dictionaries long as their elements are serializable themselves, are so that'll be nice and easy so all we got to do is say db.movie_to_dict
2:35 and then this should work because the important thing is that that's basically a Python dictionary
2:40 or a list of dictionaries, or something along those lines. All right, ready? Boom, look at that. The Abyss by James Cameron. How cool is that?
2:48 So, we've already implemented our method, our first method. We went to the database, found our movie
2:54 converted our movie from a movie object into a dictionary set the response media. Boom, already knows that's JSON. Off it goes. How cool is that?
3:03 This is pretty easy, right?


Talk Python's Mastodon Michael Kennedy's Mastodon