Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 8: File Searcher App
Lecture: Improved search results
0:00 So it's time to improve the results that come out of here, remember, we are just passing the actual lines of a bunch of files,
0:07 we don't really even know which ones those are, we just start with the folder that have some kind of match,
0:12 so we want to bundle some information together about these matches, now we could use classes and those are very powerful,
0:18 but really we just need things like what's the line number, what's the text, what's the file name and so on.
0:24 So it turns out named tuples are perfect, so again, we'll just use our collections of a SearchResult = we want to use our collections.namedtuple()
0:35 and then the first thing is the type name the second argument are basically the fields so it will be file, file line and text,
0:46 let's say those are the three things that we are going to return there. Now let's change this, so not just append a line but actually make results,
0:55 so we'll say m for match it's going to be a SearchResults() and in here what are we going to add,
1:02 we want to say line = line I think I called it and file = filename and the text = ... actually, text = line, that's the line of text
1:15 and I need the line number here, so we have to compute this ourselves so way we are doing it but that's ok,
1:25 ok, so now we are going to append that and let's just run it again, remember, these named tuples have decent output, ok,
1:32 we are going to search our books again, and we are going to search for the word friends,
1:37 and here are a bunch of matches that have way more information, so you can see we have the file is the Ulysses book
1:44 and these are the lines and we also have Tales of Two Cities and The Doll House, they all talk about friends.
1:50 Ok, that works, but let's do a little bit nicer output so we can read it here,
1:53 let's go at the top where we are doing this print and for a little bit let's go ok, stop this, what we are going to do is we are going to say print()
2:01 and we'll do something like this, there is a match ok so we'll say match then we'll print out the file, the line and the actual match text,
2:10 let's run it again. Same folder, this time let's search for happiness. All right, so we have some matches,
2:19 it turns out in Ulysses on line 20899 rule of happiness of the better land- yes, this is a much nicer output.
2:28 Now, something is funny like there is two lines here, and yet we are only doing like one print on this which is kind of unusual what is going on,
2:38 remember, when we read an individual line from the files they actually have the new line on there so that new line is appearing here
2:45 and I'd rather take more control over the text rather than assume that that's always going to be there so let's just say strip(),
2:51 now it could do an rstrip() just to get the stuff off the end or maybe
2:55 if there is white space you want to pull it to the font, anyway so let's do it this way. Great, it looks like our searching is working perfectly.
3:08 Now remember, we are giving it this folder and it turns out we are searching these files
3:12 but we are not searching Dracula or Adventure of Sherlock Holmes, or anything like that, let's just prove that,
3:20 we come over here and say I want to search for Holmes, well, Ulysses apparently has some place that talks about Sherlock Holmes
3:29 but we are not getting into that subdirectory. And this is to be expected because we said if it's a directory ignore it,
3:36 there is a variety of ways in which we could solve this problem but it turns out the most natural way to solve these hierarchical problems are
3:44 to use something called recursion, so let's take a moment and go look at this concept and then we'll come back and apply ti to our application.