In the last lesson, we added an external library to our project.
Now it's time to construct a prettytable object and practice using its methods
and attributes. Our starting point as always
is the documentation. Here
you can see how to construct an actual prettytable object.
So let's use what we've learned previously about constructing objects to go
ahead and create this object from the class. Now, firstly,
we're going to do the same thing as we did previously with turtle.
We're going to tap into the package prettytable,
and then we're going to import the prettytable class noted by the c here.
Now that we have access to this class inside our code,
I want you to go ahead and create an object from this pretty table class and the
object should be named table. So pause the video and give that a go.
All right. So we said I'll object is going to be called table,
and then we're going to construct it from this class called a prettytable.
So we're going to put it in with its casing.
So this is the Pascal case that we spoke about earlier on,
and then in order to actually give it the go-ahead and construct,
we have to add the parentheses at the end.
So now you've created a new object called table from this prettytable and we
can actually go ahead and print this table. And even at this stage,
it's ready going to start printing out a bare bones version all the table.
But of course this table has no datas
So it's actually kind of hard to visualize what all of these ASCII characters
are doing in an empty table.
But no matter, the next thing we can see in our documentation is how to go about
adding columns. So it's got this method, remember methods
are functions that are associated with an object and this method will add columns to
whatever table we specify. Now the method takes two inputs.
One is the name of the field,
so our field names are Pokemon name and type.
And then we've got a list of strings
which is going to be the data that's going to go into that column. Essentially,
every time we call that method, add column,
it's going to allow us to give a field name as a string,
and then a list of data in order that they're going to go into the table.
So we're going to add our table one column at a time. Using that documentation
see if you can add both of these columns to our table object and then once
you've done that, we're going to print the table object again
and we should be to see an ASCII table. This is what you're aiming for
when you run your code.
It should print out your table and nicely format it in this ASCII style.
Pause the video and complete the challenge.
Right?
All right.
So we've got our table object and remember that methods are functions that are
associated with the object. So we say table object dot,
and then we get to call that method,
add_column. And notice how it takes two inputs,
the field name and the data that's going to go into the column.
So the field name is going to be a string,
and this is going to be the name at the top of our column. So in our case,
it was called Pokemon name. And then after a comma,
we get to put in a list of all the data that's going to go into our column.
So just as a quick reminder, our first column contains Pikachu,
Squirtle and Charmander.
Now, we should have added a column to our table and you can safely ignore all
of these typos because of course, Pokemon names are not real words.
But if you wanted to check against the poke dex
you can actually go to this link in the course resources,
and you can see how each of these Pokemon are spelt. Now,
the next thing we want to add is the associated type with each of these
Pokemons. So for example, Charmander is a fire type of Pokemon,
Squirtle is a water type Pokemon,
but essentially we're going to add the data that's in the second column.
So the field name is called type, and then we've got three pieces of data
that's going to need to go into the list in the same order as the previous
Pokemon. That way we'll actually match them up properly.
So if you haven't already, go ahead and add the second column.
Right?
So again,
I'm going to call the same method on my table and this time I'm going to add the
field type and the data is going to go in the order of the data that I had from
the previous column. So the first one is the type of Pikachu which is electric.
And the second one is the type for Squirtle, which is Water. And finally,
we've got fire for Charmander.
So now this will be matched to this,
this will be matched this and so on and so forth.
If we go ahead and print our table now, so
if we run our code and take a look in here,
you can see how we've now got a nicely formatted table by creating an object
from this pre-made prettytable class and we've now called this method
add_column to add two columns. And when we print our table,
it's now nicely formatted in ASCII. Now,
remember that we can also change the object's attributes.
For example, if we wanted to change the appearance of our table,
that's probably controlled by an attribute. If you scroll down,
you can see that we can change the table style by tapping into each of these attributes.
For example, if I wanted to change the alignment of my data in the table,
I can change this align attribute to L for left-align, C for the center-align or
R for right-aligned.
Do you remember how to tap into an object's attribute? If you do,
go ahead and see if you can change our table from center-aligned by default to
left-aligned. Pause the video and try to complete that challenge.
Right?
So again,
we're going to tap into our table object and we're going to use the dot
notation, but this time
instead of accessing a method associated with the object
which is of course denoted by the M, we're going to access an attribute or in
this case, they've titled F which is a field.
And the one that we want is this align. So let's go ahead and type align,
and now we have access to this attribute. Let's see what happens
if I go ahead and print this align attribute.
You can see that for both of my columns, Pokemon name and type,
the align attribute is set to C, which is centered.
What I want to do is change the whole table to be left-aligned. Just as we would
with any other variable we can of course print it,
but we can also change it.
And we would change a variable by just using the equal sign, right?
So let's change the alignment to left-aligned,
and now let's run the code again.
And you can see now both of my columns are now aligning with the left hand
margin.
So we've seen how we can use attributes
like the ones here to change the styling of our table.
We've seen how we can use methods
like add_column or add_row to work with this table object and get it to
perform some sort of functionality, like adding pieces of data to it.
And we've seen how we can create new objects by simply constructing it from the
blueprint class. Feel free to mess around with prettytable
as much as you like change, other things about it,
or add different pieces of data.
But once you're happy with creating the object changing the attributes and
calling the methods,
then head over to the next lesson where I've got a quiz for you.