For this demonstration of an implementation of
seq using the Python Prompt Toolkit, I’m actually going to run this in the terminal, first, and then I’ll go through the code. Because I think once you see how it works, a lot of this Python will make more sense. When I run this…
I’ll get a little dialog box instead of having to enter in command line arguments directly. And if I print out the help, here, it’ll tell me
Print numbers from FIRST to LAST, in steps of INCREMENT. But you’ll notice it’s not really like the conventional usage message, and that’s because when I click this Sequence action, it gives me all the prompting that I need here, right?
So, I can enter in an argument, FIRST, which is just
1. Maybe I enter in
4 as the INCREMENT, and then maybe for LAST, I’ll just put in
22—and actually, I’ll put in a space after that, just that you can see that if I put in a space it’ll say Ensure that you enter a number, because it’s technically malformed input. So, that’s how you get a little bit of error handling inbuilt into here, which is pretty nice. And it’ll just let me enter it again, so I’ll enter
22, and then it prints out starting at
1, going up by
4, all the way until it gets to
22. So, pretty darn cool!
And this is not quite as fully-featured as the normal version of
seq, because I haven’t added a separator option, and I also haven’t allowed for negative number input, but that’s just to make this a little bit simpler to demonstrate.
So, I’m actually going to start from the very bottom here and go down to the
main() function. So, the key to this
main() function is this
button_dialog, which is called by calling its
version(). So, going up to these, you can see that
version() are just functions that print something, so this is a little bit different from other kinds of command line interface packages that you’ve seen that handle the version and the help by directly printing. This one handles it by calling these functions, which then print things. So, going back down here to this
seq_dlg() you can see it has this kind of logic where there are these three labels, some operands, and it has a
while True loop that generates an
input_dialog and it prompts you to enter an argument, and the argument is based on one of these labels.
So, you’re looping through these labels and then it does some basic error handling here, checks to see if the input is a digit, and, if so, it appends it to the
operands, otherwise gives an error dialog, and then it breaks out when there are three total operands.
So, you could think of ways to make this better or more concise, but what this is doing is pretty simple. It’s just looping through and prompting you to enter
"INCREMENT", and then
"LAST", and then breaking out when it gets all the operands.
So, this kind of interface is what’s called a graphical user interface, or a GUI, or even some people call it a “goo-ey”. So, a GUI is nice because it often allows you to handle errors from the user by simply limiting the kinds of things that they can actually input to your program. That’s what’s awesome about a GUI. On the command line, you rely on your user to know the bounds of the kinds of things that they can actually input to your program, right? They have to input a number, for example, in this
seq() case. But with a graphical user interface, you have a lot more flexibility in terms of how you can output an error dialog and then just keep on going and prompt them to enter stuff again.
That’s not unique to GUIs of course, but it’s something that works pretty easily in a GUI setting, which is quite nice. So, the overall thing to remember here is that Python Prompt Toolkit, which of course you have to install in some way—probably with
pip, and then you have to actually import all of the things that you’ll need from that library—the thing to remember is that this is simply an alternative kind of command line interface. It’s not anything better or worse, it’s just a different way of thinking about this philosophy.
Become a Member to join the conversation.