Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
Oftentimes as you are writing code you'll find pieces of code that you want to reuse in different projects. For this you'll want to write here own modules. So let's take a look at how we do this. For example, if this is my homepage and if you look down at the bottom of the page you'll see that it tells the time and it says it in words. It says it's twenty past three. Now, I did this with a Python module, and we're going to take a look at that module, and here it is. Now, the point of this is to show you how to put the code into the module.
We'll look at the details of the module in another lesson. But for now let's see that it has this class numwords. But for now let's just look at from the perspective of how we put the code into a module. So you'll notice that it has this class numwords, and this formats a number into words. And it has this other class saytime_t that inherits numwords and that class is for formatting the time as words.
And then it has a special version of saytime as a wrapper for using a time object. And it has a main function, so that it can be run from the command line. And it has a test function for testing. And so all of this is put in a normal file and really there is nothing special about the file. In fact, if I run it-- we'll go ahead and do that. You see that it gives the time right now, twenty-eight til five.
It can also be run with an argument that hasn't run these tests. And in Eclipse, so a little bit of a production to get it to do that, to come over here and give it an argument, say test, and Apply and Run, and then it runs it in test mode. And you'll see if I do that from the command line, I just give the word test on the command line, and it runs all of these tests. It tests all the numbers and it tests the time.
I'd like to do things this way. You can also use the unit testing module that comes with Python. And so I have a lot of results that I could look at. So the module itself you just write it like you write a normal script. You write your script and in the process you write whatever classes you're going to write. And you think about how it will be accessible from the outside world. And then when it's time to use it as a module you'll notice down here at the bottom it has this if _name_ pattern, we've seen this before.
And that prevents the main code from being run when it's imported as a module. So I don't have any code that's outside of a function. And that's generally true when I write a script any way. And here is the script on my web site. It's a very short script. That's the entire thing. And all it does is it imports time, because it grabs the time as a localtime variable, and it also wants to be able to format that time so that it can format the date and time as well. And it imports the saytime module and it calls that here.
It says "In Phoenix Arizona, it is now," and it has the saytime words. And it has the formatted date, on day of the week, and the day, month, and year. And so when we run that you'll see it has the CGI header because this is included in a page as a server side include. And it says, "In Phoenix, Arizona, it is now," and it gives the time, twenty-six til five, on this date. And so when you have code that you want to reuse you're going to want to put it in a module and when you put it in a module it's really just like writing a normal script.
You just need to be careful that you don't have any code that's outside of a class or a function, and you want to have this pattern down here at the bottom for running main. So that you have some way to test it or as I do with this one, to actually just use it as a script. Let's go ahead and take that out. It's still got the argument in there. So that when it's not run with the test argument it just runs it as a script and it gives the time so you can include it in a shell script or you can use it in a lot of different ways. So I'm looking for flexibility when I do this.
But that's essentially all there is to it. You just put your classes in there, set it up with the pattern at the bottom so that it only runs main when it's not imported, and so it doesn't do that when it's imported and then your class becomes available to other scripts. And you'll see a lot of examples of this as we go through the projects later on in the course.
Find answers to the most frequently asked questions about Python 3 Essential Training.
Here are the FAQs that matched your search "":
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.