WEBVTT

1
00:00:00.400 --> 00:00:01.890
<v Jose>Hi guys and welcome back.</v>

2
00:00:01.890 --> 00:00:03.760
In this video, we're going to be completing

3
00:00:03.760 --> 00:00:06.280
our user menu and finishing stage one

4
00:00:06.280 --> 00:00:07.500
of our application.

5
00:00:07.500 --> 00:00:09.100
Just as a reminder, let's quickly go through

6
00:00:09.100 --> 00:00:10.287
the code that we've got right now.

7
00:00:10.287 --> 00:00:15.150
This here is app.py our main user interaction file.

8
00:00:15.150 --> 00:00:17.981
We've got our menu, that let's users pick from their options

9
00:00:17.981 --> 00:00:21.350
then we print a welcome, create the tables

10
00:00:21.350 --> 00:00:23.830
with our database and then we have our loop

11
00:00:23.830 --> 00:00:25.387
that runs the menu over and over

12
00:00:25.387 --> 00:00:26.970
allowing the user to pick different things

13
00:00:26.970 --> 00:00:28.470
and we're gonna do different things

14
00:00:28.470 --> 00:00:30.460
depending on their input.

15
00:00:30.460 --> 00:00:32.470
Then we've got database.py,

16
00:00:32.470 --> 00:00:33.970
where we have all our queries,

17
00:00:33.970 --> 00:00:37.180
and a few functions that allow us to interact

18
00:00:37.180 --> 00:00:39.850
with a database from another Python file.

19
00:00:39.850 --> 00:00:42.908
We've got create tables, that runs and creates the tables,

20
00:00:42.908 --> 00:00:45.290
we've got add movie, to insert a new movie

21
00:00:45.290 --> 00:00:47.570
into the database, given two arguments.

22
00:00:47.570 --> 00:00:50.110
We've got get movies that retrieves movies

23
00:00:50.110 --> 00:00:54.390
that might be upcoming or old movies if this is false.

24
00:00:54.390 --> 00:00:56.320
Watch movie, that sets a movie to watch

25
00:00:56.320 --> 00:00:58.660
by updating the table.

26
00:00:58.660 --> 00:01:01.300
And get watched movies which only returns

27
00:01:01.300 --> 00:01:03.510
those movies that have already been watched.

28
00:01:03.510 --> 00:01:05.470
So let's go over to app.py

29
00:01:05.470 --> 00:01:07.760
and start filling in this menu.

30
00:01:07.760 --> 00:01:09.090
The first thing we're gonna want to do

31
00:01:09.090 --> 00:01:11.020
is to add new movies.

32
00:01:11.020 --> 00:01:13.330
So what I usually like to do here

33
00:01:13.330 --> 00:01:15.753
is to go ahead and create a prompt

34
00:01:15.753 --> 00:01:19.320
add movie function just because this function here

35
00:01:19.320 --> 00:01:22.360
is going to ask the user for a couple of pieces of data

36
00:01:22.360 --> 00:01:26.170
it might parse some dates and call the database file,

37
00:01:26.170 --> 00:01:28.930
so it makes sense to have it in it's own function.

38
00:01:28.930 --> 00:01:30.210
Remember that functions in Python

39
00:01:30.210 --> 00:01:33.010
like having two lines at the start and at the end.

40
00:01:33.010 --> 00:01:34.644
So I'm just gonna add that in to give it a bit more

41
00:01:34.644 --> 00:01:36.440
room to breathe.

42
00:01:36.440 --> 00:01:37.804
So the first thing we want to do here

43
00:01:37.804 --> 00:01:40.750
is find out the data we need from the user,

44
00:01:40.750 --> 00:01:41.913
so that's gonna be title,

45
00:01:41.913 --> 00:01:45.450
and we're gonna add movie title in here

46
00:01:45.450 --> 00:01:49.230
and we also need the release date.

47
00:01:49.230 --> 00:01:52.810
So we're gonna do input of release date,

48
00:01:52.810 --> 00:01:56.415
in dd, mm, yyyy format.

49
00:01:56.415 --> 00:01:59.040
This just means day, month, year,

50
00:01:59.040 --> 00:02:01.100
you can pick whichever date format you want in here

51
00:02:01.100 --> 00:02:02.564
we're gonna learn how to parse those

52
00:02:02.564 --> 00:02:05.040
into a timestamp just now.

53
00:02:05.040 --> 00:02:07.520
So now that we've got a string representing the date

54
00:02:07.520 --> 00:02:09.180
presumably in this format,

55
00:02:09.180 --> 00:02:12.130
we can use the date time module to parse it.

56
00:02:12.130 --> 00:02:15.120
So let's go over to the top and above our import

57
00:02:15.120 --> 00:02:17.410
we're gonna import the date time module.

58
00:02:17.410 --> 00:02:19.880
The reason why I put it above our import by the way,

59
00:02:19.880 --> 00:02:21.150
is because normally in Python

60
00:02:21.150 --> 00:02:23.840
you will import you know the standard library,

61
00:02:23.840 --> 00:02:25.490
the modules that come with Python

62
00:02:25.490 --> 00:02:28.130
at the very top you're own modules after that.

63
00:02:28.130 --> 00:02:29.810
So that's why I'm doing that.

64
00:02:29.810 --> 00:02:33.360
And here we are going to then go ahead and parse this.

65
00:02:33.360 --> 00:02:34.193
The first thing we're going to do is

66
00:02:34.193 --> 00:02:37.770
to use a date time module to parse

67
00:02:37.770 --> 00:02:41.410
the string into a date time object.

68
00:02:41.410 --> 00:02:42.830
When we have a date time object,

69
00:02:42.830 --> 00:02:45.990
we can very easily get the timestamp that it represents.

70
00:02:45.990 --> 00:02:47.760
So we will do parsed date,

71
00:02:47.760 --> 00:02:49.990
is gonna equal to datetime.datetime

72
00:02:49.990 --> 00:02:51.870
and that just gives us the date time class

73
00:02:51.870 --> 00:02:53.730
inside the date time module.

74
00:02:53.730 --> 00:02:58.730
And then we're gonna use STRP time for string, parse, time

75
00:02:58.770 --> 00:03:01.450
to parse the string into a date time.

76
00:03:01.450 --> 00:03:02.930
And this takes two arguments,

77
00:03:02.930 --> 00:03:05.070
the release date string in this case,

78
00:03:05.070 --> 00:03:06.823
which should be in this format,

79
00:03:06.823 --> 00:03:09.780
and then it also takes a format string

80
00:03:09.780 --> 00:03:11.560
or something to tell the date time module

81
00:03:11.560 --> 00:03:14.420
what format this string is in.

82
00:03:14.420 --> 00:03:16.150
So that is gonna be percent D

83
00:03:16.150 --> 00:03:18.310
and that tells the date time module that

84
00:03:18.310 --> 00:03:23.000
we've got essentially two numbers as the date of the month,

85
00:03:23.000 --> 00:03:25.090
then percent M that tells the date time object

86
00:03:25.090 --> 00:03:28.730
that we've got the two numbers as the month of the year,

87
00:03:28.730 --> 00:03:31.630
and then percent uppercase Y that tells the date time module

88
00:03:31.630 --> 00:03:34.440
that we've got four digits as the year.

89
00:03:34.440 --> 00:03:36.390
Notice that these are separated by dashes

90
00:03:36.390 --> 00:03:39.620
just like we have in our user prompt there

91
00:03:39.620 --> 00:03:42.150
which means that we should expect this here

92
00:03:42.150 --> 00:03:45.280
to be matching the input that the user typed.

93
00:03:45.280 --> 00:03:47.820
Now that we've got that, we have a date time object.

94
00:03:47.820 --> 00:03:50.230
And as you can remember from the database module,

95
00:03:50.230 --> 00:03:54.330
we can now do timestamp equal parsed date.timestamp

96
00:03:54.330 --> 00:03:56.950
calling the method in the date time object

97
00:03:56.950 --> 00:03:59.670
gives us the timestamp that it represents.

98
00:03:59.670 --> 00:04:00.800
And now that we have all that,

99
00:04:00.800 --> 00:04:03.483
we can simply call database.add movie,

100
00:04:04.880 --> 00:04:06.965
pass it in the title and the timestamp,

101
00:04:06.965 --> 00:04:10.440
and we're good to go, that should be added to the table.

102
00:04:10.440 --> 00:04:12.120
We can simply now call this function

103
00:04:12.120 --> 00:04:13.520
from the user menu

104
00:04:14.700 --> 00:04:16.810
and that should be asked with the ability to

105
00:04:16.810 --> 00:04:18.220
add new movies.

106
00:04:18.220 --> 00:04:19.340
I would recommend at this point

107
00:04:19.340 --> 00:04:22.350
to run your code and see if it works.

108
00:04:22.350 --> 00:04:23.721
So we're gonna run the code,

109
00:04:23.721 --> 00:04:27.320
go into the db browser to check out the file

110
00:04:27.320 --> 00:04:30.800
that this creates and see if a movie appears in there.

111
00:04:30.800 --> 00:04:34.520
So we'll do Python app.py,

112
00:04:34.520 --> 00:04:36.060
and then we're gonna add the new movie

113
00:04:36.060 --> 00:04:39.050
for example The Matrix by,

114
00:04:39.050 --> 00:04:40.730
oh I actually don't know when it was released,

115
00:04:40.730 --> 00:04:42.510
let's say the first of January, 2002

116
00:04:43.670 --> 00:04:44.560
and there we go.

117
00:04:44.560 --> 00:04:46.790
So now what we wanna do is go into db browser

118
00:04:46.790 --> 00:04:50.040
and see if we have inserted a timestamp,

119
00:04:50.040 --> 00:04:52.487
a long set of numbers as the release date

120
00:04:52.487 --> 00:04:54.050
and of course whether the movie is there

121
00:04:54.050 --> 00:04:55.600
and has the correct name.

122
00:04:55.600 --> 00:04:57.440
So I'm gonna exit by typing six

123
00:04:57.440 --> 00:04:59.290
and then let's go over to db browser.

124
00:05:01.100 --> 00:05:02.900
So we're here in db browser

125
00:05:02.900 --> 00:05:06.170
and you can see that I've got my movies table created,

126
00:05:06.170 --> 00:05:08.620
we can go ahead into browse data

127
00:05:08.620 --> 00:05:12.170
and you'll see that The Matrix is here,

128
00:05:12.170 --> 00:05:15.000
and we've got our timestamp there,

129
00:05:15.000 --> 00:05:18.120
notice that the timestamp is a float or a real number

130
00:05:18.120 --> 00:05:20.249
as we've selected when we created our table,

131
00:05:20.249 --> 00:05:23.000
but that's totally fine, timestamps can have

132
00:05:23.000 --> 00:05:26.004
you know decimal numbers if they are fractions of a second

133
00:05:26.004 --> 00:05:29.020
in some cases, in this case though, it is not.

134
00:05:29.020 --> 00:05:31.360
Notice that the watched column has a value of zero

135
00:05:31.360 --> 00:05:33.400
because we haven't seen this movie yet.

136
00:05:33.400 --> 00:05:36.390
All right, so I recommend that as we go along,

137
00:05:36.390 --> 00:05:38.810
every time we add a new function to the menu,

138
00:05:38.810 --> 00:05:41.710
you run your programme, try it out,

139
00:05:41.710 --> 00:05:42.740
look at db browser,

140
00:05:42.740 --> 00:05:45.280
make sure that the things that should be there

141
00:05:45.280 --> 00:05:47.000
are indeed there

142
00:05:47.000 --> 00:05:49.040
and that is going to let you find any problems,

143
00:05:49.040 --> 00:05:51.298
before your app gets really long and complicated

144
00:05:51.298 --> 00:05:53.270
and it's difficult to find stuff in it.

145
00:05:53.270 --> 00:05:54.710
In order to save you time though,

146
00:05:54.710 --> 00:05:57.280
I'm not going to be doing that every step of the way,

147
00:05:57.280 --> 00:06:00.070
but you should do that as you code along with me.

148
00:06:00.070 --> 00:06:02.120
So let's say that the user enters two,

149
00:06:02.120 --> 00:06:05.000
now we want to view upcoming movies,

150
00:06:05.000 --> 00:06:07.640
so we're going to create a new function here,

151
00:06:07.640 --> 00:06:11.920
print movie list, and that is going to take a set of movies,

152
00:06:11.920 --> 00:06:13.030
or a list of movies,

153
00:06:13.030 --> 00:06:16.293
and it's going to print upcoming movies,

154
00:06:17.270 --> 00:06:19.660
and then we're going to go through

155
00:06:19.660 --> 00:06:22.620
the movies for movie in movies,

156
00:06:22.620 --> 00:06:25.433
and then we're going to print the movie information.

157
00:06:29.150 --> 00:06:31.280
So here we're simply printing movie zero

158
00:06:31.280 --> 00:06:32.480
which is the title,

159
00:06:32.480 --> 00:06:35.250
and movie one which is the timestamp.

160
00:06:35.250 --> 00:06:39.740
And then we're going to add that.

161
00:06:39.740 --> 00:06:41.590
So that's just a few dashes there

162
00:06:41.590 --> 00:06:42.850
and a new line character,

163
00:06:42.850 --> 00:06:44.880
so that the next time that we print something out,

164
00:06:44.880 --> 00:06:47.710
it appears on the next line or two lines down really.

165
00:06:47.710 --> 00:06:49.550
Okay, now we can go ahead

166
00:06:49.550 --> 00:06:53.015
and do something like this, print movie list,

167
00:06:53.015 --> 00:06:56.630
but of course we do have to pass in a bunch of movies

168
00:06:56.630 --> 00:06:57.790
that we want to print out

169
00:06:57.790 --> 00:07:02.350
so we'll do movies equal database.get movies,

170
00:07:02.350 --> 00:07:04.270
and we're gonna pass in true.

171
00:07:04.270 --> 00:07:07.100
Remember two views upcoming movies,

172
00:07:07.100 --> 00:07:09.080
so we want to pass true to get movies,

173
00:07:09.080 --> 00:07:11.170
so that we get only the upcoming movies,

174
00:07:11.170 --> 00:07:13.690
I'll open this database.py here,

175
00:07:13.690 --> 00:07:16.590
and remember that get movies has the upcoming parameter

176
00:07:16.590 --> 00:07:18.230
that defaults to false,

177
00:07:18.230 --> 00:07:20.440
but we're passing in true so that we will only get

178
00:07:20.440 --> 00:07:22.050
the upcoming movies.

179
00:07:22.050 --> 00:07:24.840
Then remember to pass in the movies list

180
00:07:24.840 --> 00:07:27.950
into print movie list as the argument.

181
00:07:27.950 --> 00:07:30.700
Let's move over to three and print all the movies,

182
00:07:30.700 --> 00:07:32.210
what we have to do is actually,

183
00:07:32.210 --> 00:07:34.480
basically the same but instead of true here,

184
00:07:34.480 --> 00:07:35.510
just don't pass anything

185
00:07:35.510 --> 00:07:37.000
and it's gonna default to false,

186
00:07:37.000 --> 00:07:38.570
then we're gonna get all the movies

187
00:07:38.570 --> 00:07:40.310
and we're gonna print them out.

188
00:07:40.310 --> 00:07:41.930
However, something interesting to note,

189
00:07:41.930 --> 00:07:43.079
is that the print movie list

190
00:07:43.079 --> 00:07:45.670
always prints upcoming movies

191
00:07:45.670 --> 00:07:49.200
as a string, even when we've selected all movies.

192
00:07:49.200 --> 00:07:50.340
So instead of what were going to do

193
00:07:50.340 --> 00:07:53.470
is were going to accept a heading in here

194
00:07:53.470 --> 00:07:55.790
and we're going to instead of upcoming,

195
00:07:55.790 --> 00:07:58.910
just put the heading and then we're going to pass in

196
00:07:58.910 --> 00:08:02.173
either upcoming, or all,

197
00:08:03.610 --> 00:08:05.810
into the argument list there

198
00:08:05.810 --> 00:08:08.690
so that we will either print upcoming movies,

199
00:08:08.690 --> 00:08:10.200
or all movies.

200
00:08:10.200 --> 00:08:11.510
Now let's run this again,

201
00:08:11.510 --> 00:08:12.760
I know I said I wouldn't,

202
00:08:12.760 --> 00:08:14.380
but I wanted to show you what happens

203
00:08:14.380 --> 00:08:15.420
when you view the movies

204
00:08:15.420 --> 00:08:18.730
you got The Matrix on and in a timestamp of course

205
00:08:18.730 --> 00:08:20.130
because that is the value

206
00:08:20.130 --> 00:08:21.780
that's coming back from the column.

207
00:08:21.780 --> 00:08:23.750
And so we want to change that

208
00:08:23.750 --> 00:08:27.120
so that instead of the timestamp, we see the entire date.

209
00:08:27.120 --> 00:08:28.933
So again we're gonna use the daytime module

210
00:08:28.933 --> 00:08:32.440
to parse the timestamp into a daytime object

211
00:08:32.440 --> 00:08:36.320
and then the daytime object to converted into a string.

212
00:08:36.320 --> 00:08:39.210
So the movie date variable is gonna be our date time object,

213
00:08:39.210 --> 00:08:42.551
is datetime.datetime from timestamp

214
00:08:42.551 --> 00:08:46.720
on movie one, and then the human date is gonna be

215
00:08:46.720 --> 00:08:51.720
the movie date.STRFtime for string, format, time

216
00:08:52.150 --> 00:08:54.320
and then we pass in, just like we did here,

217
00:08:54.320 --> 00:08:57.310
a string representing the format that the want

218
00:08:57.310 --> 00:08:59.430
the timestamp to be converted to.

219
00:08:59.430 --> 00:09:01.550
And here I'm not gonna use the M or Y

220
00:09:01.550 --> 00:09:04.440
I'm gonna use B, D, Y,

221
00:09:04.440 --> 00:09:05.750
I think that's a little bit more readable

222
00:09:05.750 --> 00:09:06.770
when you're printing things out.

223
00:09:06.770 --> 00:09:10.170
But that is gonna show the month, day and year,

224
00:09:10.170 --> 00:09:12.260
but the month is gonna be a three letter string,

225
00:09:12.260 --> 00:09:13.700
now instead of movie one here,

226
00:09:13.700 --> 00:09:16.090
we want to print the human date.

227
00:09:16.090 --> 00:09:17.260
All right, let's show you that

228
00:09:17.260 --> 00:09:19.320
so that you can see it works,

229
00:09:19.320 --> 00:09:22.340
you an see that The Matrix was released on Jan,

230
00:09:22.340 --> 00:09:23.793
oh one, 2002.

231
00:09:24.730 --> 00:09:26.660
If you wanted to show the day first,

232
00:09:26.660 --> 00:09:28.470
then that's totally fine, just walk them around,

233
00:09:28.470 --> 00:09:31.220
say something like D, B, Y

234
00:09:31.220 --> 00:09:33.290
and you can do that if you prefer.

235
00:09:33.290 --> 00:09:36.760
All right so we've got inputs one, two and three sorted

236
00:09:36.760 --> 00:09:38.690
now we need to move on to four,

237
00:09:38.690 --> 00:09:41.080
which is to mark a movie as watched.

238
00:09:41.080 --> 00:09:43.280
Here what we're gonna do is ask the user

239
00:09:43.280 --> 00:09:45.006
what movie they want to mark as watched

240
00:09:45.006 --> 00:09:46.367
and then we will go and pass that

241
00:09:46.367 --> 00:09:48.250
in to our database function.

242
00:09:48.250 --> 00:09:51.760
So again, we're gonna create prompt, watch movie

243
00:09:51.760 --> 00:09:53.890
and this function here is going to ask the user

244
00:09:53.890 --> 00:09:56.920
for the movie title, we're gonna ask them something like

245
00:09:56.920 --> 00:09:59.223
enter movie title you've watched,

246
00:10:00.580 --> 00:10:04.040
and then we're going to simply do database.watch movie

247
00:10:04.040 --> 00:10:05.530
and pass in the movie title,

248
00:10:05.530 --> 00:10:07.260
as you know that's going to pass that into the query

249
00:10:07.260 --> 00:10:09.910
and that's gonna run updating our database.

250
00:10:09.910 --> 00:10:12.230
Then in here we can say prompt,

251
00:10:12.230 --> 00:10:14.050
watch movie and that's it.

252
00:10:14.050 --> 00:10:17.670
The last thing is to print out the watched movies.

253
00:10:17.670 --> 00:10:19.850
Again, we're gonna use the thing that we used

254
00:10:19.850 --> 00:10:22.250
in two and three to print out the upcoming

255
00:10:22.250 --> 00:10:25.530
or all movies, but now instead of database.get movies

256
00:10:25.530 --> 00:10:28.070
we're gonna say get watched movies

257
00:10:28.070 --> 00:10:29.700
and that's gonna give us all the watched movies

258
00:10:29.700 --> 00:10:33.330
and in here we're gonna say watched movies instead.

259
00:10:33.330 --> 00:10:34.510
That's really about the gist of it

260
00:10:34.510 --> 00:10:36.590
and I'm sure you can see that now,

261
00:10:36.590 --> 00:10:39.400
the app.py code although it's you know

262
00:10:39.400 --> 00:10:40.560
it's starting to get a little bit long

263
00:10:40.560 --> 00:10:42.380
and there's a few date parsings

264
00:10:42.380 --> 00:10:43.656
and things like that in here,

265
00:10:43.656 --> 00:10:47.360
it's actually pretty easy to understand.

266
00:10:47.360 --> 00:10:49.780
All we're going is we're getting user data

267
00:10:49.780 --> 00:10:51.173
and we're interacting with a database

268
00:10:51.173 --> 00:10:54.060
passing in or retrieving data from it.

269
00:10:54.060 --> 00:10:56.640
As long as the database holds our truth

270
00:10:56.640 --> 00:10:58.040
about our application or the data

271
00:10:58.040 --> 00:11:00.290
that our application is working with,

272
00:11:00.290 --> 00:11:02.900
then all of the Python code is doing really

273
00:11:02.900 --> 00:11:05.450
is interacting with the database and adding,

274
00:11:05.450 --> 00:11:08.090
changing or retrieving stuff.

275
00:11:08.090 --> 00:11:10.700
Also remember that because all our data interactions

276
00:11:10.700 --> 00:11:13.900
are encapsulated in the database.py file,

277
00:11:13.900 --> 00:11:16.720
then all we have to do is make sure that that reflects

278
00:11:16.720 --> 00:11:19.350
what we want to use and we can just use that

279
00:11:19.350 --> 00:11:21.270
from other places and we don't really have to worry

280
00:11:21.270 --> 00:11:23.062
about how that works.

281
00:11:23.062 --> 00:11:26.140
For example, we can easily change this

282
00:11:26.140 --> 00:11:29.250
so that instead of SQLite is uses PostgreSQL

283
00:11:29.250 --> 00:11:31.960
and as long as we keep the same function names

284
00:11:31.960 --> 00:11:36.450
app.py is not gonna care what it's using in the background.

285
00:11:36.450 --> 00:11:37.710
All right thank you guys for watching

286
00:11:37.710 --> 00:11:40.470
remember to run the code, verify everything's working

287
00:11:40.470 --> 00:11:41.880
before you move on.

288
00:11:41.880 --> 00:11:43.040
I hope you enjoyed this video

289
00:11:43.040 --> 00:11:44.690
and I'll see you in the next one.

