1
00:00:00,000 --> 00:00:03,990
The Python standard library has a number of modules that you can import into

2
00:00:04,000 --> 00:00:06,990
your programs and use for your own purposes.

3
00:00:07,000 --> 00:00:12,990
This page has a list of them starting here in section 7, under String Services.

4
00:00:13,000 --> 00:00:15,990
And you'll see that there are quite a few of them.

5
00:00:16,000 --> 00:00:19,990
So we're going to just go through a few of these, a few of the more common

6
00:00:20,000 --> 00:00:22,990
ones and show you as an example how you can use them.

7
00:00:23,000 --> 00:00:29,990
And I suggest that you go through this list at least once and just look out what they are.

8
00:00:30,000 --> 00:00:30,990
And just look at their descriptions.

9
00:00:31,000 --> 00:00:33,990
Just read through the list, so you have an idea of the kinds of things that

10
00:00:34,000 --> 00:00:35,990
are available to you.

11
00:00:36,000 --> 00:00:40,990
So here in Eclipse, we're going to make a working copy of modules.py.

12
00:00:41,000 --> 00:00:46,990
Call this modules-working.py, and we'll open that up.

13
00:00:47,000 --> 00:00:52,990
You'll notice that here we're importing sys, and here on the web page you'll see

14
00:00:53,000 --> 00:00:57,990
sys is for system-specific parameters and functions.

15
00:00:58,000 --> 00:01:03,990
If we open that page, you'll see that there's full documentation on all of the

16
00:01:04,000 --> 00:01:05,990
things that you can do with sys.

17
00:01:06,000 --> 00:01:08,990
So, one of these is to get the Python version.

18
00:01:09,000 --> 00:01:10,990
This is with sys version info.

19
00:01:11,000 --> 00:01:19,990
And so if we run this, you'll see that we say, this is Python version 3.1.2.

20
00:01:20,000 --> 00:01:22,990
You can also get the system platform.

21
00:01:23,000 --> 00:01:30,990
You can say print(sys.platform) and if we save that and run it, you'll see

22
00:01:31,000 --> 00:01:32,990
that this is win32.

23
00:01:33,000 --> 00:01:38,990
On my Mac at home it says Darwin and whatever operating system you're running Python on,

24
00:01:39,000 --> 00:01:40,990
it will give you an idea.

25
00:01:41,000 --> 00:01:42,990
And this is a category of operating system.

26
00:01:43,000 --> 00:01:44,990
It's not actually the operating system.

27
00:01:45,000 --> 00:01:48,990
You can get the operating system from the OS module, but this gives you an

28
00:01:49,000 --> 00:01:53,990
indicator of what types of services are going to be available on this platform.

29
00:01:54,000 --> 00:01:57,990
Like, for instance, if I'm on a win32 platform, I'm not going to do things that

30
00:01:58,000 --> 00:02:00,990
are UNIX specific and vise-versa.

31
00:02:01,000 --> 00:02:03,990
So, that can be a useful token to see.

32
00:02:04,000 --> 00:02:09,990
If we import OS, this is another module that you'll find in that list,

33
00:02:10,000 --> 00:02:13,990
you'll notice that I can import inside of the function.

34
00:02:14,000 --> 00:02:15,990
I don't need to do my import at the top.

35
00:02:16,000 --> 00:02:19,990
This allows me to actually selectively import things, say I read that

36
00:02:20,000 --> 00:02:24,990
sys.platform variable and say I'm going to do certain things one way or

37
00:02:25,000 --> 00:02:27,990
certain things another way depending on which platform I get.

38
00:02:28,000 --> 00:02:33,990
So I can import modules selectively, and the system won't try to import them if

39
00:02:34,000 --> 00:02:36,990
they're not on the right platform or whatever.

40
00:02:37,000 --> 00:02:44,990
So, here we can get the os.name for example, and if I save that and run it,

41
00:02:45,000 --> 00:02:47,990
you'll see this says nt.

42
00:02:48,000 --> 00:02:50,990
So, that's the name that it's giving this particular type of Windows.

43
00:02:51,000 --> 00:02:55,990
And it distinguishes it like from, for example the older Windows 3 or

44
00:02:56,000 --> 00:02:58,990
something like that.

45
00:02:59,000 --> 00:02:59,990
I can get variables from the environment.

46
00:03:00,000 --> 00:03:05,990
I can print os.getenv and say I want the PATH variable.

47
00:03:06,000 --> 00:03:10,990
Save and run.

48
00:03:11,000 --> 00:03:15,990
And there is the PATH variable from the operating system.

49
00:03:16,000 --> 00:03:24,990
So, I can get current working directory, and we'll save and run that.

50
00:03:25,000 --> 00:03:26,990
And there is the current working directory.

51
00:03:27,000 --> 00:03:31,990
Of course, this module has a lot of functions in it.

52
00:03:32,000 --> 00:03:34,990
Just one more I want to show is the urandom function.

53
00:03:35,000 --> 00:03:38,990
This is sometimes useful.

54
00:03:39,000 --> 00:03:41,990
Save and run that.

55
00:03:42,000 --> 00:03:44,990
This is a function that'll give a string of random bytes.

56
00:03:45,000 --> 00:03:54,990
And of course you see that this is the byte type, and I set 25 so it's 25 bytes long.

57
00:03:55,000 --> 00:04:01,990
Another useful module is the urllib module, and we'll go ahead and import from

58
00:04:02,000 --> 00:04:10,990
that urllib.request, and we'll grab a web page from the internet.

59
00:04:11,000 --> 00:04:19,990
urllib.request.urlopen, and we'll give it a URL here.

60
00:04:20,000 --> 00:04:25,990
We'll go ahead and get my homepage, print(page). There we go.

61
00:04:26,000 --> 00:04:28,990
Save that and run it.

62
00:04:29,000 --> 00:04:30,990
We get this object.

63
00:04:31,000 --> 00:04:32,990
And that's an iterable object.

64
00:04:33,000 --> 00:04:38,990
So, we can iterate on it and say for line in page:

65
00:04:39,000 --> 00:04:46,990
print, and convert each line to a string because they come out binary, and

66
00:04:47,000 --> 00:04:49,990
we provide an encoding.

67
00:04:50,000 --> 00:04:51,990
And they have no lines at the end of them.

68
00:04:52,000 --> 00:04:55,990
So we'll give it this line ending there.

69
00:04:56,000 --> 00:05:06,990
And we save and run and there is an entire web page.

70
00:05:07,000 --> 00:05:08,990
So, that's a really useful one.

71
00:05:09,000 --> 00:05:12,990
There are a lot of very useful modules in here. I'm just going to take a quick look

72
00:05:13,000 --> 00:05:13,990
at a couple of more.

73
00:05:14,000 --> 00:05:16,990
One is the random module.

74
00:05:17,000 --> 00:05:18,990
It's a very rich random number library.

75
00:05:19,000 --> 00:05:32,990
For example, you can print a random number from in a range. randint,

76
00:05:33,000 --> 00:05:36,990
A range between 1 and 1000. Save and run.

77
00:05:37,000 --> 00:05:37,990
And then we have the number 726.

78
00:05:38,000 --> 00:05:41,990
If we run it again, we get a different number and a different number and

79
00:05:42,000 --> 00:05:42,990
a different number.

80
00:05:43,000 --> 00:05:44,990
And that's very useful.

81
00:05:45,000 --> 00:05:51,990
Another very useful method in this library is the shuffle method. It takes a list.

82
00:05:52,000 --> 00:05:56,990
I'm going to just give it a list with a range in it, and if we print that list,

83
00:05:57,000 --> 00:06:02,990
you'll see that we have a range of 25 numbers.

84
00:06:03,000 --> 00:06:15,990
We can shuffle the list, random.shuffle, and print it again.

85
00:06:16,000 --> 00:06:19,990
And there we have the list shuffled randomly.

86
00:06:20,000 --> 00:06:25,990
If we do that a few times, save and run, you can see we get different

87
00:06:26,000 --> 00:06:28,990
shuffles each time.

88
00:06:29,000 --> 00:06:33,990
Finally, in our little partial tour of the standard library, look at

89
00:06:34,000 --> 00:06:46,990
the datetime module.

90
00:06:47,000 --> 00:06:53,990
So we'll save that and run it, and there we have the time as of right now

91
00:06:54,000 --> 00:06:54,990
when I'm recording this.

92
00:06:55,000 --> 00:07:12,990
In fact, we can do this. now.year, now.month, now.day, now.hour, now.minute,

93
00:07:13,000 --> 00:07:20,990
now.second and now.microsecond, and save that and run it.

94
00:07:21,000 --> 00:07:24,990
And there we have all of those components, separately usable with these

95
00:07:25,000 --> 00:07:26,990
properties of the datetime object.

96
00:07:27,000 --> 00:07:30,990
So, these are just a few of the standard modules that are available with Python.

97
00:07:31,000 --> 00:07:34,990
Again I strongly recommend that you look through the documentation for these

98
00:07:35,000 --> 00:07:38,990
standard modules. At least read their descriptions so that you get an idea of

99
00:07:39,000 --> 00:07:42,990
what's available there, so that as you're going through writing your own code,

100
00:07:43,000 --> 00:07:44,990
you're not tempted to reinvent the wheel.

101
00:07:45,000 --> 00:07:48,990
The modules that are included with Python and the Python distribution tend to

102
00:07:49,000 --> 00:07:49,990
be very well written.

103
00:07:50,000 --> 00:07:55,990
They tend to be very feature rich and they tend to be quite well optimized and reliable.

104
00:07:56,000 --> 00:08:06,000
So, I suggest that you use them when you have an opportunity.

