Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 4: Journal app and file I/O
Lecture: Importing and using additional Python files
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Our goal is to add file I/O persistence to our journal, but I feel like our app is getting too complicated
0:09
and this code that we are writing is actually doing several things it's both interacting with the user and dealing with the UI
0:16
as well as kind of managing this journal data and it would be much better if we could separate those.
0:22
On one hand, we have this journal thing that takes care of itself, and we have our UI bits and they can just interact with that journal.
0:28
One really nice way to do that in Python is to create a separate module or a separate file. So let's call this journal, here we are initializing,
0:38
we are creating this journal and this is ok, it works but remember,
0:43
we'd like to initialize this from a file where we have saved an existing journal load it up,
0:47
create a new one if the file doesn't exist, all that kind of stuff. So this is going to get way more complex instead of this one line here.
0:54
So let's move that over into our journal code and we'll call it something like load. So over here we are going to define a load method,
1:02
and let's suppose it's going to take the name of the journal. And, it's counter part, we are going to write later,
1:08
it's going to be saved and again this is going to take a name. Now for the load, let's just temporarily write the code
1:16
to basically do what we were doing before, just create an empty journal. And we are storing the journal data as a list
1:23
so let's do this and just to remind ourselves, let's add a little to do. Ok, so we'd like to populate this from the file if it exists,
1:30
similarly, we'd like to save something here, we are going to need a name, we are also going to need the journal data, I want to pass it in.
1:39
So, how do we access these methods which we are going to fill out with more interesting bits in a moment, how do we access those over here?
1:48
First of all, let's just try to write it, we could try to write load and there is obviously no load here,
1:54
we could write journal.load there is no journal either, so obviously like before, when we were working with
2:01
say the random class and various other pieces, we need to import it, so the way you do this is the same whether you are working with a built in module,
2:10
an external module you've got from PyPi, or just one of your own scripts that you are going to load up and consume.
2:17
So here we are going to say import journal, you can see PyCharm is saying yep, that seems like a good thing to do and down here
2:23
we can say something like journal. and there is our load and our save. So we can write it like this.
2:31
Now notice, we said import journal, format for a moment, we said import journal and down here we say journal.load.
2:40
So this style is referred to has name spaces, there are other ways we could import this, I could've said something like this instead,
2:46
I could've said from journal, import and I could just name off the things like load and save and then down here
2:54
I can just call load without the journal, right. Now, it depends on the method you are working with, it depends on how frequently you are using it
3:02
but my preference is definitely to use this style because it's extremely explicit where it comes from.
3:09
If I just call load it's not entirely obvious where it comes from. Now, there is a way to write this code where it's even less obvious
3:17
say, we just want to report everything from that journal module, and just put a star like this, from journal import star,
3:24
that would be as if we named every single thing that was in there, and that's even less good, it's less explicit
3:29
and you can run into the possibility if name clashes like imagine 2 different modules both have the method load, which one are you going to get?
3:37
It gets complicated, so I would say avoid this one, prefer this one sometimes this one is totally good too, so we are going to say import journal,
3:46
and we are going to come down here and say load and you can see that PyCharm is saying we want to give it a name so let me just create a variable for
3:52
our named journal, we'll can it journal name, and it can just be something like default. Something along this line, Ok.
4:00
And down the end, let's make sure we do a save so we'll say journal.save and we'll give it a name, and the data. So the save really does nothing,
4:10
remember it was just pass, this load really is just returning the new instance to the list,
4:15
but that should mean it really just works exactly like before, so let's add first item, second item, and list them out,
4:23
hey it looks like it's working, and it exits. Ok, so we were able to use the separate file to store some of our logic in here
4:32
if you come down here you can see there is something I am not a really big fan of down here with this add entry,
4:38
is we are directly working with this data structure, I'd much rather move this over into the journal pieces.
4:46
However, not this part because this has to do with the UI, so instead of writing this code I could write journal.add_entry,
4:55
and just pass the text here instead of this, I could of course go over to the journal file, write add entry just to kind of as you see it here
5:02
but let me just show you a cool trick with PyCharm. So PyCharm knows that the journal.py file relates to this
5:08
name space here and it knows this does not exist, so if we hit alt enter, it'll say hey, would you like me to write this method for you over here?
5:15
Yes. please, so we can actually just use this alt enter trick to sort of
5:21
avoid a lot of typing in and get everything exactly like we were planning on there. So remember this was like this,
5:29
and I forgot to pass the journal data so kind of unfortunate, now this probably would be much better model as a class, but hey, it is what it is.
5:41
Ok, so I think we moved the logic that manages the journal over into this journal.py module,
5:47
now we haven't written the real complex code yet, but we are about to, before we do, let's just review this whole import story,
5:54
this is one of our core concepts.