WEBVTT

1
00:00.000 --> 00:03.310
So friends, the last thing that I just want to briefly talk to you about is the

2
00:03.310 --> 00:03.840
concept

3
00:03.840 --> 00:05.920
of functions within Python.

4
00:05.920 --> 00:09.750
Now understand, within programming, we have this concept that's known as this,

5
00:09.750 --> 00:10.480
the dry

6
00:10.480 --> 00:11.800
concept.

7
00:11.800 --> 00:13.640
Don't repeat yourself.

8
00:13.640 --> 00:16.850
So quite simply, let's say you have a piece of code that is really quite

9
00:16.850 --> 00:17.520
valuable at

10
00:17.520 --> 00:20.900
something you want to use over and over and over again, you don't want to have

11
00:20.900 --> 00:21.720
to continually

12
00:21.720 --> 00:25.360
rewrite the same logic over and over and over again.

13
00:25.360 --> 00:29.640
This is something you want to avoid because you want to not repeat yourself as

14
00:29.640 --> 00:30.320
specified
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

14
00:29.640 --> 00:30.320
specified

15
00:30.320 --> 00:31.320
via the dry principle.

16
00:31.320 --> 00:36.020
Now quite simply, with respect to a function, all it does is it gives us a way

17
00:36.020 --> 00:37.080
to reuse code

18
00:37.080 --> 00:38.760
any nice easy manner.

19
00:38.760 --> 00:42.690
In fact, to let you in in a little bit of a secret, we have already been using

20
00:42.690 --> 00:43.640
some built

21
00:43.640 --> 00:45.480
in functions already.

22
00:45.480 --> 00:46.760
So let's take this example here.

23
00:46.760 --> 00:52.360
If I type the keyword print and I just say, you know, my name is John.

24
00:52.360 --> 00:56.350
If you actually thought about what's happening here, we simply pass in this

25
00:56.350 --> 00:57.080
value and then

26
00:57.080 --> 01:02.150
specify this keyword print and then suddenly when we run the actual script, the
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

26
00:57.080 --> 01:02.150
specify this keyword print and then suddenly when we run the actual script, the

27
01:02.150 --> 01:02.840
text inside

28
01:02.840 --> 01:04.640
happens to get printed out.

29
01:04.640 --> 01:07.840
So what we're doing here, in fact, if I just hover over it, you can see in

30
01:07.840 --> 01:08.720
brackets, this

31
01:08.720 --> 01:09.720
is a function.

32
01:09.720 --> 01:13.730
Now the reason why this is a function is because very, very often within our

33
01:13.730 --> 01:14.960
Python code,

34
01:14.960 --> 01:16.520
we want to be able to print something out.

35
01:16.520 --> 01:18.760
This is something that happens a lot.

36
01:18.760 --> 01:22.740
And so for the Python developers have made it a built in function, so you don't

37
01:22.740 --> 01:23.200
have to

38
01:23.200 --> 01:26.360
write the code in order to print things to your terminal.

39
01:26.360 --> 01:30.170
You just reference the actual function name, which in this case here is simply
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

39
01:26.360 --> 01:30.170
You just reference the actual function name, which in this case here is simply

40
01:30.170 --> 01:30.640
called

41
01:30.640 --> 01:31.640
print.

42
01:31.640 --> 01:34.840
And then you pass in a particular value to that print function and the print

43
01:34.840 --> 01:35.400
function is

44
01:35.400 --> 01:38.040
going to handle all of the underlying details.

45
01:38.040 --> 01:41.640
So we have these built in functions, but what we can also do is create our own

46
01:41.640 --> 01:43.000
custom functions.

47
01:43.000 --> 01:46.360
Now the way we do this is by utilizing the keyword def.

48
01:46.360 --> 01:49.500
This is how we define a function and then we can give the function a name,

49
01:49.500 --> 01:50.200
which is called

50
01:50.200 --> 01:51.200
this Adder.

51
01:51.200 --> 01:52.200
Okay.

52
01:52.200 --> 01:55.870
Now what I will do here is within my parentheses, I'm going to specify two

53
01:55.870 --> 01:57.040
particular arguments

54
01:57.040 --> 02:00.070
that I'm going to accept into this function pretty much data I'm going to work
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

54
01:57.040 --> 02:00.070
that I'm going to accept into this function pretty much data I'm going to work

55
02:00.070 --> 02:00.520
with.

56
02:00.520 --> 02:03.520
I will say num1 as well as num2.

57
02:03.520 --> 02:07.590
And then I'll have my colon and then indented within, we specify the logic that

58
02:07.590 --> 02:08.080
we want to

59
02:08.080 --> 02:09.080
use.

60
02:09.080 --> 02:12.040
So inside the function, I will just create a variable called total number.

61
02:12.040 --> 02:16.520
And I'll just make that equal to num1 plus num2.

62
02:16.520 --> 02:18.960
And then all I will do is print out the total number.

63
02:18.960 --> 02:22.640
So the cool thing is just like with the print function, we pass in some type of

64
02:22.640 --> 02:23.280
statement,

65
02:23.280 --> 02:25.120
you know, this is a test.

66
02:25.120 --> 02:26.560
And this would simply be printed.

67
02:26.560 --> 02:30.650
In our case here, what I will do is just simply call the Adder function and I
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

67
02:26.560 --> 02:30.650
In our case here, what I will do is just simply call the Adder function and I

68
02:30.650 --> 02:31.120
'll pass

69
02:31.120 --> 02:33.040
in the two numbers I want to add.

70
02:33.040 --> 02:36.240
So I want to add, let's say the number two to the number seven.

71
02:36.240 --> 02:41.340
So if I just save this here and I run example five dot p y and to enter, check

72
02:41.340 --> 02:41.720
this out

73
02:41.720 --> 02:42.720
right here.

74
02:42.720 --> 02:46.140
We are actually adding the two values past in there because we have defined the

75
02:46.140 --> 02:47.200
logic once,

76
02:47.200 --> 02:50.660
as we can see right here, we can just easily reference that logic just by

77
02:50.660 --> 02:51.480
giving the name

78
02:51.480 --> 02:52.480
of the function.

79
02:52.480 --> 02:56.340
So if I want to add more numbers such as, you know, one in three, save you,

80
02:56.340 --> 02:57.080
then I don't

81
02:57.080 --> 02:59.080
have to rewrite the same logic all over again.

82
02:59.080 --> 03:02.480
I can just run the script, call the function to different times.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

82
02:59.080 --> 03:02.480
I can just run the script, call the function to different times.

83
03:02.480 --> 03:05.200
And now I've got this nice piece of reusable code.

84
03:05.200 --> 03:08.560
Now similarly, what we could do is create a custom function and we can do it to

85
03:08.560 --> 03:08.960
check

86
03:08.960 --> 03:10.680
the class of an IP address.

87
03:10.680 --> 03:13.840
So what I will do is call it check class and I'm going to accept in an IP

88
03:13.840 --> 03:14.520
address.

89
03:14.520 --> 03:17.760
Now what I will do is inside this function, I will use some conditional logic.

90
03:17.760 --> 03:22.740
I will see if the IP adder starts with, I'll say 10 dot, what I will do is just

91
03:22.740 --> 03:24.040
print out.

92
03:24.040 --> 03:28.440
This is a class A address and just follow the same logic we did before.

93
03:28.440 --> 03:34.040
LIF, the IP adder starts with 172 dot, what I'll do is print.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

93
03:28.440 --> 03:34.040
LIF, the IP adder starts with 172 dot, what I'll do is print.

94
03:34.040 --> 03:36.800
This is a class B address.

95
03:36.800 --> 03:40.330
Now what I could do is use an L statement here to print out the class C address

96
03:40.330 --> 03:40.600
as we

97
03:40.600 --> 03:43.360
did before, but this is not actually the best thing.

98
03:43.360 --> 03:44.360
Let me show you why.

99
03:44.360 --> 03:46.400
So this is a class C address.

100
03:46.400 --> 03:51.040
So if I save this, let me just delete the adder one here and remove this.

101
03:51.040 --> 03:52.040
We don't need that.

102
03:52.040 --> 03:56.940
So if I just say check class and I pass in, let's go with the address of 10, 0,

103
03:56.940 --> 03:58.240
0, 5.

104
03:58.240 --> 03:59.240
Okay.

105
03:59.240 --> 04:00.760
So let's run these scripts.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

105
03:59.240 --> 04:00.760
So let's run these scripts.

106
04:00.760 --> 04:02.640
We see here this is a class A address.

107
04:02.640 --> 04:05.860
If I want to run the function again and this time we'll test out or I don't

108
04:05.860 --> 04:06.400
know let's

109
04:06.400 --> 04:10.040
save 19 to 168 45 dot one.

110
04:10.040 --> 04:11.360
Let's see what that one is.

111
04:11.360 --> 04:12.360
Let's run this.

112
04:12.360 --> 04:14.820
The first one is a class A address and it says here the second one is a class C

113
04:14.820 --> 04:15.240
address.

114
04:15.240 --> 04:16.840
Now this seems to be okay.

115
04:16.840 --> 04:23.520
And similarly if we happen to pass in 172 161 dot one, save you run it again.

116
04:23.520 --> 04:26.660
We can see the first one is class A, second one is class B. This appears to

117
04:26.660 --> 04:27.800
work correctly.

118
04:27.800 --> 04:31.460
Now just a caution about utilizing an L statement because it is a default
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

118
04:27.800 --> 04:31.460
Now just a caution about utilizing an L statement because it is a default

119
04:31.460 --> 04:32.440
statement and it's

120
04:32.440 --> 04:37.360
going to execute absolutely every time if all of the prior conditions happen to

121
04:37.360 --> 04:38.200
be false

122
04:38.200 --> 04:40.720
and just be aware that that is the logic that you're using.

123
04:40.720 --> 04:42.040
Let me give you an example here.

124
04:42.040 --> 04:48.850
If I say check class, if I pass in the address and I go with 2001, DB8, ABCD,

125
04:48.850 --> 04:49.920
colon colon

126
04:49.920 --> 04:53.120
one, now this happens to be an IPv6 address.

127
04:53.120 --> 04:54.120
Now check this out.

128
04:54.120 --> 04:58.520
If I run this logic right here and enter, it says here that the last one is a

129
04:58.520 --> 04:59.240
class C

130
04:59.240 --> 05:03.000
address, even though it's actually not, it's an IPv6 address and this is just
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

130
04:59.240 --> 05:03.000
address, even though it's actually not, it's an IPv6 address and this is just

131
05:03.000 --> 05:03.600
something

132
05:03.600 --> 05:04.600
to watch out for.

133
05:04.600 --> 05:08.640
So this part here is false as is this part here is false.

134
05:08.640 --> 05:12.910
So this is always going to print out this particular statement, even though in

135
05:12.910 --> 05:13.480
our case

136
05:13.480 --> 05:15.240
here, this is not actually true.

137
05:15.240 --> 05:18.390
So what we could do instead and I just want to highlight that you can do this

138
05:18.390 --> 05:19.520
in your logic.

139
05:19.520 --> 05:22.320
You don't have to end every F block with an L statement.

140
05:22.320 --> 05:27.280
You can just use more LF statements so I can say F, IP adder starts with 190.

141
05:27.280 --> 05:32.600
Then I can print out this as a class C address.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

141
05:27.280 --> 05:32.600
Then I can print out this as a class C address.

142
05:32.600 --> 05:39.040
So if I add in a new address of 192168, 31.1 and I run this now, hit enter.

143
05:39.040 --> 05:41.720
As we can see here, everything is being correctly classified.

144
05:41.720 --> 05:44.950
Now just for posterity, what I can do is add an else clause that's a little bit

145
05:44.950 --> 05:46.200
more appropriate

146
05:46.200 --> 05:47.600
and this is more of a default.

147
05:47.600 --> 05:50.040
Just say I'm not sure what the class is.

148
05:50.040 --> 05:55.260
So that means if you do happen to add in, let's maybe say a 2001 DB8 colon

149
05:55.260 --> 05:56.120
colon one

150
05:56.120 --> 05:59.200
and IP 6 address, arrow up, hit enter.

151
05:59.200 --> 06:02.480
We can see here the last one we have a way to handle any other data.
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

151
05:59.200 --> 06:02.480
We can see here the last one we have a way to handle any other data.

152
06:02.480 --> 06:04.320
Now crucially, look what happened.

153
06:04.320 --> 06:07.980
In this particular function right here, we actually modified the logic a few

154
06:07.980 --> 06:08.520
times.

155
06:08.520 --> 06:12.560
But the cool thing is, is that we only had to modify it in one place.

156
06:12.560 --> 06:16.910
And all we do is simply reference that one place to reuse the same logic over

157
06:16.910 --> 06:17.360
and over

158
06:17.360 --> 06:18.360
and over again.

159
06:18.360 --> 06:21.470
So you can imagine if instead of actually using this particular function, we

160
06:21.470 --> 06:22.000
wrote out

161
06:22.000 --> 06:26.330
this entire logic for every single time you wanted to test out an IP address,

162
06:26.330 --> 06:27.120
then similarly

163
06:27.120 --> 06:31.790
every single time you wanted to update a logic such as adding in this new LF
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

163
06:27.120 --> 06:31.790
every single time you wanted to update a logic such as adding in this new LF

164
06:31.790 --> 06:32.600
statement

165
06:32.600 --> 06:34.600
and this final else clause.

166
06:34.600 --> 06:38.860
And we would have to rewrite that logic every single time we used it, whereas

167
06:38.860 --> 06:39.560
instead if

168
06:39.560 --> 06:42.680
we just create a function, we write the logic ones and then just can

169
06:42.680 --> 06:43.920
continually reference

170
06:43.920 --> 06:44.920
it.

171
06:44.920 --> 06:48.510
So really this particular concept within Python, it is very, very common to use

172
06:48.510 --> 06:49.040
all over

173
06:49.040 --> 06:51.000
the place within network automation.

174
06:51.000 --> 06:54.180
So when you happen to see that, we would keyword of death, just understand that

175
06:54.180 --> 06:54.800
that's all

176
06:54.800 --> 06:55.800
we're doing.

177
06:55.800 --> 06:59.590
We're just creating a nice piece of reusable codes just to keep things neater,

178
06:59.590 --> 07:00.720
more manageable,
WEBVTT
X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000

178
06:59.590 --> 07:00.720
more manageable,

179
07:00.720 --> 07:03.480
more portable and to make your lives a little bit easier.
