Apr-29-2022, 03:36 PM
I want to do a simple calendar to learn practical use of classes, because I find classes counterintuitive in python and I struggle a lot with it.
my code rn looks like this:
PS.it is not even working
my code rn looks like this:
from dataclasses import dataclass from rich import print class Date(object): def __init__(self, year:int, month:int, day:int): self.year = self.Year(year) self.month = self.Month(month) self.day = self.Day(day) class Month: def __init__(self, name:str): assert name.capitalize() in self.Months.names, f'''[ERROR] : No such month {name} Possible names are : {self.Months.names} ''' self.id = self.Months.names.index(name) self.days = self.Months.days[id] self.name = self.Months.names[id] def __repr__(self): return self.id, self.name, self.days class Year: def __init__(self, year:int): self.year = year self.leap = not year % 4 def __repr__(self): return self.year, self.leap @dataclass class Day: id : int @dataclass class Months: names = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] def __repr__(self): return f'{self.day}:{self.year}:{self.month}'I need someone to review this, and give me some examples on what here sucks and how it should be done properly, I think this multiple nesting is one of the issues here, but I wanted to be a single-class calendar.
PS.it is not even working
In [87]: Date(1994,'July', 1) --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [87], in <cell line: 1>() ----> 1 Date(1994,'July', 1) Input In [86], in Date.__init__(self, year, month, day) 2 def __init__(self, year:int, month:int, day:int): 3 self.year = self.Year(year) ----> 4 self.month = self.Month(month) 5 self.day = self.Day(day) Input In [86], in Date.Month.__init__(self, name) 8 def __init__(self, name:str): ----> 9 assert name.capitalize() in self.Months.names, f'''[ERROR] : No such month {name} 10 Possible names are : {self.Months.names} 11 ''' 12 self.id = self.Months.names.index(name) 13 self.days = self.Months.days[id] AttributeError: 'Month' object has no attribute 'Months' In [88]: Date(1994,'July', 1)