WEBVTT

0
00:00.300 --> 00:03.780
In the last lesson, we added an external library to our project.

1
00:04.230 --> 00:09.060
Now it's time to construct a prettytable object and practice using its methods

2
00:09.060 --> 00:12.090
and attributes. Our starting point as always

3
00:12.120 --> 00:14.190
is the documentation. Here

4
00:14.190 --> 00:17.580
you can see how to construct an actual prettytable object.

5
00:18.420 --> 00:22.950
So let's use what we've learned previously about constructing objects to go

6
00:22.950 --> 00:27.360
ahead and create this object from the class. Now, firstly,

7
00:27.360 --> 00:30.600
we're going to do the same thing as we did previously with turtle.

8
00:31.110 --> 00:33.630
We're going to tap into the package prettytable,

9
00:34.050 --> 00:39.050
and then we're going to import the prettytable class noted by the c here.

10
00:41.340 --> 00:44.640
Now that we have access to this class inside our code,

11
00:45.030 --> 00:50.030
I want you to go ahead and create an object from this pretty table class and the

12
00:50.370 --> 00:54.780
object should be named table. So pause the video and give that a go.

13
00:57.120 --> 00:59.790
All right. So we said I'll object is going to be called table,

14
01:00.210 --> 01:04.320
and then we're going to construct it from this class called a prettytable.

15
01:05.070 --> 01:07.770
So we're going to put it in with its casing.

16
01:08.130 --> 01:11.160
So this is the Pascal case that we spoke about earlier on,

17
01:11.640 --> 01:15.060
and then in order to actually give it the go-ahead and construct,

18
01:15.420 --> 01:18.090
we have to add the parentheses at the end.

19
01:18.630 --> 01:23.630
So now you've created a new object called table from this prettytable and we

20
01:24.960 --> 01:28.770
can actually go ahead and print this table. And even at this stage,

21
01:28.800 --> 01:33.450
it's ready going to start printing out a bare bones version all the table.

22
01:33.810 --> 01:36.060
But of course this table has no datas

23
01:36.180 --> 01:39.960
So it's actually kind of hard to visualize what all of these ASCII characters

24
01:39.960 --> 01:41.610
are doing in an empty table.

25
01:42.390 --> 01:47.390
But no matter, the next thing we can see in our documentation is how to go about

26
01:48.420 --> 01:52.110
adding columns. So it's got this method, remember methods

27
01:52.110 --> 01:57.110
are functions that are associated with an object and this method will add columns to

28
01:57.930 --> 02:02.670
whatever table we specify. Now the method takes two inputs.

29
02:02.970 --> 02:05.220
One is the name of the field,

30
02:05.820 --> 02:09.270
so our field names are Pokemon name and type.

31
02:10.350 --> 02:13.320
And then we've got a list of strings

32
02:13.620 --> 02:18.420
which is going to be the data that's going to go into that column. Essentially,

33
02:18.510 --> 02:21.690
every time we call that method, add column,

34
02:21.990 --> 02:25.740
it's going to allow us to give a field name as a string,

35
02:26.070 --> 02:29.970
and then a list of data in order that they're going to go into the table.

36
02:30.450 --> 02:35.450
So we're going to add our table one column at a time. Using that documentation

37
02:36.600 --> 02:41.600
see if you can add both of these columns to our table object and then once

38
02:43.080 --> 02:46.410
you've done that, we're going to print the table object again

39
02:46.740 --> 02:51.270
and we should be to see an ASCII table. This is what you're aiming for

40
02:51.270 --> 02:52.470
when you run your code.

41
02:52.830 --> 02:57.810
It should print out your table and nicely format it in this ASCII style.

42
02:58.200 --> 03:00.460
Pause the video and complete the challenge.

43
03:04.110 --> 03:04.380
<v 1>Right?</v>

44
03:04.380 --> 03:04.650
<v 0>All right.</v>

45
03:04.650 --> 03:09.510
So we've got our table object and remember that methods are functions that are

46
03:09.510 --> 03:14.190
associated with the object. So we say table object dot,

47
03:14.310 --> 03:16.140
and then we get to call that method,

48
03:16.530 --> 03:21.090
add_column. And notice how it takes two inputs,

49
03:21.120 --> 03:24.240
the field name and the data that's going to go into the column.

50
03:24.840 --> 03:26.700
So the field name is going to be a string,

51
03:27.000 --> 03:31.350
and this is going to be the name at the top of our column. So in our case,

52
03:31.350 --> 03:35.490
it was called Pokemon name. And then after a comma,

53
03:35.490 --> 03:40.380
we get to put in a list of all the data that's going to go into our column.

54
03:41.280 --> 03:44.880
So just as a quick reminder, our first column contains Pikachu,

55
03:44.880 --> 03:46.140
Squirtle and Charmander.

56
03:46.140 --> 03:51.140
Now, we should have added a column to our table and you can safely ignore all

57
03:53.340 --> 03:57.690
of these typos because of course, Pokemon names are not real words.

58
03:58.020 --> 04:01.200
But if you wanted to check against the poke dex

59
04:01.380 --> 04:04.110
you can actually go to this link in the course resources,

60
04:04.440 --> 04:08.490
and you can see how each of these Pokemon are spelt. Now,

61
04:08.700 --> 04:12.870
the next thing we want to add is the associated type with each of these

62
04:12.870 --> 04:17.130
Pokemons. So for example, Charmander is a fire type of Pokemon,

63
04:17.160 --> 04:19.230
Squirtle is a water type Pokemon,

64
04:19.710 --> 04:23.430
but essentially we're going to add the data that's in the second column.

65
04:23.880 --> 04:27.270
So the field name is called type, and then we've got three pieces of data

66
04:27.570 --> 04:32.430
that's going to need to go into the list in the same order as the previous

67
04:32.430 --> 04:35.370
Pokemon. That way we'll actually match them up properly.

68
04:37.200 --> 04:40.110
So if you haven't already, go ahead and add the second column.

69
04:40.920 --> 04:41.753
<v 1>Right?</v>

70
04:44.460 --> 04:45.090
<v 0>So again,</v>

71
04:45.090 --> 04:50.090
I'm going to call the same method on my table and this time I'm going to add the

72
04:50.310 --> 04:55.310
field type and the data is going to go in the order of the data that I had from

73
04:56.670 --> 05:01.440
the previous column. So the first one is the type of Pikachu which is electric.

74
05:02.010 --> 05:06.900
And the second one is the type for Squirtle, which is Water. And finally,

75
05:06.930 --> 05:08.880
we've got fire for Charmander.

76
05:09.480 --> 05:12.210
So now this will be matched to this,

77
05:12.300 --> 05:14.730
this will be matched this and so on and so forth.

78
05:15.210 --> 05:17.700
If we go ahead and print our table now, so

79
05:17.820 --> 05:20.820
if we run our code and take a look in here,

80
05:20.850 --> 05:25.850
you can see how we've now got a nicely formatted table by creating an object

81
05:27.870 --> 05:32.640
from this pre-made prettytable class and we've now called this method

82
05:32.730 --> 05:36.810
add_column to add two columns. And when we print our table,

83
05:36.870 --> 05:41.160
it's now nicely formatted in ASCII. Now,

84
05:41.190 --> 05:44.880
remember that we can also change the object's attributes.

85
05:45.900 --> 05:48.780
For example, if we wanted to change the appearance of our table,

86
05:49.170 --> 05:53.580
that's probably controlled by an attribute. If you scroll down,

87
05:53.640 --> 05:58.640
you can see that we can change the table style by tapping into each of these attributes.

88
05:59.600 --> 06:03.710
For example, if I wanted to change the alignment of my data in the table,

89
06:04.040 --> 06:09.040
I can change this align attribute to L for left-align, C for the center-align or

90
06:10.550 --> 06:11.900
R for right-aligned.

91
06:13.010 --> 06:17.750
Do you remember how to tap into an object's attribute? If you do,

92
06:18.110 --> 06:23.110
go ahead and see if you can change our table from center-aligned by default to

93
06:23.600 --> 06:27.650
left-aligned. Pause the video and try to complete that challenge.

94
06:28.180 --> 06:29.013
<v 1>Right?</v>

95
06:30.340 --> 06:31.090
<v 0>So again,</v>

96
06:31.090 --> 06:36.090
we're going to tap into our table object and we're going to use the dot

97
06:36.250 --> 06:38.290
notation, but this time

98
06:38.320 --> 06:42.280
instead of accessing a method associated with the object

99
06:42.550 --> 06:47.110
which is of course denoted by the M, we're going to access an attribute or in

100
06:47.110 --> 06:50.140
this case, they've titled F which is a field.

101
06:50.710 --> 06:55.510
And the one that we want is this align. So let's go ahead and type align,

102
06:55.960 --> 07:00.340
and now we have access to this attribute. Let's see what happens

103
07:00.340 --> 07:03.340
if I go ahead and print this align attribute.

104
07:04.270 --> 07:08.770
You can see that for both of my columns, Pokemon name and type,

105
07:09.100 --> 07:12.370
the align attribute is set to C, which is centered.

106
07:13.240 --> 07:18.240
What I want to do is change the whole table to be left-aligned. Just as we would

107
07:18.730 --> 07:21.760
with any other variable we can of course print it,

108
07:22.090 --> 07:23.800
but we can also change it.

109
07:24.280 --> 07:28.090
And we would change a variable by just using the equal sign, right?

110
07:28.390 --> 07:31.330
So let's change the alignment to left-aligned,

111
07:31.810 --> 07:33.490
and now let's run the code again.

112
07:34.570 --> 07:39.570
And you can see now both of my columns are now aligning with the left hand

113
07:40.150 --> 07:40.983
margin.

114
07:42.700 --> 07:45.580
So we've seen how we can use attributes

115
07:45.640 --> 07:49.480
like the ones here to change the styling of our table.

116
07:49.750 --> 07:52.300
We've seen how we can use methods

117
07:52.510 --> 07:57.510
like add_column or add_row to work with this table object and get it to

118
07:58.810 --> 08:02.680
perform some sort of functionality, like adding pieces of data to it.

119
08:03.130 --> 08:08.130
And we've seen how we can create new objects by simply constructing it from the

120
08:08.350 --> 08:13.000
blueprint class. Feel free to mess around with prettytable

121
08:13.240 --> 08:15.940
as much as you like change, other things about it,

122
08:16.000 --> 08:17.770
or add different pieces of data.

123
08:18.130 --> 08:23.130
But once you're happy with creating the object changing the attributes and

124
08:23.770 --> 08:25.150
calling the methods,

125
08:25.510 --> 08:28.900
then head over to the next lesson where I've got a quiz for you.