1
00:00:01,750 --> 00:00:08,950
I don't know if you have noticed it, but recently we have been working more on improving the code base

2
00:00:09,100 --> 00:00:12,520
rather than improving the output of the program.

3
00:00:14,700 --> 00:00:17,730
So specifically, we used functions.

4
00:00:17,730 --> 00:00:23,670
We integrated functions in our code, even though the code was also working without functions.

5
00:00:25,460 --> 00:00:29,960
The program was working because it's relatively a small program.

6
00:00:31,940 --> 00:00:38,690
But if the program gets bigger, then you really have to use functions to be able to keep your code

7
00:00:38,690 --> 00:00:39,620
organized.

8
00:00:41,290 --> 00:00:45,700
So whenever you want to create a process, just put it in a function.

9
00:00:47,860 --> 00:00:52,720
And in this video, we will go and work even further improving our codes.

10
00:00:53,890 --> 00:00:58,540
So let's you remember the example of the barista and the coffee.

11
00:00:59,050 --> 00:01:01,060
So let's talk about that again.

12
00:01:04,080 --> 00:01:05,760
So I have a question for you.

13
00:01:06,180 --> 00:01:11,460
When you ordered the coffee to the barista, where does the barista make the coffee?

14
00:01:13,880 --> 00:01:17,900
Does he or she make the coffee in your table?

15
00:01:17,900 --> 00:01:19,490
In the table you're sitting?

16
00:01:19,490 --> 00:01:26,690
Or does the barista have a separate space where they prepare the coffee?

17
00:01:29,400 --> 00:01:31,860
And the answer is, of course, they have a separate.

18
00:01:32,950 --> 00:01:33,850
Place for that.

19
00:01:33,850 --> 00:01:36,190
Otherwise things would get messy.

20
00:01:38,570 --> 00:01:41,210
And that's what we want to do here in our court as well.

21
00:01:41,240 --> 00:01:46,970
We want to separate the processes in a separate file.

22
00:01:48,380 --> 00:01:50,540
So currently we are in Maine.

23
00:01:50,540 --> 00:01:52,910
That pie, which is this file in here.

24
00:01:54,110 --> 00:02:00,980
What I'll do now is I will right click over the project directory, which is the same directory where

25
00:02:00,980 --> 00:02:03,110
main that p y is located.

26
00:02:03,110 --> 00:02:06,800
So I will right click here and create a new python file.

27
00:02:10,910 --> 00:02:13,320
You can name this file, anything you want.

28
00:02:13,340 --> 00:02:15,200
I would like to name it just.

29
00:02:18,510 --> 00:02:21,540
Functions on my charm.

30
00:02:21,540 --> 00:02:26,490
I don't have to supply the dot pie extension because Pie chan will add it by default.

31
00:02:26,490 --> 00:02:36,750
So that is a function that p y file and I'll just go to my main dot pie file and just cut these two

32
00:02:36,750 --> 00:02:37,560
functions.

33
00:02:40,900 --> 00:02:43,930
And pays them in functions that p one.

34
00:02:47,950 --> 00:02:55,360
And I would like to open this file to the right side so you can see both files so main that P were here

35
00:02:55,360 --> 00:02:57,310
functions that P in here.

36
00:02:58,090 --> 00:02:59,980
I'll close all these.

37
00:03:09,640 --> 00:03:13,150
No, I get all these red underlined.

38
00:03:14,810 --> 00:03:17,480
Elements such as the function call here.

39
00:03:17,480 --> 00:03:24,950
So it says are resolved to reference get to dos because this script now doesn't know anymore.

40
00:03:24,950 --> 00:03:29,060
What get to do is same for right to dos.

41
00:03:29,300 --> 00:03:33,400
So they are defined somewhere else and not in this script.

42
00:03:33,410 --> 00:03:35,870
We can fix that by.

43
00:03:37,830 --> 00:03:39,330
Saying from.

44
00:03:41,230 --> 00:03:42,490
Functions.

45
00:03:42,850 --> 00:03:43,990
Import.

46
00:03:44,560 --> 00:03:47,020
Get to dos.

47
00:03:47,290 --> 00:03:48,190
Comma.

48
00:03:48,670 --> 00:03:50,860
Write to dos.

49
00:03:54,300 --> 00:03:56,520
And now the red lines are gone.

50
00:03:58,110 --> 00:04:03,540
So if I run the script, it will behave exactly the same as it did before.

51
00:04:04,380 --> 00:04:05,580
Just show that to dos.

52
00:04:05,580 --> 00:04:06,750
And here they are.

53
00:04:08,370 --> 00:04:12,240
So why did I place the functions in another file?

54
00:04:14,010 --> 00:04:16,220
Well, this is just a common practice.

55
00:04:16,230 --> 00:04:22,260
It's a good practice to do because you want to organize the code when it gets bigger.

56
00:04:22,260 --> 00:04:30,210
So it's better to have logical parts of the code in separate files so that the code doesn't get extremely

57
00:04:30,210 --> 00:04:30,960
long.

58
00:04:31,590 --> 00:04:39,900
So in this case, it's it's logical to put functions so procedures and processes in one file and that

59
00:04:39,900 --> 00:04:42,000
simply import them in here.

60
00:04:45,860 --> 00:04:50,720
That is the syntax to import.

61
00:04:51,530 --> 00:04:54,810
Elements from another file to another file.

62
00:04:54,830 --> 00:04:56,480
Now, this will work.

63
00:04:57,650 --> 00:05:06,830
This syntax will work only when main that P and functions that P are in the same directory because they

64
00:05:06,830 --> 00:05:08,360
are in the same directory.

65
00:05:09,570 --> 00:05:10,560
This file.

66
00:05:11,240 --> 00:05:14,340
Everybody knows where to find this functions.

67
00:05:14,340 --> 00:05:18,300
So that is the name I gave to my dad's P file.

68
00:05:19,560 --> 00:05:22,390
So that has to reflect that file.

69
00:05:22,410 --> 00:05:27,840
And of course, these also have to reflect the names of the functions in here.

70
00:05:30,440 --> 00:05:38,270
So those from an import are keywords which are part of this syntax.

71
00:05:40,010 --> 00:05:43,010
Now, this is not the only way to import.

72
00:05:43,860 --> 00:05:44,850
Functions.

73
00:05:44,880 --> 00:05:49,980
Another way is to just say, Well, let me comment this.

74
00:05:50,190 --> 00:05:51,510
So another way.

75
00:05:52,950 --> 00:05:55,980
Is to say import functions.

76
00:05:57,600 --> 00:06:01,260
And then you see that these are again in underlines.

77
00:06:03,630 --> 00:06:08,090
Because these names, again, are not defined in the script.

78
00:06:08,100 --> 00:06:10,230
They are not mentioned anywhere.

79
00:06:13,320 --> 00:06:19,050
When we had that line, which is now commented out, so it's not recognised by Python, when we had

80
00:06:19,050 --> 00:06:25,410
that line that meant that these were available, they became available in the script.

81
00:06:25,410 --> 00:06:27,860
But now we have to follow a different approach.

82
00:06:27,870 --> 00:06:35,880
If you do here import functions than here, you want to say functions don't get to dos.

83
00:06:38,940 --> 00:06:43,800
And I guess now this also becomes a bit familiar, this syntax.

84
00:06:43,830 --> 00:06:46,230
It becomes familiar to you.

85
00:06:47,760 --> 00:06:50,910
So that's like the same syntax that you use.

86
00:06:53,160 --> 00:07:00,780
To call methods, for example, user action that strip where user action is a string and strip is the

87
00:07:00,780 --> 00:07:05,700
method which removes the spaces from this string.

88
00:07:06,780 --> 00:07:13,050
The difference here is that this is a string and this is a module.

89
00:07:14,100 --> 00:07:16,610
So it is it is this script.

90
00:07:16,620 --> 00:07:22,170
But when you import the scripts here, then you call it a module.

91
00:07:22,230 --> 00:07:23,580
That's the terminology.

92
00:07:23,580 --> 00:07:24,240
So.

93
00:07:26,210 --> 00:07:32,900
In other words, when we are in this mean that P word scripts and we import that other scripts, we

94
00:07:32,900 --> 00:07:34,730
refer to that as the module.

95
00:07:36,500 --> 00:07:39,200
Same thing like in the coffee example.

96
00:07:40,710 --> 00:07:47,730
Where the clients could refer to the area where the coffee is made with another name and.

97
00:07:48,720 --> 00:07:56,160
The body store itself with their colleagues, they can refer to that place using their own name.

98
00:07:56,910 --> 00:08:05,940
So here in this may not be why we are in our table and we refer to that functions as the module.

99
00:08:07,160 --> 00:08:12,750
Where something happens and this group is not interested about what happens there.

100
00:08:12,770 --> 00:08:17,720
The script is only interested about getting the outputs of that.

101
00:08:17,750 --> 00:08:20,540
Of those procedures, those functions.

102
00:08:20,840 --> 00:08:24,590
So we get the outputs from there by calling that space.

103
00:08:26,000 --> 00:08:31,070
So we can get now functions.

104
00:08:31,160 --> 00:08:35,090
We can call functions from that space there.

105
00:08:37,280 --> 00:08:40,700
So similarly, we want to mention functions.

106
00:08:40,700 --> 00:08:41,240
That's right.

107
00:08:41,240 --> 00:08:42,260
To do here.

108
00:08:42,409 --> 00:08:48,560
Functions that get juice in there wherever you have this break lines.

109
00:08:56,070 --> 00:09:00,420
There and here as well.

110
00:09:01,750 --> 00:09:04,570
So now we don't have any more red lines.

111
00:09:06,300 --> 00:09:09,900
The question is which way should you use this or that?

112
00:09:09,900 --> 00:09:14,910
And that's a hard question, and it's not always easy to answer.

113
00:09:15,660 --> 00:09:20,550
It depends how many functions you have in the module here.

114
00:09:20,670 --> 00:09:24,000
So in our case, we only have two.

115
00:09:24,090 --> 00:09:31,260
But if we had more, then you'd have to write all the names of the functions there and so on, divided

116
00:09:31,260 --> 00:09:32,250
by commas.

117
00:09:33,690 --> 00:09:41,220
So if that was the case, then it would definitely be better to just import the functions module and

118
00:09:41,220 --> 00:09:49,860
then just refer to the functions of that module using functions as the first name separated by a dot.

119
00:09:51,170 --> 00:09:56,300
So that would avoid us having to import all the functions one by one.

120
00:09:56,450 --> 00:10:01,700
We could forget one of them, or it could become a long list here.

121
00:10:02,570 --> 00:10:10,220
So that's why we have this other simpler way to import a module and then refer to them as here.

122
00:10:11,360 --> 00:10:16,010
So if you have very few functions like we have here, you could use that.

123
00:10:16,400 --> 00:10:18,350
But this is also fine to use.

124
00:10:20,220 --> 00:10:27,990
And this can also make the codes more readable because by just looking at this parts here, you'll know

125
00:10:27,990 --> 00:10:34,220
that get to dos comes from the functions module, so that gives you some extra information.

126
00:10:34,230 --> 00:10:40,470
Whereas if you had only that without the.

127
00:10:42,260 --> 00:10:43,010
Comments.

128
00:10:43,610 --> 00:10:45,800
So you'd have something like that.

129
00:10:45,980 --> 00:10:53,420
In that case, it's not easy to tell if get to dos was defined in the script or in another script.

130
00:10:56,280 --> 00:11:02,400
Of course, you can look at the script and see if get to do is there, but it's easy to just tell.

131
00:11:03,190 --> 00:11:07,120
Immediately if this is from the script or from another script.

132
00:11:07,120 --> 00:11:13,000
So that's why I would suggest to use this syntax.

133
00:11:15,380 --> 00:11:23,510
Lastly, it could happen that you may have more and more files, and in that case you may want to create

134
00:11:23,510 --> 00:11:24,470
a directory.

135
00:11:26,010 --> 00:11:27,410
Let's call this modules.

136
00:11:28,340 --> 00:11:33,280
So this directory, which is in the same directory with main PI.

137
00:11:33,320 --> 00:11:41,930
Now if I drag functions that's pi and drop it in modules, Python will ask me if I want to refactor.

138
00:11:41,930 --> 00:11:44,870
So I refactor here in that case.

139
00:11:45,710 --> 00:11:50,540
Pie chart change this from input functions to from modules input functions.

140
00:11:50,540 --> 00:11:52,940
So this is the new syntax now.

141
00:11:53,480 --> 00:12:00,230
And so as you see modules reflects the name of the directory here and functions reflects the name of

142
00:12:00,230 --> 00:12:01,220
the module.

143
00:12:02,660 --> 00:12:09,380
So if you ever have directories where you place files, then you want to follow these syntax points

144
00:12:09,380 --> 00:12:11,960
to the directory name and then to the module.

145
00:12:12,860 --> 00:12:20,690
In our case, this is not necessary, so I'll drag this back to the main directory and this will you

146
00:12:20,720 --> 00:12:23,000
be used as just functions?

147
00:12:26,970 --> 00:12:31,560
So that's about creating modules and importing them.

148
00:12:33,210 --> 00:12:37,110
So this is both a script, as I mentioned.

149
00:12:38,110 --> 00:12:39,160
From.

150
00:12:40,030 --> 00:12:49,710
It's own perspective versus a script from the perspective of the other file where this is important,

151
00:12:49,720 --> 00:12:52,450
this function is a module.

152
00:12:55,410 --> 00:12:57,000
So I hope that makes sense.

153
00:12:57,000 --> 00:13:01,740
We will run a few experiments in the next video to understand some things better.

154
00:13:01,740 --> 00:13:02,870
So I'll see you there.

