Python for the .NET Developer Transcripts
Chapter: OOP: Object-Oriented Python
Lecture: Parking lots
0:00 Here we in our program application. Let's break this up a little bit. So this part is for creating cars and driving them.
0:09 We also want to park the cars when we're done. I want to do that a little bit separately. So let's go over here and create a method.
0:16 It's called drive_cars, or let's call it use_cars. Then we're going to have another one over here called park_cars. And we'll pass cars in like so;
0:27 and we'll just go ahead and write it here, I guess. And let's be really clear, so it helps us out a lot. We're going to take a list of cars.
0:36 Then we'll make sure everything's hanging together. Yes, it looks like it is. So, the idea is we're going to create another class
0:42 that has some other special behaviors called ParkingLot. It's not going to participate in the object hierarchy of what we had before
0:54 but it's going to do a couple of interesting things. First of all, when I create a ParkingLot it's going to have different levels.
1:00 Think of a parking garage; there's level A, level B, level C. Sometimes they get created like the orange level and the green level or, you know
1:07 the carrot level, whatever. We're going to go basic; we're going to have level A, B, and C;
1:11 and we're going to have a spot 1, 2, 3, 4, whatever in those. We're going to take a constructor.
1:16 We're going to create a __init__, and it's going to take a string, a list of strings called spot names. And be a list of str, like so.
1:28 And we can use PyCharm's magic to just add this as a field but we don't actually want to. Because there's just not enough information for us to store.
1:35 We need to know what are the spot names and is some car parked in there. And if it is, what car is actually parked in it?
1:42 So what I'm going to do is create a dictionary called spots, and we can say this is a dictionary like this. You technically can also write it like this
1:51 but let's be explicit for a second. for n in spot_names. We're going to to set, create an entry in the dictionary
2:06 and we're going to currently set it to be None. Okay, this is what we need, we, when later on we're going to put a car into into that spot.
2:13 So we'll be able to say the free spots are the ones where it's None; and the ones where it's not None
2:17 that is, we could pull the cars out and work with them. And this is totally fine. We could, we could roll with this
2:22 but I want to show you something else we can do in Python that's cool. We talked about the list comprehension, remember? 2 n, something like this.
2:35 And it even had an if statement here. This is a little bit like LINQ. This generates a list. We can actually generate a dictionary in a similar way.
2:42 So we can say self.spots and we could put a type here if we wanted. We could say this is a dict of str, Car.
2:51 Like that, I think this is what we need to say. We can say this is equal to not square braces but curly braces and put a little expression here.
2:59 So then it, the way it goes you say, key:value. So let's say n:None and then for n in spot_names, like this, all right. So what that's going to do
3:12 it's going to create a, the same thing here. So, like, we create the dictionary, then we loop over and we
3:16 put an entry for the name and we're putting None in there. And we have self.spot_names, spots of whatever. Notice Car, Car, Car, Car, Car, Car.
3:29 Because we told it, it's not just a arbitrary dictionary but key is a string and the value is a Car. Technically, this should be an optional of car.
3:40 We're going to be as accurate as possible but really we'd be able to get away with it, right. Obviously this is, something we want to indicate.
3:48 So here is our parking lot, and we're going to be able to store cars into it.