Now I've mentioned a lot about using code that other developers have written and
the most common way to do this is through using packages of code.
In this lesson,
I'll show you how to integrate an existing package of code into our project so
that we can practice creating objects and working with attributes and methods.
Now we've seen modules of code where each file that we create in our project is
essentially a module in itself.
But a package differs from a module in the sense that it's actually a whole
bunch of code that other people have written, lots and lots of files all packaged
together to achieve some sort of goal or purpose. Now,
let's say that we wanted to create a table of Pokemon and their type.
So we wanted some way of documenting that Pikachu is an electric type and maybe
Squirtle is a water type. Now,
if we wanted to do that in ASCII so that we could print it in the console, well
then we would have to go about formatting it and it'll be quite painful.
So we might have to print some sort of pipe and then maybe we would have the
name of the field, so Pokemon name,
and then maybe another pipe. And then we would have the name of the next column,
which is their type.
And then finally we would close that off and then we would have to print some
sort of a horizontal line
maybe like this and so on and so forth just to create this table.
And it's quite difficult if you're not used to manipulating ASCII and
visualizing it. I certainly am not up to this task.
So what could we do instead? Well,
we could search for a package that other developers have created to achieve this
goal. And the place that we would do that is somewhere called PyPi,
which is the Python package index.
And this is a bunch of software for the Python programming language that is
developed and shared by the Python community. So we can see their source code,
but more importantly,
we can see how to implement the code and how to put it into our project.
In here, I'm going to search for something called prettytable.
And this is a very simple library that is going to help us to display tables in
a ASCII format.
We can click on the project links to find out more about it.
And this will take us to the Google code archive,
where the documentation is hosted.
If you go to Wikis and go to tutorial,
you'll see some documentation on how to use it.
Now, this particular package is not as extensive as the turtle graphics package.
So there's actually not a lot of things you can do with it,
but its got more than we need for our use case.
The first thing we have to do is to install this package into our project.
Now, unlike the turtle package
which is already preloaded with every copy of Python, in order to use the
packages that you find in the Python package index,
you actually have to install it.
So here's how we do it in PyCharm. First, go to the preferences,
so in Windows, I think it's under file and then preferences. On Mac is under
PyCharm and then preferences.
And then here you'll see a whole bunch of things you can change
including the editor or appearance which we've already messed around with.
But I want you to click on your project.
So my project is called day-16-start and then we're going to go into the project
interpreter. And here we can click on the plus button to install any package that
you find in the Python package index. Make a note of what it's called.
So ours is called PrettyTable and then we're going to search for it inside the
available packages. And once we found it,
we're going to select it and then click install.
If you are working with Repl.it,
you can also install packages just by going to the packages tab
and then again, searching for PrettyTable. Once you click on the package
that you want, click on the plus button to add it to your project.
Now Repl.it is a little bit weird because it says installing
but it never actually confirms the success. But once you click run,
everything will go through and it will work perfectly
as long as you see it in your list of packages here. Now,
if you want to remove the package, you can simply click on the minus button,
but now we're going to work with it.
So now it's installed successfully,
we can go ahead and close this down and click okay.
And we can now access this package called PrettyTable in our code.
Again,
I'm going to import it so I can say import prettytable.
And now I can use everything that's inside this package. Now,
if you want to see the source code,
you can actually right-click on it and select, go to, implementation.
And this will take you to the Python file where this entire prettytable is
implemented, so all of the code that they've written.
It's a little bit daunting and it's got a lot of code in it,
but we don't have to worry about how the code is created and how it works.
All we need to do is look at the documentation for this package and see how we
can implement it into our project. Now in the next lesson,
we're going to get some practice
constructing a prettytable object and using the documentation to explore this
object's attributes and methods. For all of that and more, I'll see you there.