Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 4: Journal app and file I/O
Lecture: Building the event loop

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So let's jump into PyCharm and build that journal app. I want to start as always by adding our program.py,
0:09 and we are going to add other Python files and start using them but let's first start here and focus on a few concepts before we move on.
0:16 Now, as you guys know, I am kind of fond to sketching out the overall flow of the app in one method and then breaking the pieces into smaller ones.
0:24 So I want to define again a main method here. And the first thing we want to do is sort of print the header
0:29 remember that is basically the little dashes across the top we are always doing, and then we are going to go around and around
0:36 in kind of an event loop asking the user what operation do you want to perform I'll do the operation and we'll query for user input again.
0:43 So I'll just call that run_event_loop(). So we are going to need these two methods defined somewhere and let's define them first up here,
0:51 we'll say def print_header(), and we'll just say pass and we'll say run_event_loop()... pass.
0:59 Now, let's one final time sort of do this what I would call the wrong way but we are just going to invoke the main() method
1:05 as you know if we don't do that nothing happens. Let's do print() here just I'm going to our header piece.
1:14 So that we can see that main() is actually having some kind of effect, no run configurations, so we can generate one by saying right click run
1:22 and here is our little journal app piece. Now, to me the order of defining these methods is really wrong.
1:29 I like to see the high level bits, like what is happening overall and then the little individual pieces.
1:35 So let's take this and naively move this of the top here, there we go, and now our main() we can sort of, as soon as we open the file
1:42 we see what the major operations are happening in our program here and then down we have the details.
1:49 So if I run this though, things are not going to be very happy. 'print_header' is not defined. So let's just take a really quick moment to think about
1:57 how Python defines methods and classes and these sort of structures. In languages like C and C++ there is a compile step and
2:05 it actually goes to the code and it compiles in it and writes out the machine instructions and the definitions for these methods.
2:11 But in Python there is not really this compile step. There is kind of a tokenizing step but let's ignore that for a moment.
2:18 Basically there is not really a compile step so when you execute this block, when you execute line 2 to 4,
2:25 what you are actually doing is you are defining a method main(). And when you execute line 9 to 12 you are defining the method print_header().
2:33 The problem is we are executing main() which depends on print_header() before we have effectively defined it by running this.
2:40 So we can solve this problem by just putting main() at the very bottom. Again, there is going to be another better way to do this
2:46 that we are going to come back to at the end of this example, alright. Let's run it one more time,
2:52 ok, everything is looking good, we got our high level code up here, we are working on the details down here. So let's work on that event loop part.
2:59 Remember, we basically have a couple of operations the user can choose, they can list their journal entries,
3:05 they can add a journal entry or they can hit X to exit. So let's sketch out that sort of input conditional logic
3:12 and then we'll write the details of it. So first thing we are going to do is let's just do a little print() statement to let the user know
3:18 hey your journal app is running. I'll say something like... What do you want to do with your journal?
3:25 And then I want to capture the user input and we'll call that command we are using the input() function like we have been, like so.
3:33 And now we can compare this against various options, so the 3 things they could type according to our menu option here is they could type L, A or X.
3:42 So we'll say if command == L then let's just print() the L for a moment, and then else if, or elif command == A we'll print A...
3:57 now of course, this is just going to run one time but let's just test this really quick.
4:02 So we are going to run it says... What do you want to do with your journal?, let's suppose we want to list it, so if we say L it says
4:09 great, it echos out the L here if we run it again and this time we say A, it's going to echo out the A, that we put in our print statement.
4:17 Of course, we want this to go around and around, so let's write some code here, let's say while the command != X, we want to do this,
4:27 now of course if we try to run it's going to freak out, because command is not defined, you can see local variable command reference before assignment,
4:35 so let's just initialize it to nothing to start which obviously is not going to be X
4:40 after it goes into our loop the first time we should have real input, so let's try this, let's say L, L, A, X, perfect,
4:51 maybe we could add a little goodbye a bit here at the end, just to let everybody know we are done with our app.
4:57 So we pretty much have the structure working, let's write one more bit here, so if I hit L I get the L again,
5:02 but if I hit something like boo, it just sort of ignores it, it doesn't do anything, let's maybe give him a warning that we don't understand that,
5:11 so we'll come down here and say elif command != to X or any of the other options, because we are already made this far,
5:19 I'll say print("Sorry we don't understand... now notice, we have our opening quote here and the code that is naturally in the string
5:29 so we are going to do this I told you there were time when you would want to use double quotes for your strings
5:34 because it makes your life easier, this is one of them, we'll say we don't understand, and whatever the command was,
5:39 we can put that in little single quotes, like this and we'll say format, ok, great. So let's run this one more time, we'll say L, A and if I say boo,
5:50 great!, but there is a few things that are not quite yet right with our loop so if I say lower case l, they will say sorry, we don't understand 'l'
5:59 because we were checking against upper case L, and things like that, so let's stop this and fix that. So we can come down here and we can say
6:08 lower on the string and we want to put that on all these put that here and make sure wherever we are comparing we are now comparing against lower case,
6:16 now if I can say 'L', 'l', it seems to understand 'a', 'A', but if I put like a space and L all of the sudden it doesn't understand that anymore,
6:27 so we can do one more thing to sort of make our input as nice as possible, and we can do a strip here, right, so now this should be really robust,
6:36 we can say like L space space l, A, X, done goodbye. So I think we have that working well of course, if you care about performance
6:47 or even code repetition we are doing this 3 times, it might be nicer to do something like this do it once and then centralize that
6:55 so we'll say lower().strip() and then we can just work with it assuming it's got no white space and it's always lower case.
7:06 All right, let's finish sketching out the methods we are going to write and then we'll move on to work with data.
7:10 So we basically need two more methods, one is going to be list entries, and I'll just print() Listing..., for now
7:19 just so we can see that this is working, and we need to define add entry and I'll do a print() Adding..., darara... like that
7:26 and let's go over here and replace these pieces, we'll say list entries and down here this will be add entry, one more time, make sure it runs,
7:34 list, add, exit, everything looks great.


Talk Python's Mastodon Michael Kennedy's Mastodon