WEBVTT

1
00:00:00.930 --> 00:00:02.460
<v Jose>Hi guys, and welcome back.</v>

2
00:00:02.460 --> 00:00:04.960
In this video, we're going to start using Python lists

3
00:00:04.960 --> 00:00:07.930
as a database table instead of SQL Lite.

4
00:00:07.930 --> 00:00:10.470
We're going to do this first because it's simpler,

5
00:00:10.470 --> 00:00:12.460
then when we want to use SQL Lite,

6
00:00:12.460 --> 00:00:16.140
the change over will be very straightforward.

7
00:00:16.140 --> 00:00:17.050
When you think about it,

8
00:00:17.050 --> 00:00:19.640
lists and database tables are very similar.

9
00:00:19.640 --> 00:00:23.030
They are both used to store individual items.

10
00:00:23.030 --> 00:00:26.150
Inside a database table, we store rows of data.

11
00:00:26.150 --> 00:00:28.540
And each row can have multiple pieces of data,

12
00:00:28.540 --> 00:00:30.210
one per column.

13
00:00:30.210 --> 00:00:32.760
Inside the list, we can store anything we like,

14
00:00:32.760 --> 00:00:34.810
including dictionaries,

15
00:00:34.810 --> 00:00:39.110
each dictionary can contain multiple pieces of data as well.

16
00:00:39.110 --> 00:00:42.220
So here, I'm going to show you sort of a sample dictionary

17
00:00:42.220 --> 00:00:46.630
that we might store in our list.

18
00:00:46.630 --> 00:00:48.360
So here we've got our entries list,

19
00:00:48.360 --> 00:00:50.720
this is what we're going to be storing in our application.

20
00:00:50.720 --> 00:00:52.350
And you can see that it's a list,

21
00:00:52.350 --> 00:00:55.530
and inside it we've got four dictionaries.

22
00:00:55.530 --> 00:00:58.550
You can imagine that these things are things a user created,

23
00:00:58.550 --> 00:01:01.180
so we've got the content key

24
00:01:01.180 --> 00:01:03.950
that contains what the user learned on that day,

25
00:01:03.950 --> 00:01:07.450
and the date key that contains when they learned it.

26
00:01:07.450 --> 00:01:09.900
Initially, our entries list is going to be empty.

27
00:01:09.900 --> 00:01:11.860
And we will be creating a dictionary

28
00:01:11.860 --> 00:01:14.000
every time the user adds a new entry,

29
00:01:14.000 --> 00:01:16.970
and putting that dictionary into the list.

30
00:01:16.970 --> 00:01:18.290
So this is just an example.

31
00:01:18.290 --> 00:01:21.270
But let's go ahead and write this code out.

32
00:01:21.270 --> 00:01:22.103
The first thing we're going to do

33
00:01:22.103 --> 00:01:24.400
is create a separate Python file

34
00:01:24.400 --> 00:01:28.660
to host the logic that interacts with our data store.

35
00:01:28.660 --> 00:01:30.500
At the moment, as I said, this is going to be a list,

36
00:01:30.500 --> 00:01:32.310
later on it will be a database.

37
00:01:32.310 --> 00:01:34.190
So I'm going to open my file viewer there,

38
00:01:34.190 --> 00:01:37.500
create a new file called database.py.

39
00:01:37.500 --> 00:01:39.700
Then I'm going to close the file viewer.

40
00:01:39.700 --> 00:01:43.000
Here, we're going to create our entries list,

41
00:01:43.000 --> 00:01:44.910
which is this empty list initially.

42
00:01:44.910 --> 00:01:47.530
And now we're going to create a couple of functions

43
00:01:47.530 --> 00:01:50.400
just to allow us to add a new entry,

44
00:01:50.400 --> 00:01:52.053
or view existing entries.

45
00:01:54.100 --> 00:01:56.320
So here is the add_entry function.

46
00:01:56.320 --> 00:02:00.460
Notice I've used the pass keyword, which means do nothing,

47
00:02:00.460 --> 00:02:04.340
to allow Python to still process this function as valid,

48
00:02:04.340 --> 00:02:07.510
it does need the indented block in there to be valid.

49
00:02:07.510 --> 00:02:09.730
But at the moment, this function does nothing.

50
00:02:09.730 --> 00:02:12.580
And we're going to do something similar for view_entries,

51
00:02:14.350 --> 00:02:15.910
with pass as well.

52
00:02:15.910 --> 00:02:17.550
So now we've got our two functions,

53
00:02:17.550 --> 00:02:21.340
we could go into app.py and import these functions.

54
00:02:21.340 --> 00:02:24.363
And that is what we will do, indeed, in just a moment.

55
00:02:25.210 --> 00:02:29.010
To begin with, we're going to use this add_entry function

56
00:02:29.010 --> 00:02:31.690
to ask the user for what they've learned

57
00:02:31.690 --> 00:02:35.393
and when they learned it, and add that to the entries list.

58
00:02:38.100 --> 00:02:40.700
So we will create this entry content variable,

59
00:02:40.700 --> 00:02:42.200
which is going to be an input

60
00:02:42.200 --> 00:02:44.523
of what have you learned today,

61
00:02:45.730 --> 00:02:47.170
and also the entry date,

62
00:02:47.170 --> 00:02:51.380
which is another input of enter the date.

63
00:02:51.380 --> 00:02:53.620
So here, we've got two strings.

64
00:02:53.620 --> 00:02:56.220
The date is not being validated or anything like that,

65
00:02:56.220 --> 00:02:59.050
as we spoke about earlier on in the presentation,

66
00:02:59.050 --> 00:03:00.120
and what have you learned today,

67
00:03:00.120 --> 00:03:03.020
the user can enter anything they want.

68
00:03:03.020 --> 00:03:04.970
Now that we've got those two pieces of data,

69
00:03:04.970 --> 00:03:06.960
we can create a dictionary with them.

70
00:03:06.960 --> 00:03:09.410
And the dictionary should have a content key

71
00:03:09.410 --> 00:03:11.240
that is going to be the entry_content,

72
00:03:11.240 --> 00:03:14.840
and a date key, which is the entry_date.

73
00:03:14.840 --> 00:03:16.230
Now creating the dictionary on its own,

74
00:03:16.230 --> 00:03:18.380
of course, it's not going to put it into the list.

75
00:03:18.380 --> 00:03:20.740
So we do have to do that ourselves.

76
00:03:20.740 --> 00:03:24.730
Entries.append, and we will add it in there.

77
00:03:24.730 --> 00:03:26.360
So here we can see that we are appending

78
00:03:26.360 --> 00:03:27.340
into the entries list,

79
00:03:27.340 --> 00:03:30.250
our new dictionary that we create here.

80
00:03:30.250 --> 00:03:33.980
To view the entries, we can similarly go through the list

81
00:03:33.980 --> 00:03:36.290
and print out each entry information.

82
00:03:36.290 --> 00:03:38.630
So we can do for entry in entries,

83
00:03:38.630 --> 00:03:41.720
here this for loop creates the entry variable

84
00:03:41.720 --> 00:03:45.020
every time we go through one item of the entries list.

85
00:03:45.020 --> 00:03:46.280
And the entry variable initially

86
00:03:46.280 --> 00:03:48.960
will be the first item in the entries list.

87
00:03:48.960 --> 00:03:52.550
We will then run this indented block of code here.

88
00:03:52.550 --> 00:03:53.383
We will go back,

89
00:03:53.383 --> 00:03:56.060
and entry will become the second item in the entries list,

90
00:03:56.060 --> 00:03:58.860
and then we will run this block of code, and so on.

91
00:03:58.860 --> 00:04:00.790
Again, this is all covered in the Python refresher.

92
00:04:00.790 --> 00:04:02.480
So if you have any questions about for loops,

93
00:04:02.480 --> 00:04:04.560
or dictionaries, or lists, and things like that,

94
00:04:04.560 --> 00:04:06.060
do go back and check that out.

95
00:04:07.260 --> 00:04:09.500
Now, for every entry in the entries list,

96
00:04:09.500 --> 00:04:11.450
we want to print out the information.

97
00:04:11.450 --> 00:04:14.140
So we're going to say print, and then in an f string,

98
00:04:14.140 --> 00:04:17.053
we're going to put the entry, date.

99
00:04:18.230 --> 00:04:19.970
And then in a new line,

100
00:04:19.970 --> 00:04:22.573
we're going to put to the entry content.

101
00:04:23.860 --> 00:04:26.310
And then we're going to put two new lines.

102
00:04:26.310 --> 00:04:27.660
So a couple of things going on here,

103
00:04:27.660 --> 00:04:29.640
this f string is a little bit complicated.

104
00:04:29.640 --> 00:04:32.410
We are interpolating into the string,

105
00:04:32.410 --> 00:04:37.100
the value of accessing the date property of the entry.

106
00:04:37.100 --> 00:04:40.340
So notice that here, we're using double quotation marks

107
00:04:40.340 --> 00:04:42.230
at the end and the start of the string.

108
00:04:42.230 --> 00:04:45.090
The access of the data property in the dictionary

109
00:04:45.090 --> 00:04:47.220
must use single quotation marks,

110
00:04:47.220 --> 00:04:48.650
otherwise, Python would get confused

111
00:04:48.650 --> 00:04:51.420
and it would think that and this is a string,

112
00:04:51.420 --> 00:04:53.630
and this is another one, it doesn't like that.

113
00:04:53.630 --> 00:04:55.920
So make sure to use single quotation marks there.

114
00:04:55.920 --> 00:04:57.860
That's the date of the entry.

115
00:04:57.860 --> 00:05:00.960
Then we're putting a new line with a backslash n character,

116
00:05:00.960 --> 00:05:02.980
and then we're interpolating the content

117
00:05:02.980 --> 00:05:05.080
into it's a new line,

118
00:05:05.080 --> 00:05:06.800
and finally, we're putting two new lines there,

119
00:05:06.800 --> 00:05:08.240
so that when we print the next entry,

120
00:05:08.240 --> 00:05:10.470
there's going to be a bit of separation between the two.

121
00:05:10.470 --> 00:05:14.040
Okay, so now we're going to save this file,

122
00:05:14.040 --> 00:05:16.780
and we can go ahead and use it in our app.py.

123
00:05:16.780 --> 00:05:19.340
So make sure to save it, we can go over to app.py,

124
00:05:19.340 --> 00:05:21.440
and now we can import those two functions

125
00:05:21.440 --> 00:05:24.910
that allow us to interact with our data store in here.

126
00:05:24.910 --> 00:05:28.617
So we do from database import add_entry, and view_entries.

127
00:05:30.100 --> 00:05:33.370
Now once we've got that, we can go down into our menu

128
00:05:33.370 --> 00:05:36.070
and just use them instead of print adding and viewing.

129
00:05:36.070 --> 00:05:38.980
So we'll do add_entry here, make sure to call the function,

130
00:05:38.980 --> 00:05:41.590
and view_entries in here.

131
00:05:41.590 --> 00:05:43.670
Now if we save this and run it,

132
00:05:43.670 --> 00:05:45.080
you'll see that it all works.

133
00:05:45.080 --> 00:05:46.977
So I'm going to open up the terminal down here.

134
00:05:46.977 --> 00:05:48.840
I'm just going to make it a little bit bigger.

135
00:05:48.840 --> 00:05:50.040
I know the text here is a bit small,

136
00:05:50.040 --> 00:05:52.490
but hopefully you can read it there.

137
00:05:52.490 --> 00:05:55.000
And now I'm going to run Python3.8 app.py.

138
00:05:55.000 --> 00:05:56.960
Remember, we've used the walrus operator,

139
00:05:56.960 --> 00:05:58.050
or the assignment expression,

140
00:05:58.050 --> 00:06:00.363
so we need Python 3.8 for this code.

141
00:06:01.580 --> 00:06:02.920
Here we've got our menu,

142
00:06:02.920 --> 00:06:05.250
we can then add a new entry, for example.

143
00:06:05.250 --> 00:06:07.150
And when we type one, of course,

144
00:06:07.150 --> 00:06:09.630
we are running the add_entry code,

145
00:06:09.630 --> 00:06:11.450
that's taken us to database.py,

146
00:06:11.450 --> 00:06:14.250
and now we are currently on this line.

147
00:06:14.250 --> 00:06:15.260
So what have we learned today?

148
00:06:15.260 --> 00:06:19.723
We've learned how to use Python lists as a data store,

149
00:06:20.640 --> 00:06:23.690
and enter the date, the date can be today.

150
00:06:23.690 --> 00:06:25.520
Notice that there is no validation on this date,

151
00:06:25.520 --> 00:06:27.540
so we can enter whatever we want.

152
00:06:27.540 --> 00:06:30.180
Now, every one of you entries, we enter two

153
00:06:30.180 --> 00:06:32.270
and then we get today,

154
00:06:32.270 --> 00:06:35.570
and then a new line, we get the content of the entry.

155
00:06:35.570 --> 00:06:37.230
And then we get two new lines.

156
00:06:37.230 --> 00:06:39.600
And then we would get the next one if there was more,

157
00:06:39.600 --> 00:06:41.530
but now we just get the menu.

158
00:06:41.530 --> 00:06:43.800
Finally, we can enter three to exit.

159
00:06:43.800 --> 00:06:47.240
Okay, just gonna make that a bit smaller again.

160
00:06:47.240 --> 00:06:51.030
And I'm going to clear it which gets rid of all that text.

161
00:06:51.030 --> 00:06:53.040
Now there is a glaring problem

162
00:06:53.040 --> 00:06:55.070
in the way we've structured this application.

163
00:06:55.070 --> 00:06:58.670
And this problem may not be all that obvious

164
00:06:58.670 --> 00:07:00.770
if you are not that familiar with coding,

165
00:07:00.770 --> 00:07:02.840
but I'll tell you that it is a glaring problem

166
00:07:02.840 --> 00:07:04.800
for any developer that's a bit more experienced.

167
00:07:04.800 --> 00:07:08.610
The problem is that the database.py file

168
00:07:08.610 --> 00:07:10.510
is doing really two things.

169
00:07:10.510 --> 00:07:13.110
The database.py file is interacting with the user,

170
00:07:13.110 --> 00:07:16.930
gathering data and it's also storing data in a database.

171
00:07:16.930 --> 00:07:19.240
So it's doing these two separate things.

172
00:07:19.240 --> 00:07:22.400
And it can become confusing and complicated

173
00:07:22.400 --> 00:07:24.800
to then change the data that we're storing,

174
00:07:24.800 --> 00:07:26.270
or how we're gathering it.

175
00:07:26.270 --> 00:07:30.280
It's usually better to have these two things separated,

176
00:07:30.280 --> 00:07:32.540
have one part of our application

177
00:07:32.540 --> 00:07:33.940
dealing with getting the user input,

178
00:07:33.940 --> 00:07:35.380
and have a different part dealing with

179
00:07:35.380 --> 00:07:37.150
interacting with a data store.

180
00:07:37.150 --> 00:07:39.220
Similarly, the view_entries function

181
00:07:39.220 --> 00:07:42.190
is retrieving data from the data store,

182
00:07:42.190 --> 00:07:44.550
and it's also outputting data to the user.

183
00:07:44.550 --> 00:07:47.130
So it's doing those two separate things as well.

184
00:07:47.130 --> 00:07:50.220
Overall, this file here, gets data,

185
00:07:50.220 --> 00:07:52.420
processes data, and displays data.

186
00:07:52.420 --> 00:07:55.610
So yeah, it's not a great file at the moment,

187
00:07:55.610 --> 00:07:56.910
but we can make it better.

188
00:07:57.900 --> 00:08:00.850
Up until before we made this database.py file,

189
00:08:00.850 --> 00:08:04.280
the app.py was in charge of dealing with user input,

190
00:08:04.280 --> 00:08:06.490
and displaying information.

191
00:08:06.490 --> 00:08:07.810
So why don't we keep it that way,

192
00:08:07.810 --> 00:08:10.080
we will move all the user input gathering

193
00:08:10.080 --> 00:08:12.410
and data display to app.py.

194
00:08:12.410 --> 00:08:14.140
And we will keep database.py

195
00:08:14.140 --> 00:08:17.070
as only dealing with the database.

196
00:08:17.070 --> 00:08:20.030
So how to do that is we're going to move these two inputs

197
00:08:20.030 --> 00:08:22.200
over to app.py.

198
00:08:22.200 --> 00:08:25.940
And we're going to pass their contents to add_entry.

199
00:08:25.940 --> 00:08:29.140
So I'm going to add two new parameters here,

200
00:08:29.140 --> 00:08:31.820
entry_content and entry_date.

201
00:08:31.820 --> 00:08:35.230
And now, this function here is only concerned

202
00:08:35.230 --> 00:08:37.150
with taking whatever data it receives

203
00:08:37.150 --> 00:08:39.010
and putting it in a database.

204
00:08:39.010 --> 00:08:42.060
It is going to be app.py going over there now,

205
00:08:42.060 --> 00:08:45.030
that is going to ask the user for data.

206
00:08:45.030 --> 00:08:47.400
And then it's going to call the add_entry function

207
00:08:47.400 --> 00:08:51.760
with the content and the date, just like that.

208
00:08:51.760 --> 00:08:54.150
So now we're getting the user input,

209
00:08:54.150 --> 00:08:55.590
storing it in a variable,

210
00:08:55.590 --> 00:08:58.560
passing the value of that variable to add entry,

211
00:08:58.560 --> 00:09:01.040
and then add_entry is receiving that value

212
00:09:01.040 --> 00:09:02.723
and storing it in the database.

213
00:09:04.330 --> 00:09:06.610
Similarly, if we go to database.py,

214
00:09:06.610 --> 00:09:08.690
we've got here this for entry in entries

215
00:09:08.690 --> 00:09:10.160
that is displaying information.

216
00:09:10.160 --> 00:09:12.540
Instead of that, we're just going to make view_entries

217
00:09:12.540 --> 00:09:16.060
responsible for getting the data from this list,

218
00:09:16.060 --> 00:09:18.100
and giving it back to the caller,

219
00:09:18.100 --> 00:09:21.510
whoever will use this function, so we'll do return entries.

220
00:09:21.510 --> 00:09:23.760
At the moment, this may not seem all that useful.

221
00:09:23.760 --> 00:09:25.700
But later on when we're actually interacting

222
00:09:25.700 --> 00:09:28.830
with a database, you'll see why this becomes more important.

223
00:09:28.830 --> 00:09:30.510
And I've copied the for loop there.

224
00:09:30.510 --> 00:09:32.980
And I'm just going to move it down here

225
00:09:32.980 --> 00:09:34.450
for entry in entries there.

226
00:09:34.450 --> 00:09:37.250
And I'm going to get to the return value of view_entries

227
00:09:37.250 --> 00:09:39.590
and put it in a variable as well.

228
00:09:39.590 --> 00:09:42.700
Notice that you could grab this function here

229
00:09:42.700 --> 00:09:44.650
and put it directly in the for loop,

230
00:09:44.650 --> 00:09:48.860
that's totally fine as well, either option works.

231
00:09:48.860 --> 00:09:51.550
Now notice that the function is called view entries,

232
00:09:51.550 --> 00:09:53.550
which suggests that you're going to be able

233
00:09:53.550 --> 00:09:56.600
to see something, but the view_entries function

234
00:09:56.600 --> 00:10:00.130
is no longer concerned with actually displaying data,

235
00:10:00.130 --> 00:10:04.109
so we're actually going to rename it to get_entries instead.

236
00:10:04.109 --> 00:10:07.280
So we do have to rename it in database.py down here

237
00:10:07.280 --> 00:10:09.293
and in the import as well.

238
00:10:10.300 --> 00:10:13.690
This makes app.py more complicated.

239
00:10:13.690 --> 00:10:15.610
But it makes the whole application simpler

240
00:10:15.610 --> 00:10:19.090
because now reading app.py, we can have a clear idea

241
00:10:19.090 --> 00:10:21.390
of what database.py is doing.

242
00:10:21.390 --> 00:10:23.720
It adds and gets entries from a data store.

243
00:10:23.720 --> 00:10:25.289
And we've also made it clear exactly

244
00:10:25.289 --> 00:10:27.580
what data we're adding to the database,

245
00:10:27.580 --> 00:10:30.630
previously, that was hidden inside database.py

246
00:10:30.630 --> 00:10:31.630
we're calling the function,

247
00:10:31.630 --> 00:10:34.000
and we didn't really know by reading this,

248
00:10:34.000 --> 00:10:37.910
whether database.py was asking for two pieces of data or 10.

249
00:10:37.910 --> 00:10:39.520
Now it's a bit clearer.

250
00:10:39.520 --> 00:10:41.220
And app.py as you read through it,

251
00:10:41.220 --> 00:10:44.880
you can see exactly what the user is going to see as well.

252
00:10:44.880 --> 00:10:47.900
We could improve the code inside app.py a bit further

253
00:10:47.900 --> 00:10:49.890
by splitting it into functions.

254
00:10:49.890 --> 00:10:52.670
So this stuff here could go into its own function.,

255
00:10:52.670 --> 00:10:54.680
this stuff here could go into its own function.

256
00:10:54.680 --> 00:10:57.110
We're going to do that just now as well.

257
00:10:57.110 --> 00:10:59.780
We need to define functions before they're used,

258
00:10:59.780 --> 00:11:01.250
so we're going to go up here

259
00:11:01.250 --> 00:11:05.950
and create a prompt_new_entry function.

260
00:11:05.950 --> 00:11:08.860
And this is going to do all of this stuff.

261
00:11:08.860 --> 00:11:11.330
So I'm going to copy that into that.

262
00:11:11.330 --> 00:11:15.283
And down in here, we're just going to call prompt_new_entry.

263
00:11:16.670 --> 00:11:19.500
So I prefix these functions with prompt

264
00:11:19.500 --> 00:11:21.310
because they're actually interacting with the user,

265
00:11:21.310 --> 00:11:23.260
I like to make that a bit clear.

266
00:11:23.260 --> 00:11:25.460
And then we're going to create another function

267
00:11:25.460 --> 00:11:26.480
for viewing entries,

268
00:11:26.480 --> 00:11:29.150
and this time it is going to be called view_entries.

269
00:11:29.150 --> 00:11:33.140
But I'm going to make it take in the entries

270
00:11:33.140 --> 00:11:35.540
from the database itself.

271
00:11:35.540 --> 00:11:39.000
Then this one here is just going to go through the entries

272
00:11:40.090 --> 00:11:41.670
and print them out.

273
00:11:41.670 --> 00:11:43.320
Now when we've got the entries,

274
00:11:43.320 --> 00:11:45.622
we can just call it view_entries

275
00:11:45.622 --> 00:11:48.520
with the entries list that we got from the database.

276
00:11:48.520 --> 00:11:51.180
Now this is starting to become a little bit too superfluous,

277
00:11:51.180 --> 00:11:52.220
this entries database,

278
00:11:52.220 --> 00:11:54.440
so I'm just going to copy that and put it in there,

279
00:11:54.440 --> 00:11:56.430
get rid of that variable.

280
00:11:56.430 --> 00:11:59.210
So while this code is now a bit longer than before,

281
00:11:59.210 --> 00:12:02.920
it is a bit simpler because it's more divided into parts.

282
00:12:02.920 --> 00:12:04.030
When you read this function,

283
00:12:04.030 --> 00:12:06.410
you know that we're going to be adding a new entry.

284
00:12:06.410 --> 00:12:07.790
And when you view this one,

285
00:12:07.790 --> 00:12:10.100
you know we're going to be displaying entry information.

286
00:12:10.100 --> 00:12:12.310
And finally, the loop now is very simple.

287
00:12:12.310 --> 00:12:14.810
If the user input is one, then we prompt for a new entry.

288
00:12:14.810 --> 00:12:17.460
And if it's two, then we view the entries.

289
00:12:17.460 --> 00:12:19.150
So hopefully you agree with that.

290
00:12:19.150 --> 00:12:20.420
Now, in the next few videos,

291
00:12:20.420 --> 00:12:24.030
we're going to be replacing the Python lists for SQL Lite.

292
00:12:24.030 --> 00:12:26.200
So let's go and do that in the next video.

293
00:12:26.200 --> 00:12:27.150
I'll see you there.

