-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitem.py
More file actions
79 lines (63 loc) · 2.01 KB
/
Copy pathitem.py
File metadata and controls
79 lines (63 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import csv
class Item:
# class attribute
all = []
def __init__(self, name: str, price: float, quantity: int = 0):
# Validations
assert price >= 0, f'Price: {price} is not at least 0!'
assert quantity >= 0, f'__Quantity: {quantity} is not at least 0!'
# Instance Attribute
self.__name = name # makeing the instance private
self.__price = price
self.__quantity = quantity
# Actions to execute
Item.all.append(self) # Append all instances to a list
@property
def name(self):
return self.__name
@property
def price(self):
return self.__price
@property
def quantity(self):
return self.__quantity
@name.setter
def name(self, v):
self.__name = v
@quantity.setter
def quantity(self, q):
self.__quantity = q
def calculate_total_price(self):
return self.__quantity * self.__price
def apply_discount(self, pay_rate):
self.__price = self.__price * pay_rate # Encapsulation in action
def __connect(self): # private methods
pass
def send(self): # Simulation of Abstraction
self.__connect()
pass
# Class Method (Straight away use on the class)
@classmethod
def instantiate_from_csv(cls):
with open('item.csv', 'r') as f:
reader = csv.DictReader(f)
items = list(reader)
for item in items:
Item(
item['name'],
float(item[' price']),
float(item[' quantity'])
)
# Static Method (like a function not related with the instances)
@staticmethod
def is_integer(num):
if isinstance(num, float):
return num.is_integer()
elif isinstance(num, int):
return True
else:
return False
# Representing Objects
def __repr__(self):
# Auto fill the name
return f'{self.__class__.__name__}({self.__name}, {self.__price}, {self.__quantity})'