You may also create your own modules for your own projects. This is an example of a module created as an exercise when the author was first learning Python.
- [Instructor] Python makes it relatively easy to create your own reusable modules. Here in Komodo I've opened a working copy of saytime-test.py from chapter 14 of the exercise files. This is an exercise that I tend to give myself every time I learn a new language, and, of course, I learned Python a number of years ago, and so this module has been around for a while. But what this particular exercise is is to write a class that will print the time in words.
So, when I run this, you notice on the last line here, there's a lot of unit tests before that. It says, "local time is fourteen til four." And so the exercise is for me to be able to write a module that will give me the time in words like that. And if we look up here at the unit test data, I've given it a number of times of day: zero zero is midnight, one past midnight, 11 o'clock, noon, one o'clock, 29 past noon, half past noon, 29 'til one, quarter past noon.
So there's all these different times in words and this is the way that a person might look at their analog clock and they'll say it's a quarter to three. And then I have tests for boundaries: 160, that's out of range, 2,400, that's out of range. Those aren't real times. And of course, in order to do this I need to start with numbers, and so here's my numbers tests: Oh, one, two, three, four, etc, 10, 12, 15. And so, it has to be able to speak all of these numbers.
It only goes up to a thousand and so a thousand is out of range, 999. I think my C language version I went crazy and had it go into the billions, but this is my Python version. So that's basically how this works. This is the test script that imports the module, so I have import saytime, that imports the module, and then in my main here I create an object, and I print numbers test, and then here's a list of numbers that we test, and so, for in the list, we print the number in words.
And then we do the time test, and here's a list of tuples with hour and minute, and then we print those in words. And then we print the local time, setting the time to now. And so, when I run it, of course, now it's 11 'til four, because I've been running my mouth for a few minutes. Now, if we come over here to the saytime module, we can take a quick tour through that. Again, I'm not going to go through all the details, but you have the module, and you can certainly look at it and dissect it all you like.
So, first I have a class called numwords, which returns a number as words, so 42 becomes forty-two, and you see that I have that nice little docstring there. And then I have my data, so I am of the belief, and this has always worked for me, that if you design your data before you write your code, your code is going to be a lot easier to write. And so in this case, I designed my data very carefully, and you'll notice that my words list is a dictionary of tuples.
So, ones, and there's oh, one, two, three. Tens, 10, 20, 30, 40. Teens, 10, 11, 12. And so, by designing my data like this it made my code very intuitive to write, and so I have words, ones, sub n; words, teens, sub n. So, actually getting in there and finding the data becomes relatively simple. I then have a saytime class, which inherits from my numwords class, return the time from two parameters as words, and so I have specials, noon, and this is a dictionary, noon, midnight, 'til, and past.
So it would be easy to internationalize this to make it work in different languages, wouldn't it? And when it comes down here to the words, again, because the data is designed well, and I've inherited from the numbers class, that's not a lot of code to do what it is we're talking about doing. And again, you can go through this code in detail because you'll have all this code. And then I have a special class called saytime_t, which inherits from saytime, and all it does is it initializes itself with the current time and then you can print the words.
So, you'll notice that this is a module and at the bottom of the module I have this if name equals main main. So if I run the module as a module, as I did before, then I can import it and I can use it and I can get all of this. But if I run the module directly, when I run it I just get eight 'til four because what main does is, first, it tests to see if we have any command line arguments. So when I run this from a command line I can say saytime.py space test, and it'll run my unit tests.
But if I don't do that, it just prints the current time in words, right down here on line 141. So, these are some useful techniques for creating your own modules, and this is a relatively simple process, especially once you understand the language, which, hopefully, if you've gone through the course and sequence, you now understand the language pretty well. As always, I suggest you write some code of your own to gain experience creating modules.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api