-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty_decorator.py
148 lines (118 loc) · 3.43 KB
/
property_decorator.py
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
'''
Use property to protect some attributes
Followed by a full example with a bank account
'''
class Game:
def __init__(self, level=None):
self.level = level if level else 0
@property
def level(self):
return self._level
@level.setter # property must be defined over level to set it
def level(self, value):
if not isinstance(value, int):
raise TypeError('The value of level must be of type int.')
if value < 0:
self._level = 0
elif value > 100:
self._level = 100
else:
self._level = value
game = Game(200)
print(game.level)
game.level = 500
print(game.level)
### COMPLETE EXAMPLE WITH A BANK ACCOUNT
#*
class AccountException(Exception):
def __init__(self, msg):
super().__init__(f"Account Exception : {msg}")
class BankAccount:
def __init__(self, number, balance):
self.__number = number
self.__balance = balance
@property
def number(self):
return self.__number
@number.setter
def number(self, number):
raise AccountException("You can not change the account number")
@number.deleter
def number(self):
if self.__balance > 0:
raise AccountException("You only can delete an empty account")
else:
del self.__number
@property
def balance(self):
return self.__balance
@balance.setter
def balance(self, balance):
if balance < 0:
raise AccountException("You can not set a negative balance")
else:
self.__balance = balance
@balance.deleter
def balance(self):
self.__balance = 0
def __str__(self):
if hasattr(self,'number'):
return f"bank account number : {self.__number} / Balance : {self.__balance:_}"
else:
return f"bank account number : None"
def deposit(self, value):
if value > 100_000:
print("Warning : deposit > 100.000")
self.__balance += value
def withdraw(self, value):
if value > 100_000:
print("Warning : withdraw > 100.000")
if self.__balance > value:
self.__balance -= value
else:
print(f"Error : you only can withdraw {self.__balance:_}")
print("\nCreation of an account number with 1000 on balance")
b_account = BankAccount("123456", 1000)
print(b_account)
print("\nTry to set the balance to -200")
try:
b_account.balance = -200
except AccountException as e:
print(e)
print(b_account)
print("\ntrying to set a new value for the account number")
try:
b_account.number = "456789"
except AccountException as e:
print(e)
print(b_account)
print("\ntrying to deposit 1.000.000")
try:
b_account.deposit(1_000_000)
except AccountException as e:
print(e)
print(b_account)
print("\ntrying to withdraw 1.000.000")
try:
b_account.withdraw(1_000_000)
except AccountException as e:
print(e)
print(b_account)
print("\ntrying to delete the account attribute containing a non-zero balance")
try:
del b_account.number
except AccountException as e:
print(e)
print(b_account)
print("\ntrying to delete the balance")
try:
del b_account.balance
except AccountException as e:
print(e)
print(b_account)
print("\ntrying to delete the account attribute containing a non-zero balance")
try:
del b_account.number
except AccountException as e:
print(e)
print(b_account)