Jose: Hi guys, and welcome back.
In this video, we're going to start using Python lists
as a database table instead of SQL Lite.
We're going to do this first because it's simpler,
then when we want to use SQL Lite,
the change over will be very straightforward.
When you think about it,
lists and database tables are very similar.
They are both used to store individual items.
Inside a database table, we store rows of data.
And each row can have multiple pieces of data,
one per column.
Inside the list, we can store anything we like,
including dictionaries,
each dictionary can contain multiple pieces of data as well.
So here, I'm going to show you sort of a sample dictionary
that we might store in our list.
So here we've got our entries list,
this is what we're going to be storing in our application.
And you can see that it's a list,
and inside it we've got four dictionaries.
You can imagine that these things are things a user created,
so we've got the content key
that contains what the user learned on that day,
and the date key that contains when they learned it.
Initially, our entries list is going to be empty.
And we will be creating a dictionary
every time the user adds a new entry,
and putting that dictionary into the list.
So this is just an example.
But let's go ahead and write this code out.
The first thing we're going to do
is create a separate Python file
to host the logic that interacts with our data store.
At the moment, as I said, this is going to be a list,
later on it will be a database.
So I'm going to open my file viewer there,
create a new file called database.py.
Then I'm going to close the file viewer.
Here, we're going to create our entries list,
which is this empty list initially.
And now we're going to create a couple of functions
just to allow us to add a new entry,
or view existing entries.
So here is the add_entry function.
Notice I've used the pass keyword, which means do nothing,
to allow Python to still process this function as valid,
it does need the indented block in there to be valid.
But at the moment, this function does nothing.
And we're going to do something similar for view_entries,
with pass as well.
So now we've got our two functions,
we could go into app.py and import these functions.
And that is what we will do, indeed, in just a moment.
To begin with, we're going to use this add_entry function
to ask the user for what they've learned
and when they learned it, and add that to the entries list.
So we will create this entry content variable,
which is going to be an input
of what have you learned today,
and also the entry date,
which is another input of enter the date.
So here, we've got two strings.
The date is not being validated or anything like that,
as we spoke about earlier on in the presentation,
and what have you learned today,
the user can enter anything they want.
Now that we've got those two pieces of data,
we can create a dictionary with them.
And the dictionary should have a content key
that is going to be the entry_content,
and a date key, which is the entry_date.
Now creating the dictionary on its own,
of course, it's not going to put it into the list.
So we do have to do that ourselves.
Entries.append, and we will add it in there.
So here we can see that we are appending
into the entries list,
our new dictionary that we create here.
To view the entries, we can similarly go through the list
and print out each entry information.
So we can do for entry in entries,
here this for loop creates the entry variable
every time we go through one item of the entries list.
And the entry variable initially
will be the first item in the entries list.
We will then run this indented block of code here.
We will go back,
and entry will become the second item in the entries list,
and then we will run this block of code, and so on.
Again, this is all covered in the Python refresher.
So if you have any questions about for loops,
or dictionaries, or lists, and things like that,
do go back and check that out.
Now, for every entry in the entries list,
we want to print out the information.
So we're going to say print, and then in an f string,
we're going to put the entry, date.
And then in a new line,
we're going to put to the entry content.
And then we're going to put two new lines.
So a couple of things going on here,
this f string is a little bit complicated.
We are interpolating into the string,
the value of accessing the date property of the entry.
So notice that here, we're using double quotation marks
at the end and the start of the string.
The access of the data property in the dictionary
must use single quotation marks,
otherwise, Python would get confused
and it would think that and this is a string,
and this is another one, it doesn't like that.
So make sure to use single quotation marks there.
That's the date of the entry.
Then we're putting a new line with a backslash n character,
and then we're interpolating the content
into it's a new line,
and finally, we're putting two new lines there,
so that when we print the next entry,
there's going to be a bit of separation between the two.
Okay, so now we're going to save this file,
and we can go ahead and use it in our app.py.
So make sure to save it, we can go over to app.py,
and now we can import those two functions
that allow us to interact with our data store in here.
So we do from database import add_entry, and view_entries.
Now once we've got that, we can go down into our menu
and just use them instead of print adding and viewing.
So we'll do add_entry here, make sure to call the function,
and view_entries in here.
Now if we save this and run it,
you'll see that it all works.
So I'm going to open up the terminal down here.
I'm just going to make it a little bit bigger.
I know the text here is a bit small,
but hopefully you can read it there.
And now I'm going to run Python3.8 app.py.
Remember, we've used the walrus operator,
or the assignment expression,
so we need Python 3.8 for this code.
Here we've got our menu,
we can then add a new entry, for example.
And when we type one, of course,
we are running the add_entry code,
that's taken us to database.py,
and now we are currently on this line.
So what have we learned today?
We've learned how to use Python lists as a data store,
and enter the date, the date can be today.
Notice that there is no validation on this date,
so we can enter whatever we want.
Now, every one of you entries, we enter two
and then we get today,
and then a new line, we get the content of the entry.
And then we get two new lines.
And then we would get the next one if there was more,
but now we just get the menu.
Finally, we can enter three to exit.
Okay, just gonna make that a bit smaller again.
And I'm going to clear it which gets rid of all that text.
Now there is a glaring problem
in the way we've structured this application.
And this problem may not be all that obvious
if you are not that familiar with coding,
but I'll tell you that it is a glaring problem
for any developer that's a bit more experienced.
The problem is that the database.py file
is doing really two things.
The database.py file is interacting with the user,
gathering data and it's also storing data in a database.
So it's doing these two separate things.
And it can become confusing and complicated
to then change the data that we're storing,
or how we're gathering it.
It's usually better to have these two things separated,
have one part of our application
dealing with getting the user input,
and have a different part dealing with
interacting with a data store.
Similarly, the view_entries function
is retrieving data from the data store,
and it's also outputting data to the user.
So it's doing those two separate things as well.
Overall, this file here, gets data,
processes data, and displays data.
So yeah, it's not a great file at the moment,
but we can make it better.
Up until before we made this database.py file,
the app.py was in charge of dealing with user input,
and displaying information.
So why don't we keep it that way,
we will move all the user input gathering
and data display to app.py.
And we will keep database.py
as only dealing with the database.
So how to do that is we're going to move these two inputs
over to app.py.
And we're going to pass their contents to add_entry.
So I'm going to add two new parameters here,
entry_content and entry_date.
And now, this function here is only concerned
with taking whatever data it receives
and putting it in a database.
It is going to be app.py going over there now,
that is going to ask the user for data.
And then it's going to call the add_entry function
with the content and the date, just like that.
So now we're getting the user input,
storing it in a variable,
passing the value of that variable to add entry,
and then add_entry is receiving that value
and storing it in the database.
Similarly, if we go to database.py,
we've got here this for entry in entries
that is displaying information.
Instead of that, we're just going to make view_entries
responsible for getting the data from this list,
and giving it back to the caller,
whoever will use this function, so we'll do return entries.
At the moment, this may not seem all that useful.
But later on when we're actually interacting
with a database, you'll see why this becomes more important.
And I've copied the for loop there.
And I'm just going to move it down here
for entry in entries there.
And I'm going to get to the return value of view_entries
and put it in a variable as well.
Notice that you could grab this function here
and put it directly in the for loop,
that's totally fine as well, either option works.
Now notice that the function is called view entries,
which suggests that you're going to be able
to see something, but the view_entries function
is no longer concerned with actually displaying data,
so we're actually going to rename it to get_entries instead.
So we do have to rename it in database.py down here
and in the import as well.
This makes app.py more complicated.
But it makes the whole application simpler
because now reading app.py, we can have a clear idea
of what database.py is doing.
It adds and gets entries from a data store.
And we've also made it clear exactly
what data we're adding to the database,
previously, that was hidden inside database.py
we're calling the function,
and we didn't really know by reading this,
whether database.py was asking for two pieces of data or 10.
Now it's a bit clearer.
And app.py as you read through it,
you can see exactly what the user is going to see as well.
We could improve the code inside app.py a bit further
by splitting it into functions.
So this stuff here could go into its own function.,
this stuff here could go into its own function.
We're going to do that just now as well.
We need to define functions before they're used,
so we're going to go up here
and create a prompt_new_entry function.
And this is going to do all of this stuff.
So I'm going to copy that into that.
And down in here, we're just going to call prompt_new_entry.
So I prefix these functions with prompt
because they're actually interacting with the user,
I like to make that a bit clear.
And then we're going to create another function
for viewing entries,
and this time it is going to be called view_entries.
But I'm going to make it take in the entries
from the database itself.
Then this one here is just going to go through the entries
and print them out.
Now when we've got the entries,
we can just call it view_entries
with the entries list that we got from the database.
Now this is starting to become a little bit too superfluous,
this entries database,
so I'm just going to copy that and put it in there,
get rid of that variable.
So while this code is now a bit longer than before,
it is a bit simpler because it's more divided into parts.
When you read this function,
you know that we're going to be adding a new entry.
And when you view this one,
you know we're going to be displaying entry information.
And finally, the loop now is very simple.
If the user input is one, then we prompt for a new entry.
And if it's two, then we view the entries.
So hopefully you agree with that.
Now, in the next few videos,
we're going to be replacing the Python lists for SQL Lite.
So let's go and do that in the next video.
I'll see you there.