Hi.
Welcome back.
I'm very excited that today you will learn object oriented programming.
So let's begin to be able to follow this video.
You need to download the three files in the lecture resources.
So that is the data db, file, extract, y, M, ML and Main P.
These are the files of the previous app, the Scraping Tools SQL App.
So please create a new patch on project and copy those three files into that project just like I've
done here.
Now what we will do in order for you to understand object oriented programming is we will rewrite this
app by using the object oriented programming paradigm this time, so using classes instead of functions.
But just before we do that, I want to refresh your memory.
I want to quickly explain what this app does.
So these are the import statements and this shrink here is actually not needed.
We just left it there temporarily, but then we forgot to delete it.
So we don't need those two strings.
You can delete them.
And so here we establish a connection to the database data.
DB Which should be located in your project directory, this one in here.
This database file that I gave you has an event table which is currently empty.
Then we have this scrape function.
What this does is it visits this URL in here which points to this website.
Right.
And then this function.
Gets the source code of that page, which corresponds to.
This HTML code here.
And then this other function in here gets that source code that this function returns and it extracts
only this part, which sometimes is an event, a musical event of a band.
It contains the city and the date of the event.
And sometimes this changes.
So if I refresh the page, sometimes we have no upcoming tools.
Sometimes we might have another event playing here, and other times it could be the same event and
so on.
This function returns only that value, whatever it is, no upcoming tools, all the events.
Right.
So then this other function here sends an email.
That's what the responsibility of this function is.
It sends an email to that receiver email address.
That's it.
Then we have this other function store, this other function.
Gets the event this value and it stores it in the database using this SQL query here in that data DB
database.
And this other function reads the existing data of that data DB database.
And it returns those data.
So it returns the rows, which is like a tuple containing different lists, different values, depending
on how many values we have in the DB file.
And this part here is where we call all this functions, which is this here.
So we run a loop and then in every iteration we get first the source code of the page using the script
function, this script function in here.
And then we extract the value, which could be no upcoming tools or something else.
We store that value in here and then we say if the value is different than than no upcoming tools.
So if there is an event on the page, then we read the existing events from the database using the read
function.
So we return the existing events.
We see if we have stalled at events already.
If not, if there are no such events, then we store the event because the event doesn't exist in the
database.
So we store it and then we send an email to ourselves, letting ourselves know that there was a new
event in the website, a new event which was not in the database, and so we should know about that.
That's what this app does.
No.
We can rewrite this using classes.
So let's do that.
Let's install these packages first requests and select your lib.
So we get rid of these underlines.
And let me create the first class to do that.
Please select these two functions.
Scrape and extract and then press tab.
Tab will indent those functions with four space characters and then create some space in here and on
top, say class, tool or event, whatever you prefer.
I'll just leave it event.
And then in the function parameters you need to add self as the first parameter comma followed by the
other parameter that the function already has.
Same for this self and that.
And have another brake line here because that's what it is recommended to have.
And here you need one brake line.
So this is not mandatory, but it's a good practice.
So to break lines after the class definition to Berkley's before the class definition.
So this here is a class definition or just class for short, and a class is made of functions.
So this class here has two functions scrape and extract, but we don't use the word function anymore
when we are working with classes.
Now these two are called methods actually.
So we have the extract methods and we have the scrape method.
So these two methods belong to the event class.
Later, I'll explain you what self is, but for now you can just ignore that and you'll understand it
later what it is.
Now, you would ask, Well, why would we use a class?
Why not just leaving those functions as they were?
Well, but as you can see, this gives us a better way to organize the code because we have so many
functions here.
So it makes sense that you categorize them into classes.
So these two functions, basically they belong to the event class because they scrape the page that
contains the event and then they instruct the event.
So it's a good idea to place them inside a class called event.
Now you can also name this class like that.
It is possible, but it is not recommended.
And also partition will tell us that class names should be camel case, which means starting with a
calculator.
And if the word has two parts like.
It could be a good event.
Then you use a capital letter in here, no spaces or underscores, but a capital letter for the second
name.
And here we only have one.
So let's just keep it event.
Right.
So that's the class definition of both.
Now we need to change the way these methods are called here.
So currently we still have them behave like functions.
And PA charm is telling us that oh, there is no function with the name scrape, which is true because
scrape is not a function anymore in the global namespace.
Now this function is methods and it belongs to that event class.
So before you call that method, you first should create an event instance by calling that class.
So the way you call that class is like this event, the name of the class followed by parentheses.
So this is similar to how you call a function later.
You will also see that classes can also get arguments like functions do.
But for now, let's just focus on calling a class without arguments and then event will be the variable
where the instance of the class will be stored.
Right?
So you can have many instances now like event one event event to.
Event.
And when you learn about arguments, you'll see that these different events can take different values
for the arguments and so on.
So let's just ignore multiple instances for now.
For now, you should know that this creates an instance and without parentheses is just a class.
So once you create an instance, an event instance, then in here you should point to that event instance.
So you see events with lowercase letters.
So we point to the instance that scrape.
Same goes for extract.
Extract is also a method of the event instance.
So events that extract event that scrape.
And if we run this code now, it will work exactly the same as it used to do when we had functions in
here.
So the functionality of the code doesn't change when you use object oriented programming.
What changes is the structure of the program.
So object oriented programming is good for you as a programmer or for your collaborators, other programmers
who are collaborating on the same app because it gives you a well structured app which is easier to
extend later and it's easier to modify.
Now, Python might give you some advice here.
Maybe this method is static, it says, but feel free to ignore that.
We'll talk about static methods later.
No.
We can do the same with the SANS email function.
So the idea is that when you use object oriented programming, every function is categorized in objects
or classes.
So we use the term class and object interchangeably sometimes.
That's why it's called object oriented programming.
So your code is built around objects.
The first object we created was event.
So that's the object.
And then this is the object instance here.
So it's an instance of that object when we create later a ticket class for a hotel reservation app or
an airplane reservation app, then ticket will be one class, but users will create multiple ticket
instances, right?
A user will create their own ticket using that class, that blueprint, and other user will create another
ticket and so on.
So that is one object, and then the other object would be class email and this class would have that
method.
So select that press tab to indent it, make a break line here to have to break lines here and to break
lines in here.
And so this class now has a sent email methods which perhaps we can rename this into send.
Why did I rename it?
Well, because let me show you why.
So we go here then, and we don't call the send email function, but we say email.
We create an instance of email class, which we just created.
And then in here we say email dot, just send.
So it makes more sense to say email that send, then email that.
Send email.
Right.
So by using classes now you have more freedom to use shorter names.
So now you know that this send method belongs to the email class, so things are more categorized.
Yes, we do have some longer code because we added a class here, right this line.
And then we we add the instantiation part of the class here and here as well as well.
But longer code doesn't necessarily mean worse code.
The code should be readable and it should be extensible.
That is what makes good code.
And that's about this video in the next video.
We will work on creating a class for these two.
The process here will be a bit more advanced, and that is where you learn about the init method, which
is a special method and also about self.
Which should also be here, by the way.
So self should be in every method of the class most of the times, because sometimes in special cases
you might omit self.
But that's a special case which we will cover later on.
So that's.
About classes in a superficial level, but we have a lot more to cover.
But for now it's enough to remember the syntax of classes.
And let's move on to the next video.
I'll see the.