0:00
[MUSIC]
In this video I'm going to show you how to create your own Python modules.
Spoiler alert, you already know how to do this.
We know that a Python module is just an organizational unit of code.
Well, one such organizational unit is a file.
So when you write a Python file, you are actually also creating a module
that you can import into other Python code.
So let's take a look at how we can do this.
So this is a Python file that I wrote that contains a few utility functions
that I might want to be able to use in other programs that I write.
You can take a look at this first function here.
It's called indices and it takes two inputs, an iterable and an element.
And it returns a list of all of the indices at which elem
appears within the iterable.
Well, that sounds like it could be useful.
Then I have a second function here called remove_dups that takes
a list object as an input.
And it creates and returns a new list with exactly the same elements in the input,
except for all of the duplicates have been removed.
Only the first element of each replicated element will appear in the output, and
everything else will be in exactly the same order.
So these two functions sound marginally interesting.
And maybe I would want to use them again.
And so the question is how do I do this?
Well, I'm going to save this file as examples3_utils.py.
So you'll notice that there's nothing in here that will actually run.
So I'm going to try and run the file and nothing happens because no code executes.
There's just these two functions.
So how do I use it in my other programs?
Here is another Python file that I've written that is going to make use
of those utility functions in the utils module that we just looked at.
So you can see, first, I import random.
Well, we understand that.
We've been doing that before where I can import built-in Python modules.
Then I have import examples3_utils.
Well, that's the file we just looked at.
I saved that file as examples3_utils.py, so
the name of the module becomes examples3_utils.
It's the exact same as the file name, but without the .py suffix.
That name is a little bit long, so I want to import it as utils.
So you can see the full statement is import examples3_utils as utils.
This means that for the purposes of this particular file,
I can refer to that model with the name utils.
So I've now written a function called most_ones that takes seq0 and
seq1 as input.
These are two sequences.
And I'm looking for
which of these two sequences has the most number 1s within them.
And it will return 0 if the answer is seq0 and 1 if the answer is seq1.
Now if you look how I've done this, I call utils.indices.
And remember that takes an iterable and then an element that I'm looking for
while I pass seq0 and the number 1.
And then I call it again, passing seq1 and the number of 1.
This will return lists of the indices at which the number 1
appears within these sequences.
And then I can take the length of those lists, and
that will tell me how many 1s are in each sequence.
Now let's ignore the fact that there might be better ways of actually accomplishing
this task.
I want to show you how I can use code within a module that I wrote myself.
And so this should call that indices function that we just looked at.
You can see down here that I'm calling most_ones twice.
One time with the input range(42), so I'd expect 1 to appear once in that sequence.
And then another sequence that's just a list that has a couple of 1s in it.
So I would assume that that's going to have more 1s than range 42.
Then I call it a second time with the list 1 multiplied by 5.
Hey, wait a minute, what does it mean to multiply a list by 5?
Well, I encourage you to actually try this.
But what it does is it replicates the contents of that list 5 times, so
that should be a list with 5 1s in it.
Then I've got another list where I typed some numbers in there, and
I don't believe it has 5 1s.
So let's see what happens.
I run the program, the first one returns 1 because that second sequence
that's an input should have more 1s in it than range 42.
The second time it returns 0 because my list with 5 1s in it does have more 1s
than the alternating 1, 2 list that only has 4 1s in it.
So this does mean that I actually was able to execute utils.indices.
So I ran my indices function from my other Python file, yay!
I created and used the module.
Let's do it again.
Now I have a function called random_unique.
It takes a minnum and maxnum.
These are the minimum value of the numbers I want to appear in my random_unique list.
And maxnum is the maximum value of numbers I want to appear in my random_unique list.
So this returns a list with random values between the inputs with no duplicates.
So I'm just going to generate a random list.
I'm going to use a list comprehension that calls random.randint between minnum and
maxnum, and it just does it 50 times.
Now I have a list with 50 random numbers in it.
And I'm going to call utils.remove_dups to create a new list that gets rid of all
the duplicated values within that list.
I'll return the result.
Again, this may or may not be the best way to accomplish this task.
But the point here is not to accomplish the task in the best way possible, but
rather to show you that I can call functions in my
imported module that I wrote.
And lo and behold, it does seem to work.
I have a list here from the first call where I have all numbers between 0 and 5.
When I generated 50 of them, the probability was pretty high that I was
going to get every number between 0 and 5 at least once.
And it looks like that is the case.
I do have all the numbers.
They're mixed up in order, though, because when they were randomly selected for
the first time, that's where they appear in the output list as per how remove_dups
actually worked.
When I picked random unique numbers between -3 and 14, well,
I get a longer list.
But again, there are no duplicates in the lists and
I leave it to you to confirm that that is actually true.