Welcome to the section on Decorators.
Let's consider this example.
Suppose we have a system which permits users get their IDs based on their email address and also get
their subscription status based on the same email address.
So we could have a dictionary so we wouldn't have to use a dictionary.
Which is this?
Now we just fix out all.
We just fix some email addresses.
So say for example.
A at the moment.
Com And this agreement would come this linked to an idea of C two or three four and then to a subscription
status of true.
Then the next person could be c be copy of this.
You have that.
There we go.
That's fine.
So here we have B I gmail.com.
Come.
Then.
Yeah we have C 39 then.
So that is true.
True.
See we have that.
So for then, the status is false.
Then we have the the status is the ID.
That's the idea.
Zero.
And then yeah, the status is false.
Okay.
So we have this system or we have this database where we could link the email address to a corresponding
subscription status and ID could print out this user dictionary.
There we go.
We can now define two functions where in the first function permits us to get the ID based on the email
address.
So we have the ID based on the email.
We have user of email.
And then that's for firstly, let's see how we're going to get this.
We have this user dictionary right here.
So once you have the user dictionary, if we have an email, see at gmail.com, we see that put in.
This gives us this value right here.
Now, if we have D, for example, or we have FEC, this is key error because that's not the dictionary.
If we have the there we go.
Now, once we have this, we could select the ID or the subscription status.
So we just get added function.
We could do this and get it.
So simply let's let's just return this right here.
So we'll return that.
And then.
Run it, then we are defined to get status which gets the email and then returns the user dictionary
email and one.
So that's it.
So we could get his tattoos and we could get the idea.
But before get into it or get into status, we want to be sure that this user is actually in our database.
So we could create a function authenticate which gives the email and then which returns the.
Bullion.
We see as whether the user is in the platform or not or in the database or not.
So we take the email.
So we have email.
We reach an email in, use a dictionary.
Now, if you if you put, for example, e, let's say we have E and Gmail that come in user dictionary.
See we have.
True.
If we put this.
We have false.
So basically what we're doing.
I will return in.
This is going to be a true or false.
So this how we authenticate to be sure that the email we are passing in actually is in this dictionary.
And since for the get idea and it gets choose, we need to do this authentication.
We have to come in here.
Let's, let's just define this before so we get in here and then we see if if authenticate email we
return.
This else we return.
Use a not phone.
So there we go.
Now we do the same for the get stitches.
Yeah, we have.
If or then to ticket as the email.
We return that else you return user, not phone.
Now, suppose we have a hundred of such methods like this.
Get statutes and get it.
Where we'll be making use of the authenticate function.
You'll see that this becomes very inefficient as we repeat in this same piece of code every time.
The way we could solve this problem is by creating another function.
See?
Decorate those and set this below.
Create this function.
Decorate
which takes in any of these functions.
So we could pass a function in this function and the email address.
So we have a function we also pass in the email address.
Then in this decorate function, we do the authentication.
So we have we call on the authenticate method, we have the email address, and then we have the function
which could be say again ID of email now called.
So what we would do here is since we have an oh, since our decorate method is taking in another method
which could be any of this methods here we're going to have f so it would just be any of this methods.
F Awkward, especially right mentor method.
So this takes in the matter and then this method is called on email.
But before calling this email, we could put this ID or this odd boolean we see whether it's authenticated
or not.
And so every time we just have art.
So yeah, we have.
If art and yeah we have these art.
Now that we've passed in the hour before the emails.
So yeah, we need to modify this.
There we go.
So now we have all we were working with this decorate method which takes an unknown method.
We ran this and yeah, we could return known.
So that's it.
All we do is return.
We can return matter actually.
So we have a good return.
Thoughts our email.
Let's take this off.
We can get a good output of the particular matter.
Let's take this off.
We have the great obviously get it and I'll get it takes in the art and then the email.
But then as you could see from this, the art is not yet defined because this art is defined in year
after calendar authenticate method.
This means that we need to modify the way we've designed those methods, which will permit us call on
the get it all caught on the edges or whatever matter while taking into consideration the authentication.
And to do this, we need to get rid of the fact that we need to actually pass in this odd inside each
and every one of these methods.
So we need to modify this get idea, for example, modifying this, get it.
I'm not taking into art.
Simple means if we have this authenticated, right?
Yeah, we would have.
If the art is as if the user or the email is kind of is in the database.
So if art, we return the matter.
Email and else return the user.
Not fun.
So that's it.
Now we're going to return this return.
Then in here, we don't need to return this anymore.
We just have to return to.
Dictionary.
All right.
The I.D. for this case.
So, yeah, we we tend to I.D. and, yeah, we return the stitches.
So there we go.
Think this might work?
And that was fine.
Now we have this idea of the stitches.
So, yeah, we're going to have if not, we get into the matter or we take this into consideration.
Else, we have the user on our phone.
So we'll see now that we are creating this two methods without having to always define or always do
authentication in each of these methods, because we are able to create this decorate function, which
does authentication.
And if there's authentication against the method, if it's identifiable against the method else we're
going to use on the phone, so now let's run again.
This time around, we take this email to be c e gmail.com.
That gives off a run.
We actually have to separate this like this, and that's fine.
Okay.
Yeah, we have the ID given to us.
Obviously, this has been authenticated and then because he has been authenticated, we are able to
obtain the ID from the dictionaries or from the dictionary.
Now we get into the second example to this.
We have user not found so that everything works fine.
Now, if we look at this way of making our calendars get 80 Aussie guess digits, we have this error.
Let's take of this.
Yeah.
If we look at this way of calling, get I.D. or against digits, that's a good idea.
We will see that this doesn't actually look like this is a method call.
It doesn't look like we have in this matter.
And then this method is having as parameter or argument this email.
So let's modify again this decorate matter right here.
To do that, we are going to create a wrapper.
Let's take this here.
We're going to create a wrapper function here.
So we have this wrapper function we fixed in the email.
Now we don't need to specify this email again.
Here we have the matter.
We have this wrapper function takes in the email and then it actually does a job.
But when we get into this decorate method, we simply return the wrapper.
So yeah, we have to decorate.
We get into this, we tend to wrap out the rabbit, takes an email and then it goes or does this as
usual.
So let's run this again.
We have that now.
Let's turn this off.
Better still, let's call it so let's let's have this and then close here.
So now it looks like we have in this method which is calling, which is taking this argument, this
email address, this command is from now on, I guess the output we get, the next modification we can
make in order to get the right output is by taking this off.
Yeah.
So now we have an decorate we have in the function itself and then we having this get start.
So we have this variable against that and then we call on gets that.
So inside this now we have we, we have the E general account for example so on that and there we go.
So this how we create a function our method decorator which permits us carry out the authentication
without having to define authentication for all the different methods which need this authentication
defined.
So if we have 100 methods, we just need to define this method now and we'll let this decorator function,
do the authentication job.
Then another modification we could do to the syntax is by simply add in your this decorate decorator
actually.
So that is decorator decorate run this and now we don't, we don't need to call this as we don't need
to pass this method in the decorate method.
Again, all we need to do is just to say, okay, we have this status.
So basically now we have get stitches, we take this off now we call, we have gets digits, we call
this and that's fine.
So this is actually the end point.
We are able to define this all.
We are able to call on this method as in the email address and then the authentication is done automatically
if we have some staff here.
So using the phone without having to explicitly define authentication in each and every method.
Yeah.
So now we could have get I.D., we have email, get ID, let's pass in C be gmail.com.
We have these 8039 which is this now we could define in the method let's insert below.
Let's define the matter which you shall decorate will define the method, get user name.
So we have this method, get user name taking the email and then we return the user dictionary dictionary
email.
We're take two year.
So for each of this we're going to define a user name.
So yeah, we can have a user name.
ee1.
Yeah.
We have the one.
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
here we have that user name of the original com.
So that's it.
We now are able to do this without having to define the authentication.
We could do this too and we have using that phone one last modification.
We may have a case where we will not only be taking in the email, yeah, we may take in the email and
some other parameter.
So we don't always want to have those same exact parameters get into the ID or into the get it matter
the same get into the gets digits all the same, get into the get user name.
So in that case, we could have the two parameters getting in like this.
And to solve that problem, all we need to do now is instead of specifying that we're going to take
the email in this case, it's suffice to specify like this, but it's not specifying that we need to
get only the email.
Now.
We could have the arguments and the keyword arguments passed, so this would permit us specify an unknown
number of parameters.
So it would make our code make us to do this declaration now in a more flexible manner.
So there we go.
We now replace this email with that and that's fine.
So running this is use and have the same results are we have error because we took this parameter in
the graph and I see we have the same results in this get user name.
Yeah.
We could define another parameter C the okay parameter, I would define it to be okay.
So we'll just simply add that, okay, let's run this user not defined this.
I have a C, we have a one the use a name and then we've attached.
Okay, so this works now because we are able to parse in many more parameters in this method.