Python for .NET Developers Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Parking lot factory method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well, the parking lot is interesting and we could come up with a bunch of names like, A1, A2, A3 B1, B2, B3 but let's have the parking lot create
0:10 a pre-initialized version of itself giving more primitive data like if I have five levels and six spots per level
0:17 or something like that, so we're going to create a static function and just like with the abstract function we're going to do that with
0:23 a decorator, so we say @staticmethod def I have created some spots per level which is an integer and the number of levels which is also an integer.
0:34 This is going to return a parking lot notice it's not going to work because the way Python operates as it creates this class
0:43 is it doesn't really exist as a name until after line 19 so as we saw before we put quotes here it's kind of annoying but that's how it is.
0:54 What we need to do is create a list that probably looks like an array to you but that is not an array that is a list that you can dynamically grow
1:01 just like lists of string or list of integers and have some level names in our world we won't be able to go above, I don't know
1:09 like six or seven levels but that's all right. We would have levels A, B, C, D, E and G here and then we're just going to do a double loop.
1:29 hold off on this part we're going to adjust it in one second here. Put a pin into this, a level and then adjust the number, so like, A1, A2, A3.
1:44 Obviously we want to only as many levels as there are so if they pass in two, we want to get two if they pass in four, we want to get four.
1:53 Let me introduce you to a unique and super powerful idea here in Python so we don't really have a whole dedicated section on this
2:00 but let's just say we have these level names here like this. There's this concept in Python called slicing
2:09 so we have level names, just say L=level name so I can write less, we can say L is 0 and that gives us the first one, how if 1 give us
2:18 the second but in Python you can put a range in here. I could say 1 to 3 and that will give me the one index and then this is
2:28 a non inclusive upper bound. So 1, 2 and then that's it, so if I did 1 to 4 that would be B, C, D and I put it in the right spot. Like that, B, C, D.
2:41 So what we can do is we can actually use this idea, slicing, to get the amount that we need there assuming that it doesn't exceed
2:48 the length of our list, which is 6. What we can do is we can say L, our level names and if you want to start at 0
2:58 you can just say colon, you're also saying 0: it's implicit, and then we want to go in the first, if we want to get 4 entries
3:05 we could put 4 here, we get A, B, C, D and so on. With that in mind, what we can put right here is we can say colon levels
3:15 and as long as it doesn't exceed 6 we're going to get what we want. If it does exceed 6 it doesn't crash it just stops giving us more items.
3:25 So either we want to get that back or we'll get some subset of that. All right so this should create the level names
3:33 and then we can create a new parking lot and what this one is just the names not the data specified, so we're just going to pass the names, like that.
3:44 Let's go and try to use this. So where we do our park let's say lot = parking_lot like that. And we actually want to call the factory method.
3:56 Let's say we want to have 4 spots per level actually let's go with 5 spots per level and then we want 3 levels.
4:02 So it should be A1 through A5, B1 through B5 C1 through C5 and then just to make sure this works we'll deal with more interesting stuff in a second.
4:11 We can just print out the spots. So here we go, notice it's all wrapping along but we have A1, A2, A3, A4, B1, B2 it looks like we're off by one
4:26 and that is because we want to go there. Plus one, here we go. A1 through A5 and right now there's no cars parked in them.
4:35 Make sense? We were able to use our static method to create our staticmethod decorator to create a static method and then we used that
4:44 to do the little extra leg work to pass over just the names based on the data they gave us, we'll do that here and then the constructor actually uses
4:54 a dictionary comprehension to convert that to the data structure it needs to do its job.


Talk Python's Mastodon Michael Kennedy's Mastodon