Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Add An Entry4:04 with Kenneth Love
If we're going to have a diary, we have to be able to add entries to it. Let's use
sys.stdin so we can capture new lines and tabs and all sorts of other goodies.
sys- a Python module that contains functionality for interacting with the system
sys.stdin- a Python object that represents the standard input stream. In most cases, this will be the keyboard
[MUSIC] 0:00 Well, we have a table and a menu for actions. 0:04 Let's write the function that'll actually let us add entries to the database. 0:07 Python comes with a library named SYS that relates to system functionality on 0:10 your computer. 0:14 We're gonna use a great feature of that library, STDIN, so 0:15 that we can capture an entry that has new lines in it. 0:18 Since we're going to be using the sys library, we should probably go ahead and 0:21 import it. 0:25 [BLANK_AUDIO] 0:26 Not that it's really important, but 0:28 I'd like to point out that we have the stuff that comes from Python here, and 0:30 then a blank line, and then the stuff that comes from a third-party library. 0:33 This is a really good and really common way of organizing your imports. 0:38 I suggest that you kinda stick to it. 0:41 Okay. 0:44 Let's come down here and check out our Add Entry function. 0:45 Cuz this is where we're gonna use sys. 0:48 So, it's gonna capture everything that the user types, 0:51 until they enter what's known as an End of File key sequence, an EOF key sequence. 0:54 The way that you get this End of File is by pressing CTRL and D at the same time. 0:59 So, let's tell the users that. 1:05 So print, Enter your entry, press CTRL D when finished. 1:08 Kay. 1:17 Now, we need to actually capture their input. 1:19 And this is where the STDIN that I talked about comes into play. 1:22 So let's call this data, 1:26 and it's gonna be sys.stdin, which is just like the keyboard. 1:28 And then read, bringing in all the data, 1:32 everything that comes in from here, and then, oops, and then strip. 1:35 Get rid of any of the white space on either side of that. 1:39 Now though, we need to make sure that we actually got something from them. 1:42 So let's say if data. 1:50 So cool, we've got data. 1:53 Something actually came in. 1:54 And then, let's make sure that they want to save this. 1:56 So if input save entry? 1:59 And we're gonna have a Y and an N, and then we're gonna lowercase that. 2:04 So if that comes in, and it doesn't equal n, input comes in, 2:10 they answer with anything other than n upper case or lower case doesn't matter, 2:16 but anything other than the letter n [LAUGH], then we wanna do something. 2:21 And what we wanna do is we wanna call entry.create cuz we're creating a new 2:25 entry, right? 2:29 Did you forget about the model? 2:30 I almost did. 2:32 And we're gonna say the content of our entry is the data that they just typed. 2:33 And then let's print out saved successfully. 2:39 And that's it. 2:44 We don't have to do anything else. 2:46 Well we do have to add another blank line there. 2:49 So we're creating an instance for printing out a message. 2:52 I'm pretty sure that covers all the stuff we have to do on adding an entry. 2:55 So let's try it. 2:59 Come right down here ./diary. 3:03 Oh, I love that. 3:04 So much better. 3:06 All right, and let's say A for add an entry. 3:07 And I'm gonna my entry. 3:10 So I'm gonna say working with databases has been really neat. 3:12 I'm glad I can show this off. 3:21 I can even have multiple lines. 3:28 Okay. 3:31 And then I hit CTRL and D. 3:32 It comes back with save entry. 3:35 So I'm gonna say yes. 3:37 Actually I can just press Enter. 3:40 And it came back with Saved Successfully. 3:42 That's great. 3:44 Let's try doing one where I don't wanna save it. 3:46 I don't want to save this entry. 3:48 Ctrl+D, no don't save that. 3:51 It just goes straight back. 3:54 I didn't save anything. 3:56 It just takes me straight back to the menu. 3:56 I think this is actually working really, really well. 3:58 Now that we can add things, we need to be able to see them. 4:00 Let's tackle that in the next video. 4:03
You need to sign up for Treehouse in order to download course files.Sign up