1
00:00:00,180 --> 00:00:00,390
Hi.

2
00:00:00,390 --> 00:00:01,000
Welcome back.

3
00:00:01,020 --> 00:00:05,010
I'm very excited that today you will learn object oriented programming.

4
00:00:05,010 --> 00:00:08,270
So let's begin to be able to follow this video.

5
00:00:08,280 --> 00:00:12,150
You need to download the three files in the lecture resources.

6
00:00:12,150 --> 00:00:17,090
So that is the data db, file, extract, y, M, ML and Main P.

7
00:00:17,220 --> 00:00:23,940
These are the files of the previous app, the Scraping Tools SQL App.

8
00:00:23,940 --> 00:00:31,140
So please create a new patch on project and copy those three files into that project just like I've

9
00:00:31,140 --> 00:00:31,980
done here.

10
00:00:34,050 --> 00:00:41,280
Now what we will do in order for you to understand object oriented programming is we will rewrite this

11
00:00:41,280 --> 00:00:50,790
app by using the object oriented programming paradigm this time, so using classes instead of functions.

12
00:00:51,540 --> 00:00:55,200
But just before we do that, I want to refresh your memory.

13
00:00:55,200 --> 00:00:57,750
I want to quickly explain what this app does.

14
00:00:58,440 --> 00:01:03,770
So these are the import statements and this shrink here is actually not needed.

15
00:01:03,780 --> 00:01:07,230
We just left it there temporarily, but then we forgot to delete it.

16
00:01:07,230 --> 00:01:09,330
So we don't need those two strings.

17
00:01:09,330 --> 00:01:10,590
You can delete them.

18
00:01:13,050 --> 00:01:17,400
And so here we establish a connection to the database data.

19
00:01:17,640 --> 00:01:22,380
DB Which should be located in your project directory, this one in here.

20
00:01:22,590 --> 00:01:28,200
This database file that I gave you has an event table which is currently empty.

21
00:01:28,470 --> 00:01:30,720
Then we have this scrape function.

22
00:01:30,750 --> 00:01:37,890
What this does is it visits this URL in here which points to this website.

23
00:01:38,470 --> 00:01:39,000
Right.

24
00:01:39,720 --> 00:01:41,430
And then this function.

25
00:01:42,460 --> 00:01:49,270
Gets the source code of that page, which corresponds to.

26
00:01:51,710 --> 00:01:53,750
This HTML code here.

27
00:01:55,740 --> 00:02:03,510
And then this other function in here gets that source code that this function returns and it extracts

28
00:02:03,510 --> 00:02:09,840
only this part, which sometimes is an event, a musical event of a band.

29
00:02:11,580 --> 00:02:14,550
It contains the city and the date of the event.

30
00:02:14,550 --> 00:02:16,830
And sometimes this changes.

31
00:02:16,830 --> 00:02:21,150
So if I refresh the page, sometimes we have no upcoming tools.

32
00:02:21,150 --> 00:02:30,000
Sometimes we might have another event playing here, and other times it could be the same event and

33
00:02:30,000 --> 00:02:30,870
so on.

34
00:02:31,170 --> 00:02:38,130
This function returns only that value, whatever it is, no upcoming tools, all the events.

35
00:02:38,160 --> 00:02:38,590
Right.

36
00:02:38,590 --> 00:02:43,740
So then this other function here sends an email.

37
00:02:44,130 --> 00:02:47,520
That's what the responsibility of this function is.

38
00:02:47,520 --> 00:02:50,720
It sends an email to that receiver email address.

39
00:02:50,730 --> 00:02:51,480
That's it.

40
00:02:51,660 --> 00:02:55,740
Then we have this other function store, this other function.

41
00:02:57,350 --> 00:03:07,460
Gets the event this value and it stores it in the database using this SQL query here in that data DB

42
00:03:07,460 --> 00:03:08,360
database.

43
00:03:09,170 --> 00:03:14,990
And this other function reads the existing data of that data DB database.

44
00:03:15,710 --> 00:03:17,710
And it returns those data.

45
00:03:17,720 --> 00:03:25,190
So it returns the rows, which is like a tuple containing different lists, different values, depending

46
00:03:25,190 --> 00:03:28,820
on how many values we have in the DB file.

47
00:03:29,180 --> 00:03:35,600
And this part here is where we call all this functions, which is this here.

48
00:03:35,600 --> 00:03:43,820
So we run a loop and then in every iteration we get first the source code of the page using the script

49
00:03:43,820 --> 00:03:47,000
function, this script function in here.

50
00:03:47,280 --> 00:03:52,000
And then we extract the value, which could be no upcoming tools or something else.

51
00:03:52,010 --> 00:03:59,180
We store that value in here and then we say if the value is different than than no upcoming tools.

52
00:03:59,180 --> 00:04:06,410
So if there is an event on the page, then we read the existing events from the database using the read

53
00:04:06,410 --> 00:04:07,130
function.

54
00:04:10,160 --> 00:04:12,800
So we return the existing events.

55
00:04:12,800 --> 00:04:15,840
We see if we have stalled at events already.

56
00:04:15,860 --> 00:04:22,820
If not, if there are no such events, then we store the event because the event doesn't exist in the

57
00:04:22,820 --> 00:04:23,240
database.

58
00:04:23,240 --> 00:04:29,540
So we store it and then we send an email to ourselves, letting ourselves know that there was a new

59
00:04:29,540 --> 00:04:38,000
event in the website, a new event which was not in the database, and so we should know about that.

60
00:04:38,600 --> 00:04:40,220
That's what this app does.

61
00:04:40,260 --> 00:04:40,940
No.

62
00:04:42,400 --> 00:04:46,590
We can rewrite this using classes.

63
00:04:46,600 --> 00:04:47,860
So let's do that.

64
00:04:49,060 --> 00:04:54,280
Let's install these packages first requests and select your lib.

65
00:04:54,370 --> 00:04:57,310
So we get rid of these underlines.

66
00:05:01,680 --> 00:05:05,990
And let me create the first class to do that.

67
00:05:06,000 --> 00:05:08,370
Please select these two functions.

68
00:05:08,370 --> 00:05:12,420
Scrape and extract and then press tab.

69
00:05:12,450 --> 00:05:21,720
Tab will indent those functions with four space characters and then create some space in here and on

70
00:05:21,720 --> 00:05:29,100
top, say class, tool or event, whatever you prefer.

71
00:05:29,310 --> 00:05:31,250
I'll just leave it event.

72
00:05:31,260 --> 00:05:40,950
And then in the function parameters you need to add self as the first parameter comma followed by the

73
00:05:40,950 --> 00:05:43,610
other parameter that the function already has.

74
00:05:43,620 --> 00:05:46,560
Same for this self and that.

75
00:05:47,070 --> 00:05:52,590
And have another brake line here because that's what it is recommended to have.

76
00:05:52,590 --> 00:05:55,770
And here you need one brake line.

77
00:05:55,770 --> 00:05:59,520
So this is not mandatory, but it's a good practice.

78
00:05:59,520 --> 00:06:05,880
So to break lines after the class definition to Berkley's before the class definition.

79
00:06:06,780 --> 00:06:18,540
So this here is a class definition or just class for short, and a class is made of functions.

80
00:06:18,780 --> 00:06:28,770
So this class here has two functions scrape and extract, but we don't use the word function anymore

81
00:06:28,890 --> 00:06:31,490
when we are working with classes.

82
00:06:31,500 --> 00:06:35,400
Now these two are called methods actually.

83
00:06:35,400 --> 00:06:39,930
So we have the extract methods and we have the scrape method.

84
00:06:40,230 --> 00:06:44,280
So these two methods belong to the event class.

85
00:06:44,850 --> 00:06:51,300
Later, I'll explain you what self is, but for now you can just ignore that and you'll understand it

86
00:06:51,300 --> 00:06:52,680
later what it is.

87
00:06:53,130 --> 00:06:56,850
Now, you would ask, Well, why would we use a class?

88
00:06:56,850 --> 00:07:00,510
Why not just leaving those functions as they were?

89
00:07:00,720 --> 00:07:08,220
Well, but as you can see, this gives us a better way to organize the code because we have so many

90
00:07:08,220 --> 00:07:09,390
functions here.

91
00:07:09,960 --> 00:07:13,950
So it makes sense that you categorize them into classes.

92
00:07:14,580 --> 00:07:22,470
So these two functions, basically they belong to the event class because they scrape the page that

93
00:07:22,470 --> 00:07:26,310
contains the event and then they instruct the event.

94
00:07:26,310 --> 00:07:32,220
So it's a good idea to place them inside a class called event.

95
00:07:32,250 --> 00:07:35,040
Now you can also name this class like that.

96
00:07:35,040 --> 00:07:37,770
It is possible, but it is not recommended.

97
00:07:37,770 --> 00:07:44,070
And also partition will tell us that class names should be camel case, which means starting with a

98
00:07:44,070 --> 00:07:45,060
calculator.

99
00:07:45,330 --> 00:07:48,930
And if the word has two parts like.

100
00:07:49,510 --> 00:07:51,800
It could be a good event.

101
00:07:51,820 --> 00:07:59,830
Then you use a capital letter in here, no spaces or underscores, but a capital letter for the second

102
00:07:59,830 --> 00:08:00,580
name.

103
00:08:00,880 --> 00:08:02,290
And here we only have one.

104
00:08:02,290 --> 00:08:03,940
So let's just keep it event.

105
00:08:04,510 --> 00:08:04,780
Right.

106
00:08:04,780 --> 00:08:07,360
So that's the class definition of both.

107
00:08:07,390 --> 00:08:12,760
Now we need to change the way these methods are called here.

108
00:08:12,760 --> 00:08:16,180
So currently we still have them behave like functions.

109
00:08:16,180 --> 00:08:22,420
And PA charm is telling us that oh, there is no function with the name scrape, which is true because

110
00:08:22,420 --> 00:08:26,500
scrape is not a function anymore in the global namespace.

111
00:08:26,500 --> 00:08:31,750
Now this function is methods and it belongs to that event class.

112
00:08:32,470 --> 00:08:43,270
So before you call that method, you first should create an event instance by calling that class.

113
00:08:43,450 --> 00:08:49,660
So the way you call that class is like this event, the name of the class followed by parentheses.

114
00:08:49,660 --> 00:08:53,200
So this is similar to how you call a function later.

115
00:08:53,200 --> 00:08:58,020
You will also see that classes can also get arguments like functions do.

116
00:08:58,030 --> 00:09:05,170
But for now, let's just focus on calling a class without arguments and then event will be the variable

117
00:09:05,170 --> 00:09:08,950
where the instance of the class will be stored.

118
00:09:08,950 --> 00:09:09,670
Right?

119
00:09:09,790 --> 00:09:15,220
So you can have many instances now like event one event event to.

120
00:09:16,660 --> 00:09:17,530
Event.

121
00:09:17,530 --> 00:09:22,870
And when you learn about arguments, you'll see that these different events can take different values

122
00:09:22,870 --> 00:09:24,250
for the arguments and so on.

123
00:09:24,370 --> 00:09:28,330
So let's just ignore multiple instances for now.

124
00:09:29,970 --> 00:09:36,780
For now, you should know that this creates an instance and without parentheses is just a class.

125
00:09:37,470 --> 00:09:44,490
So once you create an instance, an event instance, then in here you should point to that event instance.

126
00:09:44,490 --> 00:09:47,720
So you see events with lowercase letters.

127
00:09:47,730 --> 00:09:51,060
So we point to the instance that scrape.

128
00:09:51,800 --> 00:09:53,210
Same goes for extract.

129
00:09:53,210 --> 00:09:56,360
Extract is also a method of the event instance.

130
00:09:57,690 --> 00:10:00,510
So events that extract event that scrape.

131
00:10:01,810 --> 00:10:10,270
And if we run this code now, it will work exactly the same as it used to do when we had functions in

132
00:10:10,270 --> 00:10:10,810
here.

133
00:10:10,810 --> 00:10:17,020
So the functionality of the code doesn't change when you use object oriented programming.

134
00:10:17,170 --> 00:10:20,580
What changes is the structure of the program.

135
00:10:20,590 --> 00:10:28,780
So object oriented programming is good for you as a programmer or for your collaborators, other programmers

136
00:10:28,780 --> 00:10:36,730
who are collaborating on the same app because it gives you a well structured app which is easier to

137
00:10:36,730 --> 00:10:40,420
extend later and it's easier to modify.

138
00:10:41,330 --> 00:10:45,250
Now, Python might give you some advice here.

139
00:10:45,260 --> 00:10:49,480
Maybe this method is static, it says, but feel free to ignore that.

140
00:10:49,490 --> 00:10:51,860
We'll talk about static methods later.

141
00:10:54,610 --> 00:10:55,240
No.

142
00:10:56,410 --> 00:11:00,130
We can do the same with the SANS email function.

143
00:11:00,820 --> 00:11:08,950
So the idea is that when you use object oriented programming, every function is categorized in objects

144
00:11:09,040 --> 00:11:10,540
or classes.

145
00:11:11,920 --> 00:11:16,710
So we use the term class and object interchangeably sometimes.

146
00:11:16,720 --> 00:11:20,380
That's why it's called object oriented programming.

147
00:11:20,380 --> 00:11:23,380
So your code is built around objects.

148
00:11:23,380 --> 00:11:26,890
The first object we created was event.

149
00:11:27,400 --> 00:11:28,780
So that's the object.

150
00:11:28,780 --> 00:11:33,460
And then this is the object instance here.

151
00:11:33,460 --> 00:11:41,410
So it's an instance of that object when we create later a ticket class for a hotel reservation app or

152
00:11:41,410 --> 00:11:48,550
an airplane reservation app, then ticket will be one class, but users will create multiple ticket

153
00:11:48,550 --> 00:11:49,480
instances, right?

154
00:11:49,480 --> 00:11:55,330
A user will create their own ticket using that class, that blueprint, and other user will create another

155
00:11:55,330 --> 00:11:56,350
ticket and so on.

156
00:11:59,700 --> 00:12:11,310
So that is one object, and then the other object would be class email and this class would have that

157
00:12:11,430 --> 00:12:12,360
method.

158
00:12:12,510 --> 00:12:19,440
So select that press tab to indent it, make a break line here to have to break lines here and to break

159
00:12:19,440 --> 00:12:20,430
lines in here.

160
00:12:21,180 --> 00:12:29,860
And so this class now has a sent email methods which perhaps we can rename this into send.

161
00:12:29,880 --> 00:12:31,620
Why did I rename it?

162
00:12:31,800 --> 00:12:34,230
Well, because let me show you why.

163
00:12:34,380 --> 00:12:41,070
So we go here then, and we don't call the send email function, but we say email.

164
00:12:41,070 --> 00:12:44,940
We create an instance of email class, which we just created.

165
00:12:45,260 --> 00:12:49,710
And then in here we say email dot, just send.

166
00:12:49,710 --> 00:12:53,640
So it makes more sense to say email that send, then email that.

167
00:12:53,640 --> 00:12:54,300
Send email.

168
00:12:54,300 --> 00:12:54,840
Right.

169
00:12:55,760 --> 00:13:01,940
So by using classes now you have more freedom to use shorter names.

170
00:13:02,360 --> 00:13:08,660
So now you know that this send method belongs to the email class, so things are more categorized.

171
00:13:08,690 --> 00:13:17,630
Yes, we do have some longer code because we added a class here, right this line.

172
00:13:17,630 --> 00:13:25,420
And then we we add the instantiation part of the class here and here as well as well.

173
00:13:25,430 --> 00:13:30,050
But longer code doesn't necessarily mean worse code.

174
00:13:30,290 --> 00:13:34,490
The code should be readable and it should be extensible.

175
00:13:34,760 --> 00:13:37,370
That is what makes good code.

176
00:13:38,790 --> 00:13:43,620
And that's about this video in the next video.

177
00:13:43,650 --> 00:13:47,250
We will work on creating a class for these two.

178
00:13:47,460 --> 00:13:54,540
The process here will be a bit more advanced, and that is where you learn about the init method, which

179
00:13:54,540 --> 00:13:57,840
is a special method and also about self.

180
00:13:59,610 --> 00:14:01,980
Which should also be here, by the way.

181
00:14:02,670 --> 00:14:09,630
So self should be in every method of the class most of the times, because sometimes in special cases

182
00:14:09,630 --> 00:14:11,040
you might omit self.

183
00:14:11,040 --> 00:14:14,130
But that's a special case which we will cover later on.

184
00:14:14,280 --> 00:14:15,480
So that's.

185
00:14:16,870 --> 00:14:21,940
About classes in a superficial level, but we have a lot more to cover.

186
00:14:23,360 --> 00:14:27,110
But for now it's enough to remember the syntax of classes.

187
00:14:27,110 --> 00:14:29,240
And let's move on to the next video.

188
00:14:29,240 --> 00:14:29,840
I'll see the.

