1
00:00:00,000 --> 00:00:03,990
Oftentimes as you are writing code you'll find pieces of code that you want to

2
00:00:04,000 --> 00:00:07,990
reuse in different projects. For this you'll want to write here own modules.

3
00:00:08,000 --> 00:00:10,990
So let's take a look at how we do this.

4
00:00:11,000 --> 00:00:14,990
For example, if this is my homepage and if you look down at the bottom of the

5
00:00:15,000 --> 00:00:17,990
page you'll see that it tells the time and it says it in words.

6
00:00:18,000 --> 00:00:20,990
It says it's twenty past three.

7
00:00:21,000 --> 00:00:24,990
Now, I did this with a Python module, and we're going to take a look at that

8
00:00:25,000 --> 00:00:26,990
module, and here it is.

9
00:00:27,000 --> 00:00:30,990
Now, the point of this is to show you how to put the code into the module.

10
00:00:31,000 --> 00:00:33,990
We'll look at the details of the module in another lesson.

11
00:00:34,000 --> 00:00:38,990
But for now let's see that it has this class numwords.

12
00:00:39,000 --> 00:00:43,990
But for now let's just look at from the perspective of how we put the code into a module.

13
00:00:44,000 --> 00:00:50,990
So you'll notice that it has this class numwords, and this formats a number into words.

14
00:00:51,000 --> 00:00:56,990
And it has this other class saytime_t that inherits numwords and that

15
00:00:57,000 --> 00:01:01,990
class is for formatting the time as words.

16
00:01:02,000 --> 00:01:10,990
And then it has a special version of saytime as a wrapper for using a time object.

17
00:01:11,000 --> 00:01:14,990
And it has a main function, so that it can be run from the command line.

18
00:01:15,000 --> 00:01:18,990
And it has a test function for testing.

19
00:01:19,000 --> 00:01:22,990
And so all of this is put in a normal file and really there is nothing

20
00:01:23,000 --> 00:01:24,990
special about the file.

21
00:01:25,000 --> 00:01:29,990
In fact, if I run it-- we'll go ahead and do that.

22
00:01:30,000 --> 00:01:33,990
You see that it gives the time right now, twenty-eight til five.

23
00:01:34,000 --> 00:01:38,990
It can also be run with an argument that hasn't run these tests.

24
00:01:39,000 --> 00:01:45,990
And in Eclipse, so a little bit of a production to get it to do that, to come

25
00:01:46,000 --> 00:01:55,990
over here and give it an argument, say test, and Apply and Run, and then it runs

26
00:01:56,000 --> 00:01:56,990
it in test mode.

27
00:01:57,000 --> 00:02:00,990
And you'll see if I do that from the command line, I just give the word test on

28
00:02:01,000 --> 00:02:03,990
the command line, and it runs all of these tests.

29
00:02:04,000 --> 00:02:06,990
It tests all the numbers and it tests the time.

30
00:02:07,000 --> 00:02:08,990
I'd like to do things this way.

31
00:02:09,000 --> 00:02:12,990
You can also use the unit testing module that comes with Python.

32
00:02:13,000 --> 00:02:15,990
And so I have a lot of results that I could look at.

33
00:02:16,000 --> 00:02:19,990
So the module itself you just write it like you write a normal script.

34
00:02:20,000 --> 00:02:26,990
You write your script and in the process you write whatever classes you're going to write.

35
00:02:27,000 --> 00:02:29,990
And you think about how it will be accessible from the outside world.

36
00:02:30,000 --> 00:02:33,990
And then when it's time to use it as a module you'll notice down here at the

37
00:02:34,000 --> 00:02:37,990
bottom it has this if _name_ pattern, we've seen this before.

38
00:02:38,000 --> 00:02:41,990
And that prevents the main code from being run when it's imported as a module.

39
00:02:42,000 --> 00:02:44,990
So I don't have any code that's outside of a function.

40
00:02:45,000 --> 00:02:48,990
And that's generally true when I write a script any way.

41
00:02:49,000 --> 00:02:50,990
And here is the script on my web site.

42
00:02:51,000 --> 00:02:51,990
It's a very short script.

43
00:02:52,000 --> 00:02:53,990
That's the entire thing.

44
00:02:54,000 --> 00:02:58,990
And all it does is it imports time, because it grabs the time as a localtime

45
00:02:59,000 --> 00:03:03,990
variable, and it also wants to be able to format that time so that it can format

46
00:03:04,000 --> 00:03:05,990
the date and time as well.

47
00:03:06,000 --> 00:03:09,990
And it imports the saytime module and it calls that here.

48
00:03:10,000 --> 00:03:15,990
It says "In Phoenix Arizona, it is now," and it has the saytime words.

49
00:03:16,000 --> 00:03:21,990
And it has the formatted date, on day of the week, and the day, month, and year.

50
00:03:22,000 --> 00:03:26,990
And so when we run that you'll see it has the CGI header because this is

51
00:03:27,000 --> 00:03:30,990
included in a page as a server side include.

52
00:03:31,000 --> 00:03:33,990
And it says, "In Phoenix, Arizona, it is now," and it gives the time, twenty-six

53
00:03:34,000 --> 00:03:37,990
til five, on this date.

54
00:03:38,000 --> 00:03:42,990
And so when you have code that you want to reuse you're going to want to put

55
00:03:43,000 --> 00:03:48,990
it in a module and when you put it in a module it's really just like writing a normal script.

56
00:03:49,000 --> 00:03:51,990
You just need to be careful that you don't have any code that's outside of a

57
00:03:52,000 --> 00:03:55,990
class or a function, and you want to have this pattern down here at the

58
00:03:56,000 --> 00:03:56,990
bottom for running main.

59
00:03:57,000 --> 00:04:01,990
So that you have some way to test it or as I do with this one, to actually

60
00:04:02,000 --> 00:04:03,990
just use it as a script.

61
00:04:04,000 --> 00:04:08,990
Let's go ahead and take that out. It's still got the argument in there.

62
00:04:09,000 --> 00:04:12,990
So that when it's not run with the test argument it just runs it as a script and

63
00:04:13,000 --> 00:04:16,990
it gives the time so you can include it in a shell script or you can use it in a

64
00:04:17,000 --> 00:04:17,990
lot of different ways.

65
00:04:18,000 --> 00:04:20,990
So I'm looking for flexibility when I do this.

66
00:04:21,000 --> 00:04:22,990
But that's essentially all there is to it.

67
00:04:23,000 --> 00:04:26,990
You just put your classes in there, set it up with the pattern at the bottom so

68
00:04:27,000 --> 00:04:29,990
that it only runs main when it's not imported, and so it doesn't do that when

69
00:04:30,000 --> 00:04:34,990
it's imported and then your class becomes available to other scripts.

70
00:04:35,000 --> 00:04:37,990
And you'll see a lot of examples of this as we go through the projects later

71
00:04:38,000 --> 00:04:48,000
on in the course.

