-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathdecorator_example.py
More file actions
62 lines (49 loc) · 1.4 KB
/
Copy pathdecorator_example.py
File metadata and controls
62 lines (49 loc) · 1.4 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
def grade_decorator(f):
def wrapper(score, total):
percent = f(score, total)
grades = {
5: 'A',
4: 'A',
3: 'B',
2: 'C',
1: 'D'
}
return percent, grades[percent // 20]
return wrapper
class Student:
def __init__(self, name, score, total):
self.name = name
self.__score = score # score is made private
self.total = total
# our property methods for score
@property
def score(self):
print("Getting score...")
return self.__score
@score.setter
def score(self, new_val):
print("Setting new value...")
self.__score = new_val
@score.deleter
def score(self, new_val):
print("Deleting score...")
del self.__score
# our staticmethod get_percent. grade_decorator has been applied
@staticmethod
@grade_decorator
def get_percent(score, total):
return score / total * 100
# our classmethods, to allow different ways to create objects
@classmethod
def from_str(cls, str_arg):
name, score, total = str_arg.split(',')
return cls(name, int(score), int(total))
@classmethod
def from_tuple(cls, tup_arg):
name, score, total = tup_arg
return cls(name, score, total)
def get_record(self):
percent, grade = Student.get_percent(self.score, self.total)
return f"Name: {self.name} | Percentage scored: {percent} % | Grade: {grade}"
def __str__(self):
return ("Name: " + str(self.name) + " Score: " + str(self.__score) + " Total : " + str(self.total))