Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

5.2 Packages

As your applications grow in size and complexity, it will become more and more important to structure and group your code and modules logically. Groups of modules are referred to as "packages". Let's see how to create and use some basic packages.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


5.2 Packages

Now that you understand the basic concept behind modules. A module, remember is just a really just a singular file that has a particular name. That has the .py suffix on the end of it. And then now, if we're using an import statement, or a from import statement. You can begin to bring in that functionality into your particular module in your application whether its loaded into the symbol table or not. But what happens if you have functionality maybe that is much larger then a single module. Maybe it's gonna make sense for you to break it up into a collection of modules. And really in Python, a collection of modules is known as a package. Now a package is not a complicated concept, it's very similar to a module in itself, it's just a collection of modules. And the way that we do this or the way that we create these packages within Python, is by creating basically a directory structure with an interesting little file built in. So let's go ahead and see how we can build a very basic package. So I'm going to go into my directory structure. And as you can see here, I'm in my Python folder, and I have all of my sample files that we've been working on throughout this course. Now let's say I wanted to create a package. And it can be about anything. It could be a logical grouping of some functionality you're using to build a music app, or a game, or a calculator, or anything like that. Anything you could possibly think of. You can use to structure your application, or your logical grouping of modules into a package in any way that you would like. So let's go ahead and start by creating a very simple folder. So I'm gonna say I want to start by creating a folder. And think of this root folder of your packages kind of exactly that it's going to be that top level. Naming convention, naming convention, or even namespace if you will not name spaces. A little bit different of a term, but you can kind of liken it to this concept of a package with in Python. So let's just say for some reason we were creating a calculator folder. So this is gonna be a calculator package. And within here there's going to be a lot of different functionality. You can have all sorts of things like, let's just say things like addition and subtraction and multiplication and division and complex math. And all sorts of different things like that. But ultimately it's all going to live within a package that has the name of calculator. So now, in order to really define this as a package. We have to do one very specific Python thing. So let's go into our calculator folder, which is ultimately going to be the root of our package. And the way that we define this as actually being a package is by sticking a file in here with a very specific name. So let's go ahead and come in here and create a new file. So we'll create a new file. And what's interesting about this is the name of this file is going to be, __init__.py. Now, this very specifically named file is what Python is going to use to initialize this particular folder, the calculator folder as being a package. So if Python sees this, it's going to know that I see a package here. And it's going to allow us to use what's known as the dot notation. To be able to reference different hierarchy levels Of not only our package, but also ultimately, get down into our defined modules. So, now that we've done that, let's just say we wanted to do a couple different types of calculators. We wanted maybe a simple calculator and a complex calculator, or advanced, or what have you. So let's go ahead and just create a new folder here. So we'll create a new folder we'll call this simple. And let's say we'll create another new folder and we'll call this complex. So now, all we're doing at this point is we're breaking down this package into different logical pieces of functionality. Where we're going to have a lot of this Logic for our calculator in this case, or our calculator functionality to live. Now once again, if we want these two folders to basically be sub-levels of our package or be actually sub-packages themselves. We're gonna need to have this same file in both. So I'll go ahead and copy this and paste it into both, like that. And then let's just say within simple let's create a very simple file here. No pun intended. And we'll just call this simple.py like that. Yes. And we'll go ahead and copy this and we'll navigate up a level into complex. And we will paste that here. But we'll change this to complex like that. Well actually to make this a little bit simpler to understand, we'll change the name to complex calc. And then we'll just go back into simple and we'll just call this simple calc like that. So now, if you want to kind of investigate our structure here we have our root level package, which is called calculator. And the reason that this is being considered a package is because of this the existence of this file right here. And then we have two sub packages here we have complex and simple and within here these are also considered packages. Once again, because of the init file that exists. And then we have a complex calc within complex and within simple, we have a simple calc. Now that's the basic structure of what we have going on here. So, let's just go ahead in here and we're gonna edit these files and I'll simply put in here a single function that will be called compute. Once again we're not gonna worry about any sort of crazy logic here. We'll just say, this is simple and we'll save that. And then we'll come back out to complex and we will edit this one. And we'll say compute, is going to print and said, boy this is really complex. Very, very complicated logic here. So then we'll save that. So now we have our two modules are simple_calc and our complex_calc that are living within our calculator package in the complex and simple packages. Now how do we get at these things how do we get access to these modules that are found within a package. And getting packages like this is actually very common in Python. So this is the way that we're gonna basically bundle up our functionality and send it around. Probably zip up the contents of our folder into a single file that we can pass around and other people can ultimately use our package. So now let's go ahead and enter into our interpreter. So now let's say at this point I wanted to get at some functionality that existed within our package. Now our package remember, our name is calculator. And we have two sub packages is called simple and complex. So let's say at this point I wanted to import a specific calculator. So let's say I wanted to import my simple calculator. So I could get access to its functionality. I could do an import statement, and I would use the fully qualified .notation version within our package to where I want to get to to refer to this specific module, the simple calc module. So I would say calculator.simple.simple_calc. Now by doing that I'm going to import my simple calc module. But if you recall when we started to do just a straight import statement before when we dealt with a module. We had to refer to that actual module. So let's see if we can do that here. So we'll say is simplecalc.compute. And we'll see if we can run this and its not working. And why is that? Well once again, just a straight import statement is going to work. It's going to get that functionality into our symbol table, but unfortunately, we're going to have to refer to it, basically, by its fully qualified name. So, if I wanted to use that function, I'm gonna have to say calculator. .simple.simple_calc.compute. Like so. Now, yes, the function itself is returning that this is simple but this is not simple. So depending on how you are doing your application, how you're structuring it and what you're ultimately trying to do. This will absolutely work. And you'll just have to refer to all the different layers found within the package to ultimately get down to the function or functionality that you're trying to use. Now there is of course a way around that. And we can use a slightly modified from important version of our import statement. So I could say from. And once again define the full path down to where it is I want to go. And I have a couple different options here I could say from calculator.simple, calculator. I could bring in all the functionality that's found within my simple package. So I could say from calculator import simple like that. So now at this point I would have all the functionality within my simple package. But unfortunately at this point all I have is my simple calc module. But once again, I'm not getting everything within my simple package loaded. So I would still have to say simple.simple_calc.compute, like that to get it to work. Or I could take it a step further, and take it all the way down to exactly what it is I want to import. So I could say from Calcuator.simple. And I could even take it a layer deeper and I could say simple underscore calc at this point all I want to import is compute. And then of course at this point I could just say, compute. Now this will absolutely work too. Now the one thing that you have to kind of watch here is that depending on the different naming conventions that you find within the different packages you're using and importing within your modules and within your application. The naming conventions can get quite difficult to maintain. And so, one thing I would advise you to do is don't take it all the way down to something like this. Down to compute, unless you know exactly where its coming from. So that you don't get confused, because if I were to now import another version here and get all the way down to the complex version of compute. Now we're really starting to get a little bit confusing. So I would only go as deep as you need to go, and I would say maybe only take it down to the most logical sub package that you can. So, in this case, may be simple and complex and just refer to things that way. Just to kind of keep things a little bit more organized and easier for you ultimately, to understand. So, this is the way that you're going to find a lot of applications structured within Python by using these modules and packages. And if you've ever done any sort of development using something called Django, which is a web development framework written for Python. This is exactly how things are structured all the way up and down using a number of different packages and different naming conventions and things like that. So this is a very common thing that you're gonna see once you start looking at other people's code and other people's applications within Python. So it's definitely a very good thing for you to begin to understand.

Back to the top