1
00:00:00,330 --> 00:00:02,550
Welcome to the section on Decorators.

2
00:00:03,180 --> 00:00:04,980
Let's consider this example.

3
00:00:05,880 --> 00:00:15,750
Suppose we have a system which permits users get their IDs based on their email address and also get

4
00:00:15,750 --> 00:00:19,000
their subscription status based on the same email address.

5
00:00:19,560 --> 00:00:24,690
So we could have a dictionary so we wouldn't have to use a dictionary.

6
00:00:27,780 --> 00:00:28,620
Which is this?

7
00:00:29,220 --> 00:00:30,840
Now we just fix out all.

8
00:00:30,840 --> 00:00:32,490
We just fix some email addresses.

9
00:00:32,490 --> 00:00:33,720
So say for example.

10
00:00:33,720 --> 00:00:37,000
A at the moment.

11
00:00:37,050 --> 00:00:47,760
Com And this agreement would come this linked to an idea of C two or three four and then to a subscription

12
00:00:47,760 --> 00:00:48,720
status of true.

13
00:00:49,710 --> 00:00:53,490
Then the next person could be c be copy of this.

14
00:00:55,080 --> 00:00:56,100
You have that.

15
00:00:56,700 --> 00:00:57,510
There we go.

16
00:00:57,900 --> 00:00:58,440
That's fine.

17
00:00:58,680 --> 00:01:01,050
So here we have B I gmail.com.

18
00:01:01,050 --> 00:01:01,050
Come.

19
00:01:03,720 --> 00:01:04,080
Then.

20
00:01:04,080 --> 00:01:09,600
Yeah we have C 39 then.

21
00:01:09,730 --> 00:01:11,220
So that is true.

22
00:01:11,640 --> 00:01:12,060
True.

23
00:01:12,750 --> 00:01:14,100
See we have that.

24
00:01:14,700 --> 00:01:18,780
So for then, the status is false.

25
00:01:20,130 --> 00:01:25,800
Then we have the the status is the ID.

26
00:01:25,950 --> 00:01:26,630
That's the idea.

27
00:01:26,640 --> 00:01:27,330
Zero.

28
00:01:27,340 --> 00:01:28,860
And then yeah, the status is false.

29
00:01:29,700 --> 00:01:30,060
Okay.

30
00:01:30,070 --> 00:01:40,650
So we have this system or we have this database where we could link the email address to a corresponding

31
00:01:41,430 --> 00:01:48,180
subscription status and ID could print out this user dictionary.

32
00:01:49,110 --> 00:01:49,710
There we go.

33
00:01:50,640 --> 00:01:58,950
We can now define two functions where in the first function permits us to get the ID based on the email

34
00:01:58,950 --> 00:01:59,550
address.

35
00:01:59,670 --> 00:02:01,700
So we have the ID based on the email.

36
00:02:01,710 --> 00:02:03,720
We have user of email.

37
00:02:04,440 --> 00:02:08,760
And then that's for firstly, let's see how we're going to get this.

38
00:02:08,880 --> 00:02:11,070
We have this user dictionary right here.

39
00:02:11,940 --> 00:02:21,840
So once you have the user dictionary, if we have an email, see at gmail.com, we see that put in.

40
00:02:21,840 --> 00:02:24,330
This gives us this value right here.

41
00:02:24,360 --> 00:02:31,170
Now, if we have D, for example, or we have FEC, this is key error because that's not the dictionary.

42
00:02:31,170 --> 00:02:32,970
If we have the there we go.

43
00:02:33,360 --> 00:02:42,330
Now, once we have this, we could select the ID or the subscription status.

44
00:02:42,570 --> 00:02:44,280
So we just get added function.

45
00:02:45,240 --> 00:02:46,590
We could do this and get it.

46
00:02:46,920 --> 00:02:49,510
So simply let's let's just return this right here.

47
00:02:49,530 --> 00:02:50,430
So we'll return that.

48
00:02:50,820 --> 00:02:51,360
And then.

49
00:02:52,620 --> 00:03:05,280
Run it, then we are defined to get status which gets the email and then returns the user dictionary

50
00:03:06,570 --> 00:03:09,840
email and one.

51
00:03:09,990 --> 00:03:10,530
So that's it.

52
00:03:10,770 --> 00:03:13,740
So we could get his tattoos and we could get the idea.

53
00:03:14,940 --> 00:03:23,580
But before get into it or get into status, we want to be sure that this user is actually in our database.

54
00:03:23,730 --> 00:03:34,080
So we could create a function authenticate which gives the email and then which returns the.

55
00:03:36,460 --> 00:03:36,970
Bullion.

56
00:03:36,970 --> 00:03:42,310
We see as whether the user is in the platform or not or in the database or not.

57
00:03:42,310 --> 00:03:43,330
So we take the email.

58
00:03:43,570 --> 00:03:44,950
So we have email.

59
00:03:44,980 --> 00:03:49,390
We reach an email in, use a dictionary.

60
00:03:50,290 --> 00:03:58,930
Now, if you if you put, for example, e, let's say we have E and Gmail that come in user dictionary.

61
00:04:01,630 --> 00:04:02,140
See we have.

62
00:04:02,140 --> 00:04:02,580
True.

63
00:04:02,860 --> 00:04:03,820
If we put this.

64
00:04:04,840 --> 00:04:05,620
We have false.

65
00:04:05,950 --> 00:04:07,300
So basically what we're doing.

66
00:04:07,300 --> 00:04:08,050
I will return in.

67
00:04:08,050 --> 00:04:10,440
This is going to be a true or false.

68
00:04:10,440 --> 00:04:16,660
So this how we authenticate to be sure that the email we are passing in actually is in this dictionary.

69
00:04:17,980 --> 00:04:21,490
And since for the get idea and it gets choose, we need to do this authentication.

70
00:04:22,300 --> 00:04:23,920
We have to come in here.

71
00:04:24,700 --> 00:04:37,870
Let's, let's just define this before so we get in here and then we see if if authenticate email we

72
00:04:37,870 --> 00:04:38,290
return.

73
00:04:38,290 --> 00:04:42,460
This else we return.

74
00:04:45,510 --> 00:04:48,660
Use a not phone.

75
00:04:49,500 --> 00:04:50,220
So there we go.

76
00:04:50,520 --> 00:04:54,610
Now we do the same for the get stitches.

77
00:04:54,630 --> 00:04:55,800
Yeah, we have.

78
00:04:57,050 --> 00:05:04,070
If or then to ticket as the email.

79
00:05:05,950 --> 00:05:13,180
We return that else you return user, not phone.

80
00:05:15,640 --> 00:05:19,180
Now, suppose we have a hundred of such methods like this.

81
00:05:19,190 --> 00:05:20,520
Get statutes and get it.

82
00:05:21,130 --> 00:05:23,950
Where we'll be making use of the authenticate function.

83
00:05:24,520 --> 00:05:32,020
You'll see that this becomes very inefficient as we repeat in this same piece of code every time.

84
00:05:32,260 --> 00:05:37,540
The way we could solve this problem is by creating another function.

85
00:05:38,330 --> 00:05:38,850
See?

86
00:05:39,100 --> 00:05:41,260
Decorate those and set this below.

87
00:05:41,560 --> 00:05:42,310
Create this function.

88
00:05:42,310 --> 00:05:42,910
Decorate

89
00:05:45,580 --> 00:05:49,420
which takes in any of these functions.

90
00:05:50,800 --> 00:05:56,110
So we could pass a function in this function and the email address.

91
00:05:56,140 --> 00:06:00,100
So we have a function we also pass in the email address.

92
00:06:00,910 --> 00:06:04,960
Then in this decorate function, we do the authentication.

93
00:06:05,260 --> 00:06:15,550
So we have we call on the authenticate method, we have the email address, and then we have the function

94
00:06:15,550 --> 00:06:18,670
which could be say again ID of email now called.

95
00:06:19,330 --> 00:06:25,690
So what we would do here is since we have an oh, since our decorate method is taking in another method

96
00:06:25,690 --> 00:06:32,050
which could be any of this methods here we're going to have f so it would just be any of this methods.

97
00:06:32,980 --> 00:06:38,830
F Awkward, especially right mentor method.

98
00:06:39,460 --> 00:06:43,120
So this takes in the matter and then this method is called on email.

99
00:06:44,140 --> 00:06:52,540
But before calling this email, we could put this ID or this odd boolean we see whether it's authenticated

100
00:06:52,540 --> 00:06:52,900
or not.

101
00:06:53,290 --> 00:06:55,210
And so every time we just have art.

102
00:06:56,890 --> 00:06:57,760
So yeah, we have.

103
00:06:59,940 --> 00:07:04,740
If art and yeah we have these art.

104
00:07:07,040 --> 00:07:09,810
Now that we've passed in the hour before the emails.

105
00:07:09,810 --> 00:07:11,330
So yeah, we need to modify this.

106
00:07:12,200 --> 00:07:12,770
There we go.

107
00:07:13,010 --> 00:07:18,560
So now we have all we were working with this decorate method which takes an unknown method.

108
00:07:19,580 --> 00:07:23,570
We ran this and yeah, we could return known.

109
00:07:24,410 --> 00:07:25,580
So that's it.

110
00:07:28,670 --> 00:07:29,990
All we do is return.

111
00:07:29,990 --> 00:07:31,340
We can return matter actually.

112
00:07:31,790 --> 00:07:32,940
So we have a good return.

113
00:07:33,170 --> 00:07:37,160
Thoughts our email.

114
00:07:37,220 --> 00:07:38,120
Let's take this off.

115
00:07:38,270 --> 00:07:43,320
We can get a good output of the particular matter.

116
00:07:44,060 --> 00:07:44,980
Let's take this off.

117
00:07:44,990 --> 00:07:55,730
We have the great obviously get it and I'll get it takes in the art and then the email.

118
00:07:56,360 --> 00:08:06,140
But then as you could see from this, the art is not yet defined because this art is defined in year

119
00:08:07,190 --> 00:08:09,380
after calendar authenticate method.

120
00:08:11,270 --> 00:08:19,430
This means that we need to modify the way we've designed those methods, which will permit us call on

121
00:08:19,430 --> 00:08:26,690
the get it all caught on the edges or whatever matter while taking into consideration the authentication.

122
00:08:28,520 --> 00:08:33,740
And to do this, we need to get rid of the fact that we need to actually pass in this odd inside each

123
00:08:33,740 --> 00:08:35,030
and every one of these methods.

124
00:08:35,450 --> 00:08:39,530
So we need to modify this get idea, for example, modifying this, get it.

125
00:08:40,760 --> 00:08:42,020
I'm not taking into art.

126
00:08:42,350 --> 00:08:45,110
Simple means if we have this authenticated, right?

127
00:08:45,110 --> 00:08:46,250
Yeah, we would have.

128
00:08:46,640 --> 00:08:53,810
If the art is as if the user or the email is kind of is in the database.

129
00:08:54,170 --> 00:08:58,150
So if art, we return the matter.

130
00:09:00,540 --> 00:09:08,460
Email and else return the user.

131
00:09:11,200 --> 00:09:12,160
Not fun.

132
00:09:14,560 --> 00:09:15,160
So that's it.

133
00:09:15,520 --> 00:09:17,840
Now we're going to return this return.

134
00:09:18,250 --> 00:09:21,520
Then in here, we don't need to return this anymore.

135
00:09:21,550 --> 00:09:22,890
We just have to return to.

136
00:09:25,980 --> 00:09:26,760
Dictionary.

137
00:09:27,180 --> 00:09:27,750
All right.

138
00:09:27,840 --> 00:09:30,180
The I.D. for this case.

139
00:09:30,750 --> 00:09:36,300
So, yeah, we we tend to I.D. and, yeah, we return the stitches.

140
00:09:37,950 --> 00:09:39,030
So there we go.

141
00:09:39,800 --> 00:09:40,770
Think this might work?

142
00:09:41,280 --> 00:09:41,940
And that was fine.

143
00:09:42,240 --> 00:09:44,400
Now we have this idea of the stitches.

144
00:09:44,640 --> 00:09:49,920
So, yeah, we're going to have if not, we get into the matter or we take this into consideration.

145
00:09:50,340 --> 00:09:52,770
Else, we have the user on our phone.

146
00:09:53,070 --> 00:10:01,200
So we'll see now that we are creating this two methods without having to always define or always do

147
00:10:01,200 --> 00:10:08,790
authentication in each of these methods, because we are able to create this decorate function, which

148
00:10:08,790 --> 00:10:10,140
does authentication.

149
00:10:10,470 --> 00:10:15,930
And if there's authentication against the method, if it's identifiable against the method else we're

150
00:10:15,930 --> 00:10:18,120
going to use on the phone, so now let's run again.

151
00:10:19,050 --> 00:10:23,890
This time around, we take this email to be c e gmail.com.

152
00:10:24,480 --> 00:10:26,160
That gives off a run.

153
00:10:28,030 --> 00:10:32,470
We actually have to separate this like this, and that's fine.

154
00:10:32,830 --> 00:10:33,190
Okay.

155
00:10:33,190 --> 00:10:36,190
Yeah, we have the ID given to us.

156
00:10:37,240 --> 00:10:43,180
Obviously, this has been authenticated and then because he has been authenticated, we are able to

157
00:10:43,480 --> 00:10:48,730
obtain the ID from the dictionaries or from the dictionary.

158
00:10:49,450 --> 00:10:53,100
Now we get into the second example to this.

159
00:10:53,110 --> 00:10:56,110
We have user not found so that everything works fine.

160
00:10:57,820 --> 00:11:07,290
Now, if we look at this way of making our calendars get 80 Aussie guess digits, we have this error.

161
00:11:07,300 --> 00:11:08,490
Let's take of this.

162
00:11:08,500 --> 00:11:08,920
Yeah.

163
00:11:09,580 --> 00:11:16,750
If we look at this way of calling, get I.D. or against digits, that's a good idea.

164
00:11:19,560 --> 00:11:25,180
We will see that this doesn't actually look like this is a method call.

165
00:11:25,630 --> 00:11:27,520
It doesn't look like we have in this matter.

166
00:11:27,850 --> 00:11:34,000
And then this method is having as parameter or argument this email.

167
00:11:34,510 --> 00:11:38,380
So let's modify again this decorate matter right here.

168
00:11:39,250 --> 00:11:41,350
To do that, we are going to create a wrapper.

169
00:11:41,470 --> 00:11:42,700
Let's take this here.

170
00:11:42,970 --> 00:11:45,340
We're going to create a wrapper function here.

171
00:11:45,820 --> 00:11:48,580
So we have this wrapper function we fixed in the email.

172
00:11:49,660 --> 00:11:51,670
Now we don't need to specify this email again.

173
00:11:51,670 --> 00:11:53,170
Here we have the matter.

174
00:11:53,170 --> 00:11:59,890
We have this wrapper function takes in the email and then it actually does a job.

175
00:12:01,030 --> 00:12:05,590
But when we get into this decorate method, we simply return the wrapper.

176
00:12:07,840 --> 00:12:09,240
So yeah, we have to decorate.

177
00:12:09,250 --> 00:12:16,870
We get into this, we tend to wrap out the rabbit, takes an email and then it goes or does this as

178
00:12:16,870 --> 00:12:17,380
usual.

179
00:12:17,950 --> 00:12:19,150
So let's run this again.

180
00:12:19,780 --> 00:12:21,190
We have that now.

181
00:12:21,190 --> 00:12:23,050
Let's turn this off.

182
00:12:24,970 --> 00:12:31,690
Better still, let's call it so let's let's have this and then close here.

183
00:12:32,500 --> 00:12:39,190
So now it looks like we have in this method which is calling, which is taking this argument, this

184
00:12:39,190 --> 00:12:46,570
email address, this command is from now on, I guess the output we get, the next modification we can

185
00:12:46,610 --> 00:12:51,370
make in order to get the right output is by taking this off.

186
00:12:51,640 --> 00:12:52,090
Yeah.

187
00:12:52,210 --> 00:13:00,550
So now we have an decorate we have in the function itself and then we having this get start.

188
00:13:00,550 --> 00:13:04,150
So we have this variable against that and then we call on gets that.

189
00:13:04,690 --> 00:13:15,880
So inside this now we have we, we have the E general account for example so on that and there we go.

190
00:13:16,120 --> 00:13:26,020
So this how we create a function our method decorator which permits us carry out the authentication

191
00:13:27,730 --> 00:13:35,200
without having to define authentication for all the different methods which need this authentication

192
00:13:35,200 --> 00:13:35,740
defined.

193
00:13:36,190 --> 00:13:42,940
So if we have 100 methods, we just need to define this method now and we'll let this decorator function,

194
00:13:43,270 --> 00:13:45,310
do the authentication job.

195
00:13:46,120 --> 00:13:54,250
Then another modification we could do to the syntax is by simply add in your this decorate decorator

196
00:13:54,250 --> 00:13:54,760
actually.

197
00:13:56,200 --> 00:14:04,030
So that is decorator decorate run this and now we don't, we don't need to call this as we don't need

198
00:14:04,030 --> 00:14:07,480
to pass this method in the decorate method.

199
00:14:07,480 --> 00:14:11,320
Again, all we need to do is just to say, okay, we have this status.

200
00:14:11,920 --> 00:14:17,590
So basically now we have get stitches, we take this off now we call, we have gets digits, we call

201
00:14:17,590 --> 00:14:19,030
this and that's fine.

202
00:14:19,510 --> 00:14:22,360
So this is actually the end point.

203
00:14:22,360 --> 00:14:25,260
We are able to define this all.

204
00:14:25,270 --> 00:14:31,810
We are able to call on this method as in the email address and then the authentication is done automatically

205
00:14:32,230 --> 00:14:34,400
if we have some staff here.

206
00:14:34,420 --> 00:14:41,260
So using the phone without having to explicitly define authentication in each and every method.

207
00:14:41,260 --> 00:14:41,580
Yeah.

208
00:14:42,040 --> 00:14:52,340
So now we could have get I.D., we have email, get ID, let's pass in C be gmail.com.

209
00:14:52,360 --> 00:14:58,690
We have these 8039 which is this now we could define in the method let's insert below.

210
00:14:59,380 --> 00:15:05,710
Let's define the matter which you shall decorate will define the method, get user name.

211
00:15:06,610 --> 00:15:18,670
So we have this method, get user name taking the email and then we return the user dictionary dictionary

212
00:15:19,360 --> 00:15:20,140
email.

213
00:15:22,150 --> 00:15:23,320
We're take two year.

214
00:15:23,470 --> 00:15:25,930
So for each of this we're going to define a user name.

215
00:15:25,930 --> 00:15:27,880
So yeah, we can have a user name.

216
00:15:27,880 --> 00:15:31,090
ee1.

217
00:15:34,920 --> 00:15:35,190
Yeah.

218
00:15:35,190 --> 00:15:37,980
We have the one.

219
00:15:39,870 --> 00:15:52,650
Yeah we have see one and then yeah we have D one, so we're on that and now we could simply do a call

220
00:15:52,650 --> 00:15:59,130
here we have that user name of the original com.

221
00:16:00,030 --> 00:16:00,600
So that's it.

222
00:16:00,870 --> 00:16:06,930
We now are able to do this without having to define the authentication.

223
00:16:06,930 --> 00:16:11,670
We could do this too and we have using that phone one last modification.

224
00:16:12,960 --> 00:16:18,060
We may have a case where we will not only be taking in the email, yeah, we may take in the email and

225
00:16:18,060 --> 00:16:19,650
some other parameter.

226
00:16:19,980 --> 00:16:27,210
So we don't always want to have those same exact parameters get into the ID or into the get it matter

227
00:16:27,210 --> 00:16:30,960
the same get into the gets digits all the same, get into the get user name.

228
00:16:31,290 --> 00:16:36,210
So in that case, we could have the two parameters getting in like this.

229
00:16:36,450 --> 00:16:41,640
And to solve that problem, all we need to do now is instead of specifying that we're going to take

230
00:16:41,640 --> 00:16:46,800
the email in this case, it's suffice to specify like this, but it's not specifying that we need to

231
00:16:46,800 --> 00:16:48,090
get only the email.

232
00:16:48,120 --> 00:16:48,310
Now.

233
00:16:48,360 --> 00:16:56,940
We could have the arguments and the keyword arguments passed, so this would permit us specify an unknown

234
00:16:56,940 --> 00:16:58,140
number of parameters.

235
00:16:58,710 --> 00:17:06,540
So it would make our code make us to do this declaration now in a more flexible manner.

236
00:17:07,170 --> 00:17:08,040
So there we go.

237
00:17:08,040 --> 00:17:11,130
We now replace this email with that and that's fine.

238
00:17:11,490 --> 00:17:18,900
So running this is use and have the same results are we have error because we took this parameter in

239
00:17:19,170 --> 00:17:25,650
the graph and I see we have the same results in this get user name.

240
00:17:25,650 --> 00:17:25,920
Yeah.

241
00:17:25,920 --> 00:17:31,110
We could define another parameter C the okay parameter, I would define it to be okay.

242
00:17:32,580 --> 00:17:38,590
So we'll just simply add that, okay, let's run this user not defined this.

243
00:17:38,700 --> 00:17:42,630
I have a C, we have a one the use a name and then we've attached.

244
00:17:42,630 --> 00:17:48,990
Okay, so this works now because we are able to parse in many more parameters in this method.
