#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Ensuring starter data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 When you saw me get started with Flask here, and I created a main method, and the main method we call run. And then down here where if the,
0:08 the main convention where I call main, you might have thought, "Well, why don't you just run down there? Come on. Like, what are you doing?"
0:14 It turns out that in real web applications, there's tons of startup and configuration code. Configuring the logger, configuring the
0:19 database connections, etc., etc. So I did this with that fact in mind. So one of the things that we're going to need to do is
0:27 we actually need to make sure that the starter data that they application is going to use exists. All the rules have have been created in the database.
0:36 There's always going to need to be a computer, sort of opponent user, like a built-in user. We are going to create that as just a standard user
0:44 in the database with a special lookup and things like that. So let's go over here, and maybe we can make this more obvious. We can say...
0:56 "Run web app," and we can take this and put that down there. And up here, we'll say, "Run web app" as the last thing,
1:05 but let's call another one, let's say, "Build starter data." And we'll define Builder Starter Data. Well, what goes in here?
1:12 Well, we can go to our game decider. Go up here and say, "from game_logic import game_decider" We're also going to need GameService later
1:29 in just a second. So we'll come down here and say, game_decider.all_rollnames." We can just print out roll names, just to see what's
1:45 coming along really quick here. And let's just throw a quick return so the app doesn't actually start.
1:53 Here you can see we are getting all the roll names. That's really cool. So it looks like that's working. And then the GameService,
2:01 this is the thing that talks to the database. It has an init rolls, and if you give it the roll names,
2:05 it will make sure that all these are created in the database, have ID's and things like that. If they're already existing,
2:12 this is just going to bail out of that. The other thing has to do with that computer users. So let's come over here and say, Computer = GameService()
2:23 Find player by computer." And if the computer already exists, that's great. But if it doesn't, we're going to create this one.
2:36 We'll create the player called computer. Okay, so we want to make sure this happens every single time. Now, it's kind of silly because, really,
2:44 it only has to happen once, and then the database is initialized. But have this here to make any changes,
2:48 make any additions to, will be really, really nice. So, before we run our web app, we're going to make sure that the starter date, or like the rolls,
2:55 and the computer player, and so on, are all there. Let's go ahead and run this and see how it works. Run it and nothing happens. Well, not nothing.
3:03 It built the starter data and then I, sure, sure I could do this right here, right? So if we run it now, it should be running the web app.
3:09 But let's look in here. Woo, look what we got over here. We have our Rock Paper Scissors SQLite database. Let's put over into here and see what we got.
3:30 There are all the rolls. Created that time: rock, gun, lightning, devil, and so on. If we run it again and again, we're never going to have
3:37 more than 15 rolls. The other thing that we might want to look at is from players. And now we just have our computer that we created, as well.
3:45 So we have our starter data. Now our system is just going to check. Like this init_rolls, if we go to there, you'll see.
3:56 It says, "Look, if we have some here, let's just get out." Right? Similarly, we check to see if the player already exists,
4:02 and if they do, we're just not going to do anything. But if they don't exist, we're going to create them the one time.
4:08 All right, so now we have our models imported. And remember, this is effectively just what we did in the previous demo. There's nothing changed here.
4:17 We just dropped it in here and called a couple of functions in the beginning of our web app before we let it run every
4:22 time it starts up, to make sure out database is all set up.


Talk Python's Mastodon Michael Kennedy's Mastodon