1
00:00:00,300 --> 00:00:03,780
In the last lesson, we added an external library to our project.

2
00:00:04,230 --> 00:00:09,060
Now it's time to construct a prettytable object and practice using its methods

3
00:00:09,060 --> 00:00:12,090
and attributes. Our starting point as always

4
00:00:12,120 --> 00:00:14,190
is the documentation. Here

5
00:00:14,190 --> 00:00:17,580
you can see how to construct an actual prettytable object.

6
00:00:18,420 --> 00:00:22,950
So let's use what we've learned previously about constructing objects to go

7
00:00:22,950 --> 00:00:27,360
ahead and create this object from the class. Now, firstly,

8
00:00:27,360 --> 00:00:30,600
we're going to do the same thing as we did previously with turtle.

9
00:00:31,110 --> 00:00:33,630
We're going to tap into the package prettytable,

10
00:00:34,050 --> 00:00:39,050
and then we're going to import the prettytable class noted by the c here.

11
00:00:41,340 --> 00:00:44,640
Now that we have access to this class inside our code,

12
00:00:45,030 --> 00:00:50,030
I want you to go ahead and create an object from this pretty table class and the

13
00:00:50,370 --> 00:00:54,780
object should be named table. So pause the video and give that a go.

14
00:00:57,120 --> 00:00:59,790
All right. So we said I'll object is going to be called table,

15
00:01:00,210 --> 00:01:04,319
and then we're going to construct it from this class called a prettytable.

16
00:01:05,069 --> 00:01:07,770
So we're going to put it in with its casing.

17
00:01:08,130 --> 00:01:11,160
So this is the Pascal case that we spoke about earlier on,

18
00:01:11,640 --> 00:01:15,060
and then in order to actually give it the go-ahead and construct,

19
00:01:15,420 --> 00:01:18,090
we have to add the parentheses at the end.

20
00:01:18,630 --> 00:01:23,630
So now you've created a new object called table from this prettytable and we

21
00:01:24,960 --> 00:01:28,770
can actually go ahead and print this table. And even at this stage,

22
00:01:28,800 --> 00:01:33,450
it's ready going to start printing out a bare bones version all the table.

23
00:01:33,810 --> 00:01:36,060
But of course this table has no datas

24
00:01:36,180 --> 00:01:39,960
So it's actually kind of hard to visualize what all of these ASCII characters

25
00:01:39,960 --> 00:01:41,610
are doing in an empty table.

26
00:01:42,390 --> 00:01:47,390
But no matter, the next thing we can see in our documentation is how to go about

27
00:01:48,420 --> 00:01:52,110
adding columns. So it's got this method, remember methods

28
00:01:52,110 --> 00:01:57,110
are functions that are associated with an object and this method will add columns to

29
00:01:57,930 --> 00:02:02,670
whatever table we specify. Now the method takes two inputs.

30
00:02:02,970 --> 00:02:05,220
One is the name of the field,

31
00:02:05,820 --> 00:02:09,270
so our field names are Pokemon name and type.

32
00:02:10,350 --> 00:02:13,320
And then we've got a list of strings

33
00:02:13,620 --> 00:02:18,420
which is going to be the data that's going to go into that column. Essentially,

34
00:02:18,510 --> 00:02:21,690
every time we call that method, add column,

35
00:02:21,990 --> 00:02:25,740
it's going to allow us to give a field name as a string,

36
00:02:26,070 --> 00:02:29,970
and then a list of data in order that they're going to go into the table.

37
00:02:30,450 --> 00:02:35,450
So we're going to add our table one column at a time. Using that documentation

38
00:02:36,600 --> 00:02:41,600
see if you can add both of these columns to our table object and then once

39
00:02:43,080 --> 00:02:46,410
you've done that, we're going to print the table object again

40
00:02:46,740 --> 00:02:51,270
and we should be to see an ASCII table. This is what you're aiming for

41
00:02:51,270 --> 00:02:52,470
when you run your code.

42
00:02:52,830 --> 00:02:57,810
It should print out your table and nicely format it in this ASCII style.

43
00:02:58,200 --> 00:03:00,460
Pause the video and complete the challenge.

44
00:03:04,110 --> 00:03:04,380
Right?

45
00:03:04,380 --> 00:03:04,650
All right.

46
00:03:04,650 --> 00:03:09,510
So we've got our table object and remember that methods are functions that are

47
00:03:09,510 --> 00:03:14,190
associated with the object. So we say table object dot,

48
00:03:14,310 --> 00:03:16,140
and then we get to call that method,

49
00:03:16,530 --> 00:03:21,090
add_column. And notice how it takes two inputs,

50
00:03:21,120 --> 00:03:24,240
the field name and the data that's going to go into the column.

51
00:03:24,840 --> 00:03:26,700
So the field name is going to be a string,

52
00:03:27,000 --> 00:03:31,350
and this is going to be the name at the top of our column. So in our case,

53
00:03:31,350 --> 00:03:35,490
it was called Pokemon name. And then after a comma,

54
00:03:35,490 --> 00:03:40,380
we get to put in a list of all the data that's going to go into our column.

55
00:03:41,280 --> 00:03:44,880
So just as a quick reminder, our first column contains Pikachu,

56
00:03:44,880 --> 00:03:46,140
Squirtle and Charmander.

57
00:03:46,140 --> 00:03:51,140
Now, we should have added a column to our table and you can safely ignore all

58
00:03:53,340 --> 00:03:57,690
of these typos because of course, Pokemon names are not real words.

59
00:03:58,020 --> 00:04:01,200
But if you wanted to check against the poke dex

60
00:04:01,380 --> 00:04:04,110
you can actually go to this link in the course resources,

61
00:04:04,440 --> 00:04:08,490
and you can see how each of these Pokemon are spelt. Now,

62
00:04:08,700 --> 00:04:12,870
the next thing we want to add is the associated type with each of these

63
00:04:12,870 --> 00:04:17,130
Pokemons. So for example, Charmander is a fire type of Pokemon,

64
00:04:17,160 --> 00:04:19,230
Squirtle is a water type Pokemon,

65
00:04:19,709 --> 00:04:23,430
but essentially we're going to add the data that's in the second column.

66
00:04:23,880 --> 00:04:27,270
So the field name is called type, and then we've got three pieces of data

67
00:04:27,570 --> 00:04:32,430
that's going to need to go into the list in the same order as the previous

68
00:04:32,430 --> 00:04:35,370
Pokemon. That way we'll actually match them up properly.

69
00:04:37,200 --> 00:04:40,110
So if you haven't already, go ahead and add the second column.

70
00:04:40,920 --> 00:04:41,753
Right?

71
00:04:44,460 --> 00:04:45,090
So again,

72
00:04:45,090 --> 00:04:50,090
I'm going to call the same method on my table and this time I'm going to add the

73
00:04:50,310 --> 00:04:55,310
field type and the data is going to go in the order of the data that I had from

74
00:04:56,670 --> 00:05:01,440
the previous column. So the first one is the type of Pikachu which is electric.

75
00:05:02,010 --> 00:05:06,900
And the second one is the type for Squirtle, which is Water. And finally,

76
00:05:06,930 --> 00:05:08,880
we've got fire for Charmander.

77
00:05:09,480 --> 00:05:12,210
So now this will be matched to this,

78
00:05:12,300 --> 00:05:14,730
this will be matched this and so on and so forth.

79
00:05:15,210 --> 00:05:17,700
If we go ahead and print our table now, so

80
00:05:17,820 --> 00:05:20,820
if we run our code and take a look in here,

81
00:05:20,850 --> 00:05:25,850
you can see how we've now got a nicely formatted table by creating an object

82
00:05:27,870 --> 00:05:32,640
from this pre-made prettytable class and we've now called this method

83
00:05:32,730 --> 00:05:36,810
add_column to add two columns. And when we print our table,

84
00:05:36,870 --> 00:05:41,160
it's now nicely formatted in ASCII. Now,

85
00:05:41,190 --> 00:05:44,880
remember that we can also change the object's attributes.

86
00:05:45,900 --> 00:05:48,780
For example, if we wanted to change the appearance of our table,

87
00:05:49,170 --> 00:05:53,580
that's probably controlled by an attribute. If you scroll down,

88
00:05:53,640 --> 00:05:58,640
you can see that we can change the table style by tapping into each of these attributes.

89
00:05:59,600 --> 00:06:03,710
For example, if I wanted to change the alignment of my data in the table,

90
00:06:04,040 --> 00:06:09,040
I can change this align attribute to L for left-align, C for the center-align or

91
00:06:10,550 --> 00:06:11,900
R for right-aligned.

92
00:06:13,010 --> 00:06:17,750
Do you remember how to tap into an object's attribute? If you do,

93
00:06:18,110 --> 00:06:23,110
go ahead and see if you can change our table from center-aligned by default to

94
00:06:23,600 --> 00:06:27,650
left-aligned. Pause the video and try to complete that challenge.

95
00:06:28,180 --> 00:06:29,013
Right?

96
00:06:30,340 --> 00:06:31,090
So again,

97
00:06:31,090 --> 00:06:36,090
we're going to tap into our table object and we're going to use the dot

98
00:06:36,250 --> 00:06:38,290
notation, but this time

99
00:06:38,320 --> 00:06:42,280
instead of accessing a method associated with the object

100
00:06:42,550 --> 00:06:47,110
which is of course denoted by the M, we're going to access an attribute or in

101
00:06:47,110 --> 00:06:50,140
this case, they've titled F which is a field.

102
00:06:50,710 --> 00:06:55,510
And the one that we want is this align. So let's go ahead and type align,

103
00:06:55,960 --> 00:07:00,340
and now we have access to this attribute. Let's see what happens

104
00:07:00,340 --> 00:07:03,340
if I go ahead and print this align attribute.

105
00:07:04,270 --> 00:07:08,770
You can see that for both of my columns, Pokemon name and type,

106
00:07:09,100 --> 00:07:12,370
the align attribute is set to C, which is centered.

107
00:07:13,240 --> 00:07:18,240
What I want to do is change the whole table to be left-aligned. Just as we would

108
00:07:18,730 --> 00:07:21,760
with any other variable we can of course print it,

109
00:07:22,090 --> 00:07:23,800
but we can also change it.

110
00:07:24,280 --> 00:07:28,090
And we would change a variable by just using the equal sign, right?

111
00:07:28,390 --> 00:07:31,330
So let's change the alignment to left-aligned,

112
00:07:31,810 --> 00:07:33,490
and now let's run the code again.

113
00:07:34,570 --> 00:07:39,570
And you can see now both of my columns are now aligning with the left hand

114
00:07:40,150 --> 00:07:40,983
margin.

115
00:07:42,700 --> 00:07:45,580
So we've seen how we can use attributes

116
00:07:45,640 --> 00:07:49,480
like the ones here to change the styling of our table.

117
00:07:49,750 --> 00:07:52,300
We've seen how we can use methods

118
00:07:52,510 --> 00:07:57,510
like add_column or add_row to work with this table object and get it to

119
00:07:58,810 --> 00:08:02,680
perform some sort of functionality, like adding pieces of data to it.

120
00:08:03,130 --> 00:08:08,130
And we've seen how we can create new objects by simply constructing it from the

121
00:08:08,350 --> 00:08:13,000
blueprint class. Feel free to mess around with prettytable

122
00:08:13,240 --> 00:08:15,940
as much as you like change, other things about it,

123
00:08:16,000 --> 00:08:17,770
or add different pieces of data.

124
00:08:18,130 --> 00:08:23,130
But once you're happy with creating the object changing the attributes and

125
00:08:23,770 --> 00:08:25,150
calling the methods,

126
00:08:25,510 --> 00:08:28,900
then head over to the next lesson where I've got a quiz for you.

