Jose: Hi guys and welcome back.
In this video, we're going to be completing
our user menu and finishing stage one
of our application.
Just as a reminder, let's quickly go through
the code that we've got right now.
This here is app.py our main user interaction file.
We've got our menu, that let's users pick from their options
then we print a welcome, create the tables
with our database and then we have our loop
that runs the menu over and over
allowing the user to pick different things
and we're gonna do different things
depending on their input.
Then we've got database.py,
where we have all our queries,
and a few functions that allow us to interact
with a database from another Python file.
We've got create tables, that runs and creates the tables,
we've got add movie, to insert a new movie
into the database, given two arguments.
We've got get movies that retrieves movies
that might be upcoming or old movies if this is false.
Watch movie, that sets a movie to watch
by updating the table.
And get watched movies which only returns
those movies that have already been watched.
So let's go over to app.py
and start filling in this menu.
The first thing we're gonna want to do
is to add new movies.
So what I usually like to do here
is to go ahead and create a prompt
add movie function just because this function here
is going to ask the user for a couple of pieces of data
it might parse some dates and call the database file,
so it makes sense to have it in it's own function.
Remember that functions in Python
like having two lines at the start and at the end.
So I'm just gonna add that in to give it a bit more
room to breathe.
So the first thing we want to do here
is find out the data we need from the user,
so that's gonna be title,
and we're gonna add movie title in here
and we also need the release date.
So we're gonna do input of release date,
in dd, mm, yyyy format.
This just means day, month, year,
you can pick whichever date format you want in here
we're gonna learn how to parse those
into a timestamp just now.
So now that we've got a string representing the date
presumably in this format,
we can use the date time module to parse it.
So let's go over to the top and above our import
we're gonna import the date time module.
The reason why I put it above our import by the way,
is because normally in Python
you will import you know the standard library,
the modules that come with Python
at the very top you're own modules after that.
So that's why I'm doing that.
And here we are going to then go ahead and parse this.
The first thing we're going to do is
to use a date time module to parse
the string into a date time object.
When we have a date time object,
we can very easily get the timestamp that it represents.
So we will do parsed date,
is gonna equal to datetime.datetime
and that just gives us the date time class
inside the date time module.
And then we're gonna use STRP time for string, parse, time
to parse the string into a date time.
And this takes two arguments,
the release date string in this case,
which should be in this format,
and then it also takes a format string
or something to tell the date time module
what format this string is in.
So that is gonna be percent D
and that tells the date time module that
we've got essentially two numbers as the date of the month,
then percent M that tells the date time object
that we've got the two numbers as the month of the year,
and then percent uppercase Y that tells the date time module
that we've got four digits as the year.
Notice that these are separated by dashes
just like we have in our user prompt there
which means that we should expect this here
to be matching the input that the user typed.
Now that we've got that, we have a date time object.
And as you can remember from the database module,
we can now do timestamp equal parsed date.timestamp
calling the method in the date time object
gives us the timestamp that it represents.
And now that we have all that,
we can simply call database.add movie,
pass it in the title and the timestamp,
and we're good to go, that should be added to the table.
We can simply now call this function
from the user menu
and that should be asked with the ability to
add new movies.
I would recommend at this point
to run your code and see if it works.
So we're gonna run the code,
go into the db browser to check out the file
that this creates and see if a movie appears in there.
So we'll do Python app.py,
and then we're gonna add the new movie
for example The Matrix by,
oh I actually don't know when it was released,
let's say the first of January, 2002
and there we go.
So now what we wanna do is go into db browser
and see if we have inserted a timestamp,
a long set of numbers as the release date
and of course whether the movie is there
and has the correct name.
So I'm gonna exit by typing six
and then let's go over to db browser.
So we're here in db browser
and you can see that I've got my movies table created,
we can go ahead into browse data
and you'll see that The Matrix is here,
and we've got our timestamp there,
notice that the timestamp is a float or a real number
as we've selected when we created our table,
but that's totally fine, timestamps can have
you know decimal numbers if they are fractions of a second
in some cases, in this case though, it is not.
Notice that the watched column has a value of zero
because we haven't seen this movie yet.
All right, so I recommend that as we go along,
every time we add a new function to the menu,
you run your programme, try it out,
look at db browser,
make sure that the things that should be there
are indeed there
and that is going to let you find any problems,
before your app gets really long and complicated
and it's difficult to find stuff in it.
In order to save you time though,
I'm not going to be doing that every step of the way,
but you should do that as you code along with me.
So let's say that the user enters two,
now we want to view upcoming movies,
so we're going to create a new function here,
print movie list, and that is going to take a set of movies,
or a list of movies,
and it's going to print upcoming movies,
and then we're going to go through
the movies for movie in movies,
and then we're going to print the movie information.
So here we're simply printing movie zero
which is the title,
and movie one which is the timestamp.
And then we're going to add that.
So that's just a few dashes there
and a new line character,
so that the next time that we print something out,
it appears on the next line or two lines down really.
Okay, now we can go ahead
and do something like this, print movie list,
but of course we do have to pass in a bunch of movies
that we want to print out
so we'll do movies equal database.get movies,
and we're gonna pass in true.
Remember two views upcoming movies,
so we want to pass true to get movies,
so that we get only the upcoming movies,
I'll open this database.py here,
and remember that get movies has the upcoming parameter
that defaults to false,
but we're passing in true so that we will only get
the upcoming movies.
Then remember to pass in the movies list
into print movie list as the argument.
Let's move over to three and print all the movies,
what we have to do is actually,
basically the same but instead of true here,
just don't pass anything
and it's gonna default to false,
then we're gonna get all the movies
and we're gonna print them out.
However, something interesting to note,
is that the print movie list
always prints upcoming movies
as a string, even when we've selected all movies.
So instead of what were going to do
is were going to accept a heading in here
and we're going to instead of upcoming,
just put the heading and then we're going to pass in
either upcoming, or all,
into the argument list there
so that we will either print upcoming movies,
or all movies.
Now let's run this again,
I know I said I wouldn't,
but I wanted to show you what happens
when you view the movies
you got The Matrix on and in a timestamp of course
because that is the value
that's coming back from the column.
And so we want to change that
so that instead of the timestamp, we see the entire date.
So again we're gonna use the daytime module
to parse the timestamp into a daytime object
and then the daytime object to converted into a string.
So the movie date variable is gonna be our date time object,
is datetime.datetime from timestamp
on movie one, and then the human date is gonna be
the movie date.STRFtime for string, format, time
and then we pass in, just like we did here,
a string representing the format that the want
the timestamp to be converted to.
And here I'm not gonna use the M or Y
I'm gonna use B, D, Y,
I think that's a little bit more readable
when you're printing things out.
But that is gonna show the month, day and year,
but the month is gonna be a three letter string,
now instead of movie one here,
we want to print the human date.
All right, let's show you that
so that you can see it works,
you an see that The Matrix was released on Jan,
oh one, 2002.
If you wanted to show the day first,
then that's totally fine, just walk them around,
say something like D, B, Y
and you can do that if you prefer.
All right so we've got inputs one, two and three sorted
now we need to move on to four,
which is to mark a movie as watched.
Here what we're gonna do is ask the user
what movie they want to mark as watched
and then we will go and pass that
in to our database function.
So again, we're gonna create prompt, watch movie
and this function here is going to ask the user
for the movie title, we're gonna ask them something like
enter movie title you've watched,
and then we're going to simply do database.watch movie
and pass in the movie title,
as you know that's going to pass that into the query
and that's gonna run updating our database.
Then in here we can say prompt,
watch movie and that's it.
The last thing is to print out the watched movies.
Again, we're gonna use the thing that we used
in two and three to print out the upcoming
or all movies, but now instead of database.get movies
we're gonna say get watched movies
and that's gonna give us all the watched movies
and in here we're gonna say watched movies instead.
That's really about the gist of it
and I'm sure you can see that now,
the app.py code although it's you know
it's starting to get a little bit long
and there's a few date parsings
and things like that in here,
it's actually pretty easy to understand.
All we're going is we're getting user data
and we're interacting with a database
passing in or retrieving data from it.
As long as the database holds our truth
about our application or the data
that our application is working with,
then all of the Python code is doing really
is interacting with the database and adding,
changing or retrieving stuff.
Also remember that because all our data interactions
are encapsulated in the database.py file,
then all we have to do is make sure that that reflects
what we want to use and we can just use that
from other places and we don't really have to worry
about how that works.
For example, we can easily change this
so that instead of SQLite is uses PostgreSQL
and as long as we keep the same function names
app.py is not gonna care what it's using in the background.
All right thank you guys for watching
remember to run the code, verify everything's working
before you move on.
I hope you enjoyed this video
and I'll see you in the next one.