Python for Absolute Beginners Transcripts
Chapter: Using external Python packages
Lecture: Word completion, substring edition

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We have our game working pretty well it's running again over here. Remember to get the completer to work the drop down thing, it has to be over here
0:08 in your terminal or command prompt. Your name and then we can type r, we can type s. Couple of things that would be nice;
0:15 The selection of which one you've selected is not as obvious as I would like. Is it sponge or is it scissors? Without looking at the line above
0:22 well, I don't really know because they kind of both look selected in some way. Guess if there were more, it'd be obvious.
0:28 Also, I would like to be able to not have to start typing. I would like to be able to use this little drop-down and just hit Dot
0:33 and have it show me all of them. I would also like to be able to type just anything with an o or r and have that pull it up.
0:40 Like Air, Water, Fire and Rock. Pretty much almost all- That's all of them but, some of the sub-letters are sub-word matching, right?
0:49 So if it was like, Fire Rock and you just wanted to type Rock and get it to work that's what we're going to add here. So this is pretty good, and what
0:58 we need to do to make that happen is we need to make another one of these completers. Now, the way we're going to build this
1:06 uses a concept we haven't yet talked about in Python. So, what we're going to do is we're going to have to create a class.
1:13 Class is something from the object oriented side of programming of Python and it's a way to bundle both data and behavior together.
1:21 You'll want to create a class don't worry too much about the details. Later class courses go into object oriented programming and whatnot
1:30 but this is pretty basic so I'm sure you can follow along. We're going to create this thing called a PlayCompleter, and this name we make up.
1:38 Just like we called this file name and we called that directory here, we called that We can call that whatever we want
1:43 but it has to be based on this completer thing here. And completer comes from the prompt_toolkit. So it's like a foundational information there.
1:54 And then we're going to give this the function get_completions, and it takes all of this stuff. Really cool that it has these type annotations
2:03 but I'm going to just take them out for now. Keep it simple, 'cause we don't technically need them. Here we go, now what do we do with
2:11 Let's put put this up here. Our job to extend or customize this prompt tool kit library is going to pass in basically the terminal as it is
2:22 and what it's trying to work with. And then we're going to be able to say well gimme the word. and then here's what we've computed the
2:30 recommendations for what the dropdown should be. The first thing we're going to need to know is the roll names
2:35 and this has to be a list so when I go to the rolls the keys convert that into a list. Remember that roll.keys is like a list
2:43 you can go through it but it's not technically a list and so it's a dictionary key collection or something. So we got to do this or it won't work.
2:50 The word, we're going to get that from the document. You know there's nothing totally discoverable about this to tell you what you need to do
2:57 but if you go to the documentation you want to get word before a cursor. Like that, and that's what they've typed.
3:04 So if they type ai and they want Air completed what we're going to get for word is ai. Then we might want to complete all of them
3:14 if there's no word here. Or if they type dot, I want to be able to type dot and just have the dropdown drop down.
3:20 So that's what we're going to do right here and save not word. So our first case is if there's nothing typed
3:33 we're going to make sure that something is here. We're going to type word equals dot. So, if there's a word we're not going to Complete All
3:43 otherwise we're going to just check and see if it's a dot. Then we need to store up the completions. So these are the actual words that match
3:52 that we want to work with. So, a lot of setting the stage and then what we're going to do is going to say for roll in roll_names.
4:00 If we're going to Complete All then we want to go to Completions and Append roll. So what's happening here is we're going to go
4:08 through all of these and say if it's like something we want to show all of them just through every one and put it in a list, okay?
4:14 We can also do another test here or if the word is in the roll. So this is the sub word like ai and here are the rolls Air, so we want to put Air in.
4:24 But if it's Rock, where it is not ai is not in rock so we're not going to put it in the list of completions. So either we want to put them all there
4:32 our word is sub string of the roll the word that we're looking for and trying to complete, and then we're going to add it there.
4:39 Now it would be nice to just let's go ahead and say Return Completions. That's really all that we have to do.
4:44 However, for the completion to work it's not enough to put just the string here. We have to put this thing called a Completion.
4:54 Completion, import that from prompt_toolkit and this takes extra information. While the text is going to be roll.
5:01 Let's wrap this around so we can see a little bit better. I see the start position. I'm going to go back and basically erase what they've typed.
5:12 If they type ai we're going to go back two, and overwrite ai. We're going to set a style, this is like the color and whatnot.
5:21 Set the foreground to white and the background to dark green. And that's not misspelled, not really. And then we save the selected style
5:30 going to be something like this but not exactly. The yellow and green. You can play around with these and
5:37 see what they look like, and if you like them. Okay well, that probably is a little bit complicated but it's just going through the documentation for
5:44 how this prompt_toolkit works. Let's run through it again. We're given basically the terminal we say Alright, we need all the names
5:51 that we could complete.' We're going to get the word, whatever they've typed. We need to compute do we just put everything in there?
5:58 This is the little test for that. Then we got to bundle up all the stuff that we say These are the words I want you to show in the dropdown.'
6:05 go through all the possible names and either add them all or, this is our other test is it a sub string? If that's true, we're going to create one of
6:12 these completion objects, put it in the list and give them back. All right, let's go and try to use this thing up here
6:18 where we did our word completer before. We had this, now all we got to do is change that, and that.
6:25 And if we did everything right, that should work, right? So what we usually pass the completer this library asks this little class, this object
6:32 Hey, here's some words. This is what they've typed what do you want to do? How do we complete that?' Well, let's give it a shot.
6:38 Remember, we cannot run it in here it's just going to go back to the old brain input. Got to run it out here. All right, fingers crossed!
6:46 Micheal, my roll is Watch if I hit dot, yes! It'll pick scissors, see how cool that is? And look how obvious which one is selected
6:53 it even matches the background color of what's shown there. So, let's throw some sponge, yes! Now if I type o, look at that. Rock, scissors, sponge.
7:04 If I type space, that's where it's not the word the strip thing, and we can do that as well. So if you just hit space you get this
7:10 if you hit dot you get this. Or if you type something like s or r well R is a little bit too much. E, here's all of the things that have e in it
7:18 how cool is that? Incredibly simple. Throw some water into it. Water, oops got to still select it. Boom, water. Do it again, you know what?
7:26 We're going to finish this out with some fire! Take the round, finally yes! Phase through the sponge, burnt that thing up
7:33 game over, Micheal takes the game. How awesome, so I really really love the way this is working. It's not huge advanced user interface
7:40 but it is much better than what we had before. So all we had to do is extend this a little bit pass it over here to this prompt_toolkit.
7:47 Here's the class that we wrote. It just derives from completer which means it takes some of that information and then has this little bit of extra.
7:55 We're going to go down here. I guess we could probably make this clearer if we use some variable names. I could say Let's do it like this.
8:05 It's sub string. That'll either complete them all or if it's a sub string and then here, this. And completion is okay.
8:23 Here we go, either you complete them all or it's a sub string. And if it's a sub string, either way
8:28 we're going to create one of these completions pieces and then add it to our list and we're trying to make sure it's going to work again.
8:34 Not going to be able to find out there, are we? Dot, works like a champ, awesome. Well, there it is, we've taken two external packages.
8:43 Up here at the top, you can see them. Colorama and prompt_toolkit and we started working with all the capabilities and features they provide for us.
8:51 And we got a much better game and we had to know very, very little about working with the colors, working with the terminal stuff.
8:58 Whatever is involved in there. To do the dropdown and all that all we got to do work with the simple APIs
9:03 from these public pre-opened sourced packages. It's great.


Talk Python's Mastodon Michael Kennedy's Mastodon