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