Using and Mastering Cookiecutter Transcripts
Chapter: Programmatic Cookiecutter: Using the API
Lecture: Gathering game maker data

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Okay let's begin having this program, which if I run it does nothing so far. Let's begin by having it have a cool little output here,
0:09 so I'll call this print_welcome() or something like that, we'll create this function, and we'll put that there, let's move that below main, okay,
0:18 now if we run it, it says welcome to the game creator, we'll create either hi-lo, pacman, or pong for you,
0:23 so this is slightly better experience, I understand this is not that big of a deal, but, it's still cool that we have a little bit of a
0:32 welcome to the game creator type thing going on here, the first thing that we're going to do is we're going to gather the inputs,
0:38 so we want to ask them questions like hey what's your name, and what kind of game do you want to make,
0:43 basically the things that Cookiecutter would ask and potentially other ones as well, but remember, in Cookiecutter, we have the Cookiecutter defaults
0:50 so it already knows what my name is, it already knows what my GitHub username is, things like that.
0:56 So as much as possible, we would like to just leverage the defaults and not even ask, right, just go hey we got the default values,
1:01 but how do we do that, well, that's the first step into the Cookiecutter API. So when I come over here and say import cookiecutter
1:09 and there is a bunch of stuff here, we're going to get the cookiecutter.config Now, let's go down here at the gather inputs,
1:15 and we're going to say the config = cookiecutter.config.get_user_config() Now, it seems like that is what you might want to have here,
1:24 okay, you might want to say this, but it turns out if we just print this out, this is actually not what we're looking for, if we look here,
1:32 this is the, it's like the full thing, and then down here, there is this default context and in the default context we have our name,
1:38 our email and so on, so it's really the default context that we want, so let's maybe say something like this, ctx = config.get('default_context')
1:48 this is just the dictionary, so now, if we print out our context, I think that's what we're looking for, perfect,
1:54 so we can grab like full name and store it. So once we have this, we can start writing code like this,
2:00 so we can say if, we'll say full_name = ctx.get('full_name') right and this get function on a dictionary in Python will return none if it's not there,
2:11 or it will return value for this, so we'll just say if we found a full name, let's say if we didn't find the full name, then we'll say full_name =
2:20 and we'll have to ask and let's see, what else do we need to do that for. We have to figure out what type of game it is,
2:27 so we can ask a similar type of question, this time, I don't really think there is a reasonable default
2:33 like somebody doesn't always want to make Pacman or something and let me just tell PyCharm this is not misspelled, so it doesn't look wrong to you.
2:39 So we can figure out the game type that's going to be fun, and let's figure out the package name,
2:46 so we'll say do an input('What do you call your game?') Now, they might have spaces, a punctuation, all that kind of stuff,
2:54 so I wrote a function here that we can take and transform package name and we'll say to package style, right,
3:01 what this is going to do if you just look down here, it's going to get rid of all the white space and double spaces and things like that,
3:07 it's going to kind of normalize it, it's like what we've seen before with the project slug but this is actually more robust than that.
3:13 We can also ask questions like where do you want to create it, so let's come down here and ask this, we can even ask it in a loop,
3:18 in case it get it wrong, now, this I am not sure how great of a user experience this is, but I am pointing this out
3:24 because this is not possible in Cookiecutter, and it is possible here, so we can ask like this, we can come down here
3:30 and say what is the full path to where your project, where you are going to create it, give us a directory,
3:35 and if they give us something wrong, instead of just trying to create it and fail,
3:39 we can go no, no, actually that directory you gave us, that didn't work, let's try it again, give us another directory, right,
3:46 so because we're doing this in our own code, we have our own flexibility, and just to be clear, this doesn't have to be Python code,
3:50 this could be any code, any code that we want to write this in as long as we can somehow shell out to interact with Python for the API.
3:57 Okay, so let's add that to the top there, okay, so we have our working directory, and then I think we'll have everything we need to create the game.
4:06 Now, one final thing I'd like to do is package this up a little bit, and I really like returning a bunch of data in the form we would have to there
4:13 so let's go and create using a named collection here, let's go and create a game create info, and it's going to have a project name,
4:22 full name, game type, a working dir, surprise, those are the things we needed and let's have that function here return it.
4:28 so we can come down here and just say actually, let's just return this. So we'll need project name, we'll need full name, game type,
4:37 and working dir. package name, not project name. So that is going to create it, and let's just make sure this first part is working,
4:46 I'll just print out the create info and then I want to come back
4:50 and we are going to actually do the building in a minute, let's run it see what we get. So let's come down here I want to play Pong,
4:56 okay great, I am going to call it "The great ping pong", the full path will just be this, boom, we're going to create this directory
5:07 and there is the full name, notice, it didn't ask me what my full name was, right, because it got it from the defaults. the game type is Pong,
5:15 and the working directory is just right here. Okay great, so it looks like we're gathering the inputs perfectly well here.
5:21 The next thing we need to do is actually use those inputs to build the game and this is where we're going to execute the Cookiecutter template.


Talk Python's Mastodon Michael Kennedy's Mastodon