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