1
00:00:01,080 --> 00:00:02,160
Instructor: Sometimes in your code,

2
00:00:02,160 --> 00:00:05,790
you might have the need for some logging to happen.

3
00:00:05,790 --> 00:00:07,110
In this code exercise,

4
00:00:07,110 --> 00:00:09,110
we're going to write a logging_decorator

5
00:00:10,050 --> 00:00:12,120
and it's going to print the name

6
00:00:12,120 --> 00:00:13,860
of the function that was called,

7
00:00:13,860 --> 00:00:15,570
the arguments that was given

8
00:00:15,570 --> 00:00:19,200
and also the returned output of that function.

9
00:00:19,200 --> 00:00:20,790
Take a look in the description pane

10
00:00:20,790 --> 00:00:23,820
and you'll see an example of what we want the output

11
00:00:23,820 --> 00:00:25,080
to look like.

12
00:00:25,080 --> 00:00:27,030
Without changing the body

13
00:00:27,030 --> 00:00:30,300
of the function that we've called a_function

14
00:00:30,300 --> 00:00:33,390
on lines eight and nine, create a decorator

15
00:00:33,390 --> 00:00:37,860
to decorate that function and be able to log what you see

16
00:00:37,860 --> 00:00:39,840
in the example output.

17
00:00:39,840 --> 00:00:40,920
As before, you remember,

18
00:00:40,920 --> 00:00:44,200
you can use function.__name__

19
00:00:45,270 --> 00:00:46,680
to get the name of the function

20
00:00:46,680 --> 00:00:51,680
and you can use the args to get hold of the arguments.

21
00:00:52,410 --> 00:00:57,410
So create this decorator, decorate the necessary functions

22
00:00:57,750 --> 00:01:02,313
and see if you manage to achieve the desired output.

23
00:01:09,840 --> 00:01:13,110
First thing first is to define our decorator

24
00:01:13,110 --> 00:01:15,360
and I've called it logging_decorator

25
00:01:15,360 --> 00:01:19,170
that takes an input, which I'll call fn,

26
00:01:19,170 --> 00:01:20,760
which is short for function.

27
00:01:20,760 --> 00:01:22,460
You can call it whatever you like.

28
00:01:23,850 --> 00:01:26,760
So next, let's define our wrapper function.

29
00:01:26,760 --> 00:01:30,600
It's going to take the arguments that come in

30
00:01:30,600 --> 00:01:32,730
and it's going to print

31
00:01:32,730 --> 00:01:36,270
that you called the function.name,

32
00:01:36,270 --> 00:01:39,240
and then we're going to insert the arguments

33
00:01:39,240 --> 00:01:41,040
that were passed in.

34
00:01:41,040 --> 00:01:45,210
That way we can log to the user which function they called

35
00:01:45,210 --> 00:01:47,250
and which arguments were passed in,

36
00:01:47,250 --> 00:01:49,650
which as you can imagine sometimes for debugging

37
00:01:49,650 --> 00:01:52,380
that this might be quite handy to know.

38
00:01:52,380 --> 00:01:54,840
Next, we get hold of the result

39
00:01:54,840 --> 00:01:57,810
of running our function, which is decorated.

40
00:01:57,810 --> 00:02:00,436
So we have our function being passed in

41
00:02:00,436 --> 00:02:05,436
and we pass in the arguments at zero, one and two.

42
00:02:06,450 --> 00:02:09,060
So in this case, we only have arguments

43
00:02:09,060 --> 00:02:13,200
and remember that when we use args, we can get a list

44
00:02:13,200 --> 00:02:17,220
of all of the inputs that were passed into the function.

45
00:02:17,220 --> 00:02:19,500
So in this case, we take a look in the input pane.

46
00:02:19,500 --> 00:02:21,570
We can see there are three inputs.

47
00:02:21,570 --> 00:02:24,000
We take the arguments at position zero, one and two,

48
00:02:24,000 --> 00:02:26,578
all three, pass it through the function,

49
00:02:26,578 --> 00:02:29,640
whichever function we're going to decorate in the future.

50
00:02:29,640 --> 00:02:32,790
And then we should end up with an output, which we assign

51
00:02:32,790 --> 00:02:34,920
to the variable results.

52
00:02:34,920 --> 00:02:37,560
And then we print it out and we say it returned

53
00:02:37,560 --> 00:02:40,440
and then we insert the result using an f string.

54
00:02:40,440 --> 00:02:42,723
And finally, we return the wrapper function.

55
00:02:43,980 --> 00:02:46,620
The last part is actually decorating our function,

56
00:02:46,620 --> 00:02:49,020
using the logging_decorator we created.

57
00:02:49,020 --> 00:02:53,640
So line 10, we add that decorator to a_function

58
00:02:53,640 --> 00:02:56,310
so that now every time a_function runs,

59
00:02:56,310 --> 00:02:59,140
it's going to run the logging_decorator

60
00:03:00,060 --> 00:03:02,850
and it's going to print out which function was called,

61
00:03:02,850 --> 00:03:04,500
which arguments were passed in

62
00:03:04,500 --> 00:03:07,593
and which results were returned.

63
00:03:08,490 --> 00:03:10,380
And if you run the code as it is,

64
00:03:10,380 --> 00:03:13,110
you should see the expected outcome.

65
00:03:13,110 --> 00:03:16,068
If you have anything you need to change in your code or fix

66
00:03:16,068 --> 00:03:19,350
or just to check, then head back to the previous slide

67
00:03:19,350 --> 00:03:21,573
and you can update your code as needed.

