Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 8: File Searcher App
Lecture: Sketching out the search app

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So let's just take a moment and sketch out the flow of this application just to build a skeleton as we always do.
0:07 So, we have a main() method, and we'll do something here and of course we'll use the live template from main() to call it down here, like so,
0:14 from PyCharm, and then, the next thing we want to do of course we are going to say define print header, what we need to do is ask the user
0:21 hey what directories subtree you want to search and what do you want to search for, so those are the next two things we are going to need,
0:27 we'll say get_folder_from_user(), and similarly we'll say get_search_text_from_user(). then let's just define a search_file() method here,
0:38 and we'll figure out the parameters in a minute, ok, we'll reformat so we are all good, via PEP 8 and let's start writing,
0:45 so print_header(), you guys know this, this is old hat by now, so we'll just fly through it, next we are going to get a folder from the user,
1:00 we'll say get_folder_form_user() and let's just do a quick little test like if they enter nothing we would rather not have our app crash
1:07 we'll just say hey search that, moreover, if they enter folder that doesn't exist, we'd also want to build the deal with this so just do a test,
1:17 and we'l leverage the truthiness of strings so we'll say if not folder, print something like sorry we can't search that location,
1:26 now notice here is a little single quote, this is one of those times where double quote strings are nice and handy
1:31 and then we'll just exit out of here. Next thing is we'll get text we'll say get the text from the user
1:37 and basically exactly the same deal here boom, I can't search for nothing, notice PyCharm has these grayed out
1:45 and that's because it knows there is no return value from that method and yet we are trying to store in a variable, it will not crush,
1:51 it will give us the implicit return none but that is going to be highly unuseful for us, that is kind of what the warning is,
1:58 now finally we want something like search file and over here we'll say we want to search the folder and the text.
2:04 Now, I don't really like this name so much, I'd rather say search files or search folders, something like that so we can hit control t and rename this
2:12 and go I want to search folders, like that, and do the refactoring, and down here and if there was 100 files all potentially leveraging this
2:21 and docstrings leveraging and so on, all of that would have been fixed, now we need our folders and text of course, excellent,
2:29 so these should be pretty easy to write, let's just come here and pull this out,
2:33 now we are going to do a little more than just get the text that is the folder, we are actually going to verify it.
2:39 So ask the user what folder do you want to search, then we'll say if not folder: so for some reason it came back empty
2:44 I don't think it can ever come back as none
2:46 but let's just verify it, let's little safe and we'll say or if possibly the folder is just white space, right,
2:54 so in either case we'll just return none so we have nothing, there is no folder and that will trigger remember up here
3:02 that will trigger this and say no, it doesn't work. Next, let's verify that this directory exists and that we have access to it.
3:10 Again, back to our OS.path.exists, now we can be little better, we can say isdir in case they put a file
3:18 and we are trying to search through subdirectories, or something like this, maybe we'd write our app to behave differently
3:24 if you give it a file versus a folder, but we are not doing that right now, so we are just going to say if it's not a directory
3:31 we'll just return none and then finally, let's clean this up a little bit and say we'll return OS.path.absolutepath()
3:38 so we have a nice absolute path for a folder instead of something relative, as you will see having an absolute path is helpful for later on.
3:46 Next, let's just get some search text here, we'll say text = some sort of input again, we'll ask what are you searching for and then we'll give
3:55 a little warning that says single phrases only, because we are not going to do like words that mean and breaking into parts
4:02 we are just going to do basic substring searches, that's an interesting app to write but it's not the focus that we are really trying to get here.
4:08 So we'll just return text. All right, it looks like things are going pretty well here,
4:14 now let's just finally do this and just do a little print statement just to show everything is hanging together,
4:18 print would search this place for that. So let's run ti really quick, let's just search the current directory,
4:27 it looks like I made a mistake, let's see here, not folder, that should be or not strip, ok, perfect, ok let's try again,
4:38 I want to search the current location, and I am searching for the word cats, so we would search/users/screencaster/desktop_08_file seracher for cats.
4:47 Perfect, so we have kind of all this parts of like user input and everything completely done, and now
4:52 it's just a matter of implementing the search method.


Talk Python's Mastodon Michael Kennedy's Mastodon