Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Parsing CSV data into Classes

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Ok, we saw that the csv dict reader was really the way to go from the text file into memory for comma separated data.
0:10 Now, there are still some things that are not quite perfect, like imagine I was given this data, I would somehow want to go through it,
0:19 if you asked me what is the average number of beds, well, I still have to know every single time to say quote beds,
0:25 and if I say bed instead of beds, crash, that's not great. It's not discoverable what all the rows are,
0:34 and the rows all come back as text even though beds represent a number,
0:39 it's just a string that came out of the text file so if I look at the type here, I'm going to say type of that, it should come back as str,
0:52 guess what, type is str because it's a string, we need to convert it to its either an integer or for things like longitude, latitude to floats,
1:02 so let's bundle up that sort of implicit knowledge of the format here, and we can put that into a separate class,
1:12 so I'll create a file and I'll call this just data types, probably a better descriptive name would be great there
1:19 but let's define a class and we'll call this, let's just call it a purchase, right, I was going to call it real estate purchase
1:25 but within the context of our real estate app hopefully the real estate component is clear.
1:29 Now, I am going to show you something that you've not seen in this class about classes, what we have done so far is we have defined a __init__()
1:37 and we are going to do that again here, let me just really quickly sketch out the pieces that are going to go in here,
1:42 you don't need to see me type this, ok, so now I've sketched out a fairly complicated initialization method but you know,
1:53 it's just a matter of what pieces of data you want to store in here and where do we go. Now, we could go back to program and make it this method,
2:01 this load files job to allocate a bunch of those purchase objects do the conversion type here and stash the data in here
2:10 by passing them to the initializer. But, this class already knows the structure and it knows the pieces that it expects,
2:17 so let's make this class responsible for creating other ones, ok. So we can come over here and define the method,
2:25 let's just call this create_from_dict(), something like this, and we'll call this a lookup, or something like that, ok,
2:35 we will come down here we will say something this, return purchase and we are going to allocate one of these things,
2:41 the first thing that goes in here is going to be the street so we would say lookup of street like so,
2:47 again just let me type this out and I'll skip ahead, ok, so I wrote this method, now there is a few more things to do here,
3:05 we want to convert the number of beds into an integer, so we need to parse this, similarly we want to convert the number of baths the square footage,
3:12 I believe that is an integer and a price, let's make that a float, just to be safe. I suppose we could do that here as well.
3:22 Ok, so this will create these purchase objects and from then on, I can say things like . (dot) and here is all my list of items,
3:29 and the price is always going to be a float, and that will be a strong structure that we can work with in our app.
3:34 But, let's try to use this method this is not going to be so cool, so come over here to our program, let's go ahead and make a purchases list here,
3:46 so we are just going to store all the purchases basically one for each row, let's say purchases.append() purchase,
3:53 let's just say p, keep it short to fit on the screen, so we'll say p=purchase, we need to import this, if I try this, it's- let's give it the row here,
4:05 row is a dictionary, if we try to run this, it's not going to go so well, it says missing one position lookup,
4:11 the other thing that it wanted here is self, we have to order these this way, I have to do this weird thing,
4:18 I have to say p=purchase and I got it created and then I have to say p. well, that almost works, notice this line little error went away,
4:26 the line above now says oh, you are missing these 12 parameters, yayks. So, let's go back through this.
4:33 This method has nothing to do with the individual objects, it deals with the class, right, this just is associated with this class
4:42 but it doesn't have anything to do with the self parameter, it could be a static method, it could be one that we can just call right on the type
4:49 and it maybe will allocate instances, of the purchase. Now PyCharm is actually telling us to head as well like see the little squiggles there
4:55 it says you know what, you can make this method static and the way we do that is we add what is called a decorator,
5:01 a decorator starts with the @ symbol, and they have a cool color in PyCharm, so we can say @staticmethod there is other things you can say,
5:08 you can say @property, and define the method that would be a property instead of a method and so on, what we are going to use is static method
5:17 and that lets us go back here and instead of requiring an instance we say like this, purchase.create_from_lookup and it only takes the dictionary here,
5:29 so let's go and append these, and let's top the print out here, I'll just delete those,
5:34 when we are done, I'll just print let's just print the first entry, our first class, all right, run this- oops,
5:42 I mean purchases, so we go this first one now that's not super helpful,
5:47 let's use this internal dictionary just to see what the data is like really quick,
5:52 here we go, sale date, such and such, state number of baths, perfect,
5:59 and notice, the type, residential has a quote but the number of baths is an actual number, it's the 1, not quote '1',
6:05 which means we can do math on it similarly for things like the longitude, latitude, number of bedrooms and so on.
6:13 So this is great, now we are just going to return our purchases,
6:16 what will be in this list, this will be our specifically typed purchase class that has to do, that knows specifically about the data containing here
6:24 and we can leverage that to help us do math like ask what is the average number of bathrooms and so on.


Talk Python's Mastodon Michael Kennedy's Mastodon