MCPcopy Create free account
hub / github.com/Mrinank-Bhowmick/python-beginner-projects / Item

Class Item

projects/Expense-Tracker/item.py:46–216  ·  view source on GitHub ↗

Represents an expense or income item in the expense tracker app. This class provides methods to create, manipulate, and serialize/deserialize expense or income items.

Source from the content-addressed store, hash-verified

44
45@dataclass
46class Item:
47 """
48 Represents an expense or income item in the expense tracker app.
49
50 This class provides methods to create, manipulate, and serialize/deserialize expense or income items.
51 """
52
53 item_id: str
54 name: str
55 amount: float
56 description: str
57 date: datetime
58 category: Optional[Category] = None
59
60 def __str__(self):
61 return self.to_json_str(indent=4)
62
63 def get_category_str(self) -> str:
64 """
65 Returns the category of the item as a string.
66
67 Returns:
68 str: The category of the item or "Uncategorized" if category is None.
69 """
70 return "Uncategorized" if self.category is None else str(self.category)
71
72 @classmethod
73 def create(
74 cls,
75 name: str,
76 amount: float,
77 description: str,
78 date_str: str,
79 category: Optional[Category] = None,
80 ):
81 """
82 Factory method to create an expense or income Item instance.
83
84 Args:
85 name (str): The name for the item.
86 amount (float): The amount associated with the item. This could be +ve or -ve.
87 description (str): Additional details about the item.
88 date_str (str): The date and time when the item occurred, in "YYYY-MM-DD" format.
89 category (Optional[Category]): The category of the item. Defaults to None.
90
91 Returns:
92 Item: An instance of the Item class representing the created item.
93 """
94 item_id = str(uuid.uuid4()) # Generate a unique ID
95 # item_id = str(name + '-' + date_str) # Generate a unique ID
96 date = datetime.strptime(
97 date_str, "%Y-%m-%d"
98 ) # Parse the date string into a datetime object
99 return cls(
100 item_id=item_id,
101 name=name,
102 amount=amount,
103 description=description,

Callers 1

_create_itemMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected