-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-property-decorators-product.py
More file actions
407 lines (292 loc) · 11.3 KB
/
04-property-decorators-product.py
File metadata and controls
407 lines (292 loc) · 11.3 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""Question: Define a class Product with private attributes _name and _price.
Use properties to get and set these attributes with validation.
Add a method to apply a discount to the price.
"""
# LEARNING CHALLENGE
#
# Before looking at any solution below, please try to solve this yourself first!
#
# Tips for success:
# - Read the question carefully
# - Think about what classes and methods you need
# - Start with a simple implementation
# - Test your code step by step
# - Don't worry if it's not perfect - learning is a process!
#
# Remember: The best way to learn programming is by doing, not by reading solutions!
#
# Take your time, experiment, and enjoy the learning process!
# Try to implement your solution here:
# (Write your code below this line)
# HINT SECTION (Only look if you're really stuck!)
#
# Think about:
# - What are private attributes? (attributes starting with _)
# - How do you create properties? (@property and @attribute.setter decorators)
# - What validation should price have? (positive number)
# - How do you calculate discount? (percentage of original price)
#
# Remember: Start simple and build up complexity gradually!
# ===============================================================================
# STEP-BY-STEP SOLUTION
# ===============================================================================
#
# CLASSROOM-STYLE WALKTHROUGH
#
# Let's solve this problem step by step, just like in a programming class!
# Each step builds upon the previous one, so you can follow along and understand
# the complete thought process.
#
# ===============================================================================
# Step 1: Define the Product class with private attributes
# ===============================================================================
# Explanation:
# Let's start by creating our Product class with private attributes.
# Private attributes start with _ and are meant for internal use.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
# What we accomplished in this step:
# - Created Product class with private attributes _name and _price
# Step 2: Add property getter for name
# ===============================================================================
# Explanation:
# Properties allow us to access private attributes through methods that look like attributes.
# The @property decorator creates a getter method.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
# What we accomplished in this step:
# - Added property getter for name attribute
# Step 3: Add property setter for name with validation
# ===============================================================================
# Explanation:
# The @name.setter decorator creates a setter method that includes validation.
# We'll check that the name is not empty.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
# What we accomplished in this step:
# - Added property setter for name with validation
# Step 4: Add property getter for price
# ===============================================================================
# Explanation:
# Now let's add the property getter for price, similar to what we did for name.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def price(self):
return self._price
# What we accomplished in this step:
# - Added property getter for price attribute
# Step 5: Add property setter for price with validation
# ===============================================================================
# Explanation:
# The price setter should validate that the price is a positive number.
# We'll accept both integers and floats.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if not isinstance(value, (int, float)) or value <= 0:
raise ValueError("Price must be a positive number")
self._price = value
# What we accomplished in this step:
# - Added property setter for price with validation
# Step 6: Add discount method
# ===============================================================================
# Explanation:
# The apply_discount method reduces the price by a given percentage.
# We'll validate that the discount is between 0 and 100 percent.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if not isinstance(value, (int, float)) or value <= 0:
raise ValueError("Price must be a positive number")
self._price = value
def apply_discount(self, discount_percentage):
if not isinstance(discount_percentage, (int, float)):
raise ValueError("Discount percentage must be a number")
if not (0 < discount_percentage < 100):
raise ValueError("Discount percentage must be between 0 and 100")
discount_amount = self._price * (discount_percentage / 100)
self._price -= discount_amount
return discount_amount
# What we accomplished in this step:
# - Added apply_discount method with validation
# - Returns the discount amount for transparency
# Step 7: Add string representation and additional methods
# ===============================================================================
# Explanation:
# Let's add a __str__ method and some additional useful methods to make our Product class more complete.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if not isinstance(value, (int, float)) or value <= 0:
raise ValueError("Price must be a positive number")
self._price = value
def apply_discount(self, discount_percentage):
if not isinstance(discount_percentage, (int, float)):
raise ValueError("Discount percentage must be a number")
if not (0 < discount_percentage < 100):
raise ValueError("Discount percentage must be between 0 and 100")
original_price = self._price
discount_amount = self._price * (discount_percentage / 100)
self._price -= discount_amount
print(f"Applied {discount_percentage}% discount: ${discount_amount:.2f} off")
return discount_amount
def __str__(self):
return f"Product(name='{self._name}', price=${self._price:.2f})"
# What we accomplished in this step:
# - Added __str__ method for readable product representation
# - Enhanced apply_discount with user feedback
# Step 8: Create instances and test our product
# ===============================================================================
# Explanation:
# Finally, let's create instances of our Product class and test all the properties
# and methods to make sure everything works correctly.
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def name(self):
return self._name
@name.setter
def name(self, value):
if not value or not value.strip():
raise ValueError("Name cannot be empty")
self._name = value
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if not isinstance(value, (int, float)) or value <= 0:
raise ValueError("Price must be a positive number")
self._price = value
def apply_discount(self, discount_percentage):
if not isinstance(discount_percentage, (int, float)):
raise ValueError("Discount percentage must be a number")
if not (0 < discount_percentage < 100):
raise ValueError("Discount percentage must be between 0 and 100")
original_price = self._price
discount_amount = self._price * (discount_percentage / 100)
self._price -= discount_amount
print(f"Applied {discount_percentage}% discount: ${discount_amount:.2f} off")
return discount_amount
def __str__(self):
return f"Product(name='{self._name}', price=${self._price:.2f})"
# Test our class:
product = Product("Laptop", 1000)
print(f"Initial product: {product}")
# Test property getters
print(f"Product name: {product.name}")
print(f"Product price: ${product.price}")
# Test property setters
product.name = "Gaming Laptop"
product.price = 1200
print(f"Updated product: {product}")
# Test discount method
product.apply_discount(10)
print(f"After 10% discount: {product}")
product.apply_discount(15)
print(f"After additional 15% discount: {product}")
# Test validation (these would raise errors if uncommented)
# try:
# product.name = "" # Should raise ValueError
# except ValueError as e:
# print(f"Name validation error: {e}")
# try:
# product.price = -100 # Should raise ValueError
# except ValueError as e:
# print(f"Price validation error: {e}")
print("All product operations completed successfully!")
# What we accomplished in this step:
# - Created and tested our complete Product implementation
# - Demonstrated properties, validation, and discount functionality
# ===============================================================================
# CONGRATULATIONS!
#
# You've successfully completed the step-by-step solution!
#
# Key concepts learned:
# - Private attributes and encapsulation
# - Property decorators for getters and setters
# - Data validation in property setters
# - Business logic implementation (discount calculation)
# - Error handling and user feedback
#
# Try it yourself:
# 1. Start with Step 1 and code along
# 2. Test each step before moving to the next
# 3. Understand WHY each step is necessary
# 4. Experiment with modifications (try adding tax calculation or bulk discounts!)
#
# Remember: The best way to learn is by doing!
# ===============================================================================