Time
2 hours 30 minutes
Difficulty
Intermediate
CEU/CPE
2

Video Transcription

00:00
Hello, everyone. And welcome back to Intermediate Python here on side Mary, on demand, I have always and your instructor, Joe Perry And I'm excited to tell you that this is gonna be the last actual lesson of this course.
00:13
The next lesson, Lesson three is going to be our course summary and review, Which means this is gonna be the last lesson in which we introduce new material. And of course, this is gonna be a lesson to of Module Three,
00:24
in which we will be creating a module
00:27
so we only have one learning objective. We only have one task in this final video. Using what we learned in our previous lesson about modules and how they work and how they're imported, we're going to create our own. We're gonna create a custom python module. We're gonna turn that into a python package.
00:42
So let's jump right in or vm and take care of business. So we're here. We're in our bm again, Terminator. Everything's pretty standard. The first thing we want to do is want to create our modules directory. So we're gonna do make a dirt my mob,
00:57
and then I'm gonna see the into it
01:00
first thing I remember that I talked about when we're creating a python package, the first thing, the one thing that you have to have to make it a proper python, that package is the pro is the code
01:11
file theory of the file in it dot pie
01:15
and we don't have to put anything in that We just needed to exist,
01:19
So we're just gonna touch in it dot pie.
01:23
Then we're going to create our actual python module that we're gonna be importing here in just a second and in this case, what we're gonna import or we're gonna create if we're gonna create them and we're gonna use them to create
01:34
my math
01:44
and we're gonna give the shebang line just like usual. And mostly, this is gonna be a standard python program right here. All we're gonna do is we're gonna define that. We're gonna create a few custom functions. Gonna say death. My add a B
01:57
A plus B
02:01
death my sub A b
02:06
return
02:07
a minus B
02:10
deaf. My div a B
02:17
return aid by a B and f my mole maybe
02:23
return
02:24
a times B.
02:27
Now, when we read a normal python for when you write a pilot program that doesn't have any, you know, any module intense or any special intense built in this would pretty much be it, except for the code that actually invokes these functions. So we might do something like that. Mane,
02:45
Uh, my ad
02:46
print my AB
02:52
12
02:53
and then down here, the bottom we would just call May. Now, this is how we would run this program as their This is how we run this code as a normal python program. But I want to create a custom. Want to do something special with it? What we're gonna do here is, instead of doing may actually going to use a special python invocation. And that python invocation is if name
03:13
no. The double underscores
03:15
is equal to
03:16
main.
03:23
Then we're going to call the main function. Now what? This Python indication? What this little trick does is that it actually examines the module itself the python program that this code is founded to determine whether or not it has been important. I talked about that part of the import processes the finder and the other part is loading
03:39
in the loading process for a python module,
03:43
it assigns a name. And generally speaking, that name is gonna be the name of the module. However, if a module is running on its own, if it's just directly in folks than it's going to be named Main. So it's a really useful python shorthand. You'll probably see in a lot of python programs that says, Hey, if this is being invoked by itself, run the main function
04:02
and we can see that here.
04:04
We can see that here just by quickly running
04:09
my math, that pie
04:12
and you can see the little print three, which, of course, is a result of adding one and two.
04:15
So that's our model. That's our package. Now let's prove that it worked correctly. It's true that we actually made it
04:21
go back to our parent directory here,
04:24
and we're going to actually call and invoke it. We're gonna do vim test out pie
04:30
user been Python three
04:33
and then we're going to imports my mod dot
04:39
math. My
04:42
I'll make sure that I have that program named corrects l s my mod. Yep. Sure enough, my math dot pie. So then test a pie
04:51
and then we're really quickly. We're going to invoke
04:56
one of those functions that we custom created. We're going to do that
05:00
with
05:01
my mom, actually, before I do that one thing that I do want a note when you're using packages and you've got these these nested references, something that you're often going to want to do with your import statement
05:12
is import the module as some things with a slightly different name. It just makes it easier to address. So instead of my ma dot mind map, we're going to say, Import my ma dot my math as my math.
05:23
That's just a useful shorthand that let us get around having very, very, very long variable names so important. I'm odd, my mouth as my math. And then we're going to say Prince, my math dot add one
05:35
three.
05:38
And hopefully, if we've done everything correctly when we run this, it's just going to directly run that function and print out the result of adding one in three.
05:48
And it did not work because it has no attributes called Ad because, as I recall, I did not name it ad. I named it something else. I named it my ad
06:01
and let's try this one last time,
06:11
and there we go printed out the number four so you can see the process of creating a module in the process of creating a package is actually much more straightforward than you might suspect.
06:20
It's a pretty simple process in which you just create the python code you want to create and then make it easy to reference. And that import as is a very useful tool. It's something that I use pretty often when I'm importing python modules.
06:31
It's because those names can get pretty extensive and pretty ridiculous.
06:34
So it's gonna be the end of this video and therefore the last piece of material you're gonna learn here. An intermediate python. When you come back for our final video in the series, all we're gonna do is a course summary and review. So thank you for joining me. I have been your instructor, Joe Perry, and you were watching intermediate python on Sai Berry on demand

Up Next

Intermediate Python

This Intermediate Python course uses and builds upon the materials from Intro to Python, using the Python language to accomplish more complex and difficult tasks from building Modules from scratch to constructing and using custom Classes. You’ll learn to make the most of Python’s object orientation, and set yourself up for advanced applications.

Instructed By

Instructor Profile Image
Joe Perry
Senior Technical Instructor at FireEye, Inc
Instructor