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