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