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 want 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 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 an 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 it's 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 pre-loaded 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, it's 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.
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 Pretty Table 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.