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