From the course: Python Code Challenges for Object-Oriented Programming

Solution: The month class

- [Instructor] Okay, this is exciting. Let's take a look at our first challenge. So we've got these three test cases here, and we need to ensure that our code passes all three test cases. So the first test case is that we need to have a string representation for our month object so that it returns the month followed by a right slash and then followed by the year. Now we can also see that if you have a month which has just a single digit, you need to have a zero in front of that. Then the second and third requirement is that we need to have some sort of ordering. So we need to be able to sort the month objects. And then if I want to try and compare a month object with a date/time object, I should get a type error. So if I go ahead and select test my code, you can see that I end up with an error for my first test case. And then there seems to be problems with there being a type error where less than is not supported between the instances of month and month. And that's because we haven't implemented our solution yet. Now you might be tempted to start writing a regular class like this, and as you can see, there's going to be a lot of boilerplate code. And since you need an initializer, a string representation and a quality, you can get all of this out of the box with data classes. Now if you're new to data classes, here's a quick primer. So they're part of the Python standard library, and the reason we use them is to reduce the amount of boilerplate code to define classes, right? So this means that they automatically will generate methods like dunder_init and dunder_repr and dunder_equals and others. So you can import data classes from the data class package, and then you go ahead and decorate the class. So I've got my month class here, and I decorate it by using data class over at the top. So let me just go ahead and get rid of this code here. And then the next thing you'll want to do is to specify the type of each field, and then that's basically it. Now what we need to do over here is to create our string representation for the month class. So let's go ahead and do that. And so we'll want to define our dunder_str method. So we define dunder_str, and then I want to go ahead and return, and I'm going to use an S string here. I want to return the month first, so that's self.month. And then I need to remember that I should have formatting so that if it's a single digit month, I need to return a zero in front. So I'm going to do that with a colon and a zero two. I've got my forward slash and then self.year. So let's go ahead and test our code. And you can see that we've passed the first test, which is this one. And now we need to go ahead and implement some means of working with comparing the date/time and the month and the different month objects. And this is really easy to do in data classes because all we need to do is to pass order as one of the parameters to our decorator. And so I just say order equals true and select test my code. And you can see that it's now passed all three tests. This is just one way to go about solving this problem. If you have another solution that passes all three tests, then that's absolutely fine.

Contents