1
00:00:00,269 --> 00:00:07,319
Good, now I'll go ahead and transform
this script into an object oriented

2
00:00:07,319 --> 00:00:12,870
programming style, so in other words I'll
organize these functions in a more

3
00:00:12,870 --> 00:00:19,740
efficient way and just to refresh your
memory what this script does, so we have

4
00:00:19,740 --> 00:00:25,619
a back-end and front-end script, and the
front-end what it does is it constructs

5
00:00:25,619 --> 00:00:32,910
the graphical user interface so
you know that. And we have this books dot

6
00:00:32,910 --> 00:00:39,420
db database file, and there are some data
there and you can search for entries so

7
00:00:39,420 --> 00:00:43,980
for rows in the database, add new and so on.
And this frontend.py constructs

8
00:00:43,980 --> 00:00:48,719
the graphical user interface
and this backend is responsible for the

9
00:00:48,719 --> 00:00:53,570
database interaction, and so
object-oriented programming is about

10
00:00:53,570 --> 00:01:00,859
encapsulating functions inside a class.
So class, and let's call this database.

11
00:01:07,820 --> 00:01:16,049
Yeah, that's it basically we still have a
lot of things to do but, let's think

12
00:01:16,049 --> 00:01:21,689
thing about what is this database class.
Well, whenever you decide to use

13
00:01:21,689 --> 00:01:26,040
object-oriented programming you first
need to think about the object that you

14
00:01:26,040 --> 00:01:30,450
want to use which is the object that
that you want to apply this methods to

15
00:01:30,450 --> 00:01:38,070
and in our case that would be the
database, so then we insert data to the

16
00:01:38,070 --> 00:01:41,970
tables of this database and view data
and so on.

17
00:01:41,970 --> 00:01:48,479
Now this object, this object you first
need to create a minimal object when you

18
00:01:48,479 --> 00:01:54,060
call the class, and I'll show you how you
call a class in a minute, but for now

19
00:01:54,060 --> 00:01:59,040
let's focus on creating the blueprint
of the object. So you create the

20
00:01:59,040 --> 00:02:03,840
blueprint of the object which is this one
here, and then you create object

21
00:02:03,840 --> 00:02:10,709
instances using that blueprint, and you
do that by calling the class. Now when

22
00:02:10,709 --> 00:02:15,840
you call the class you want to have
this minimal object, so in this case that

23
00:02:15,840 --> 00:02:21,510
would be to establish a connection to
this database, and then create a coursor

24
00:02:21,510 --> 00:02:26,130
object, and maybe check for tables so
that you make sure that your database

25
00:02:26,130 --> 00:02:33,540
has a table, and so that it allows you to
do later apply methods to this table of

26
00:02:33,540 --> 00:02:39,330
database, otherwise the database
would be useless, so it needs to have

27
00:02:39,330 --> 00:02:44,760
a table and an established connection.
That means you have to figure out a way

28
00:02:44,760 --> 00:02:52,890
that when you call the class these lines
here are executed. And Python has

29
00:02:52,890 --> 00:02:59,510
reserved a special syntax for that.
So the first function of your script you

30
00:02:59,510 --> 00:03:07,260
need to call it init with double
underscore in front and on the back.

31
00:03:07,260 --> 00:03:11,790
So that's an init function which means
initialize an object. In other

32
00:03:11,790 --> 00:03:16,400
programming languages that is called
a constructor so, it constructs the object.

33
00:03:16,400 --> 00:03:23,340
So when you call a class, this function
is executed but not the other ones.

34
00:03:23,340 --> 00:03:27,450
The other ones are executed if you refer
to them like in this case that would be

35
00:03:27,450 --> 00:03:31,860
something like a database dot insert,
and then you pass the parameters, the

36
00:03:31,860 --> 00:03:37,260
arguments there. Now we have quite a few
things to add here in script for this to

37
00:03:37,260 --> 00:03:44,730
work, but I'd prefer to do this on a trial
and error base, so we'll execute it, we'll

38
00:03:44,730 --> 00:03:49,980
see whatever we get so that you understand
how things are working there, so what

39
00:03:49,980 --> 00:03:55,620
I'll do I'll call this class as it is
now, so we don't need this function. This

40
00:03:55,620 --> 00:04:01,799
function is gone now because I replaced that
with init, so you know you don't call

41
00:04:01,799 --> 00:04:08,850
functions anymore like we did here, so
for instance here we are calling

42
00:04:08,850 --> 00:04:14,280
functions from the backend module so
which is like a module, you are importing

43
00:04:14,280 --> 00:04:25,419
this as a module. And in this case now
what we do is from the backend import

44
00:04:25,419 --> 00:04:34,030
database, so we import the database class
from the backend script so that makes

45
00:04:34,030 --> 00:04:39,520
available the database class for us so
the database blueprint. So this is just

46
00:04:39,520 --> 00:04:43,810
a blueprint. Now we need to create an
object out of this blueprint, so let's

47
00:04:43,810 --> 00:04:50,770
call that object database and that
would be equal to database, so the class

48
00:04:50,770 --> 00:04:58,659
and so let's, for now let's not pass any
arguments there, and then we need to replace

49
00:04:58,659 --> 00:05:05,590
these references, so we don't do back-end
anymore, we do database dot view. So we

50
00:05:05,590 --> 00:05:14,110
are referring to the view function of
the database object which is an instance

51
00:05:14,110 --> 00:05:16,830
of this class.
Great, so we go ahead and replace

52
00:05:21,580 --> 00:05:30,629
these other ones with database, so
back-end replace that, replac, replace.

53
00:05:30,629 --> 00:05:37,770
Yeah, that's it. This is the script name.
And it should be like that. Escape here.

54
00:05:37,770 --> 00:05:45,490
Great, now I'll go ahead and execute that,
so don't expect this to work but we have

55
00:05:45,490 --> 00:05:51,879
to look closely to the error trying to
understand what it says. So trace back

56
00:05:51,879 --> 00:06:01,389
file frontend dot py line 4, here so in it takes 0
positional argument,  but one was given.

57
00:06:01,389 --> 00:06:08,800
And what's happening here now is that
when you call the class this, class will

58
00:06:08,800 --> 00:06:15,490
create an object instance, and store it
in this variable, and then it sends this

59
00:06:15,490 --> 00:06:24,250
object instance to this function.
So let me split this on the right so that we

60
00:06:24,250 --> 00:06:30,550
have both the scripts in here, so the frontend
and the backend, so basically

61
00:06:30,550 --> 00:06:37,060
this database object instance is
being sent to this init object even

62
00:06:37,060 --> 00:06:42,400
though this is an empty bracket.
So that's like a hidden syntax there and

63
00:06:42,400 --> 00:06:49,639
to fix this you need to perform a trick
here, so it says init takes zero

64
00:06:49,639 --> 00:06:55,129
positional arguments because it actually
it has an empty bracket here, so it's

65
00:06:55,129 --> 00:06:59,870
like a normal function, and functions if
you don't put a parameter there, but then

66
00:06:59,870 --> 00:07:05,330
you pass a parameter when you call it,
the function will say I don't take any

67
00:07:05,330 --> 00:07:09,949
positional arguments or any mandatory
arguments but one argument was given to

68
00:07:09,949 --> 00:07:17,259
me so I'll throw an error. Now to fix that
you need to pass a parameter there and

69
00:07:17,259 --> 00:07:24,169
everyone passes self there, so you can
pass any word, that that is still work,

70
00:07:24,169 --> 00:07:28,999
but as a convention so that so that your
code is readable by other people you

71
00:07:28,999 --> 00:07:35,300
need to pass self. So now your init
function takes in a parameter and when

72
00:07:35,300 --> 00:07:41,089
you call this class, this class will send
object instance, and that object instance

73
00:07:41,089 --> 00:07:46,639
will go inside this parameter. So this
parameter will be replaced by the object

74
00:07:46,639 --> 00:07:53,839
instance argument. Great, if you still
don't understand that, I'm sure you'll do

75
00:07:53,839 --> 00:08:01,250
so later as you write more classes. Good.
Now I'll go ahead and save this and this,

76
00:08:01,250 --> 00:08:08,509
and try out the script again, and great.
So that means that the class was

77
00:08:08,509 --> 00:08:15,229
successfully initialized, and it created
on the object basis for us. Let me

78
00:08:15,229 --> 00:08:20,960
close this, and maybe add something there.
This is not necessary, but just to

79
00:08:20,960 --> 00:08:26,180
show you that if you want to add more
parameters to your init function you do

80
00:08:26,180 --> 00:08:36,349
something like db, and then replace this
with db, and then here you pass the

81
00:08:36,349 --> 00:08:42,229
argument that will be passed to this db
parameter, so that would be books

82
00:08:42,229 --> 00:08:50,269
dot db, the file path to our database.
Great, and if you agree do this again

83
00:08:50,269 --> 00:08:56,280
you get the same result.
So what's happening here is that Python

84
00:08:56,280 --> 00:09:02,010
is first sending the database object
instance to the self parameter and then

85
00:09:02,010 --> 00:09:08,850
it sends the path of the file to
the db parameter, and that db parameter

86
00:09:08,850 --> 00:09:17,580
sends that file path to the db parameter
over the connect method. Great, now let's

87
00:09:17,580 --> 00:09:23,070
see how these methods will work, so we
have the view all button which if we

88
00:09:23,070 --> 00:09:30,150
press it what will happen is the view
function of the database class, actually

89
00:09:30,150 --> 00:09:36,990
the view method of the database class
will be executed, so this one here. Let's

90
00:09:36,990 --> 00:09:44,730
see if we were able to get it. Well an error.
Let me close this. So it says view takes

91
00:09:44,730 --> 00:09:51,120
zero positional arguments, but one was
given, so that again suggests that when

92
00:09:51,120 --> 00:09:56,700
you call a method of class, the class is
sending the object instance to that

93
00:09:56,700 --> 00:10:01,290
method as well just as we did with the
init function. That means we need to do

94
00:10:01,290 --> 00:10:04,980
the trick again.
So just pass self there, and you try out

95
00:10:04,980 --> 00:10:13,710
again, see whatever we get this time.
Well, no error things are working obviously.

96
00:10:13,710 --> 00:10:20,610
So that's great, however if you try the other
methods now like insert, you'll probably get

97
00:10:20,610 --> 00:10:24,750
an error, so let me try that. So View all
is working fine, but if we insert

98
00:10:24,750 --> 00:10:30,650
something let's say "Hey There" and…
And yeah, add entry. So insert takes four

99
00:10:40,720 --> 00:10:46,839
positional arguments, but five were given.
So insert and these values are being

100
00:10:46,839 --> 00:10:51,639
passed to these parameters, but also we
are passing the object instance, so let's

101
00:10:51,639 --> 00:11:05,040
go ahead and add self there, and I'll do the
same for search as well, delete and update.

102
00:11:05,040 --> 00:11:18,779
And yeah, that's what we have. Looks good.
Let's try out again, so view all and

103
00:11:22,949 --> 00:11:30,459
add entry, and yeah this seems like it's
working. View all, and here we have "Hey There".

104
00:11:30,459 --> 00:11:40,360
Close, so we were able to create
a class and put all the functions inside

105
00:11:40,360 --> 00:11:46,120
the class, so we have an organized
program now, and so that's

106
00:11:46,120 --> 00:11:50,740
object-oriented programming,
but object-oriented programming is not

107
00:11:50,740 --> 00:11:55,959
simply about putting some functions
inside a class. Object oriented

108
00:11:55,959 --> 00:12:01,930
programming is about good design, so this
script it's not probably the best

109
00:12:01,930 --> 00:12:08,589
design that we can come up with. Why is that?
Because we're not making use of the

110
00:12:08,589 --> 00:12:15,730
real advantages that we have by using
classes, so what I mean is that here we

111
00:12:15,730 --> 00:12:21,750
are creating these methods and in each
method we're executing some closed

112
00:12:21,750 --> 00:12:28,750
functions, and also creating a connection
in each of the methods. So we can reduce

113
00:12:28,750 --> 00:12:35,769
this code further more buried by
removing the repeated actions, so in a

114
00:12:35,769 --> 00:12:42,730
class of what you need to think about is
a way of constructing something at first,

115
00:12:42,730 --> 00:12:45,910
so in the init function, and then you
refer to that something

116
00:12:45,910 --> 00:12:50,110
in the other functions, in the
other methods, so we're talking about

117
00:12:50,110 --> 00:12:53,920
methods here. So even though our script
is working fine

118
00:12:53,920 --> 00:13:00,070
in the next lecture we will go ahead and
improve the design of it.

119
00:13:00,070 --> 00:13:00,280
So I'll see you there.

