-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection7.py
More file actions
266 lines (209 loc) · 8.16 KB
/
section7.py
File metadata and controls
266 lines (209 loc) · 8.16 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
# #############################################################################
# ### Imports
import operator
# #############################################################################
# ### Resource Class
class Resource:
def __init__(self, name, manufacturer, total, allocated):
self._name = str(name).strip()
self._manufacturer = str(manufacturer).strip()
if self._validate_data(total, raise_error=True):
self._total = total
if self._validate_data(allocated, raise_error=True):
self._allocated = allocated
@property
def name(self):
return self._name
@property
def manufacturer(self):
return self._manufacturer
@property
def total(self):
return self._total
@property
def allocated(self):
return self._allocated
def __str__(self):
return f'{self.__class__.__name__}({self.name})'
def __repr__(self):
return f'{self.__class__.__name__}(name={self.name}, manufacturer={self.manufacturer}, total={self.total}, allocated={self.allocated})'
def _validate_data(self, n, to_compare=None, *, raise_error=False):
try:
if not isinstance(n, int):
raise TypeError('value must be of type integer.')
if n < 0:
raise ValueError('n must be positive')
if to_compare and n > to_compare:
raise ValueError(f'value must be less than or equal to {to_compare}')
except Exception as ex:
print(ex)
if raise_error:
raise
return False
else:
return True
def _aggregate_data(self, n, to_update, op):
return op(to_update, n)
def claim(self, n):
if self._validate_data(n, self._total):
self._total = self._aggregate_data(n, self._total, operator.isub)
self._allocated = self._aggregate_data(n, self._allocated, operator.iadd)
def freeup(self, n):
if self._validate_data(n, self._allocated):
self._total = self._aggregate_data(n, self._total, operator.iadd)
self._allocated = self._aggregate_data(n, self._allocated, operator.isub)
def died(self, n):
if self._validate_data(n, self.total):
self._total = self._aggregate_data(n, self._total, operator.isub)
def purchased(self, n):
if self._validate_data(n):
self._total = self._aggregate_data(n, self._total, operator.iadd)
@property
def category(self):
return self.__class__.__name__.lower()
r1 = Resource('Intel Core i9-9900k', 'Intel', 50, 20)
# print(f'Start: total: {r1.total}, allocated: {r1.allocated}')
# r1.freeup(19)
# print(f'freeup(19): total: {r1.total}, allocated: {r1.allocated}')
# r1.died(30)
# print(f'died(30): total: {r1.total}, allocated: {r1.allocated}')
# r1.claim(19)
# print(f'claim(19): total: {r1.total}, allocated: {r1.allocated}')
# r1.purchased(30)
# print(f'purchased(30): total: {r1.total}, allocated: {r1.allocated}')
# r1.claim(100)
# print(f'claim(100): total: {r1.total}, allocated: {r1.allocated}')
# r1.freeup(100)
# print(f'freeup(100): total: {r1.total}, allocated: {r1.allocated}')
# r1.died(100)
# print(f'died(100): total: {r1.total}, allocated: {r1.allocated}')
# print(r1.__str__())
# print(r1.__repr__())
# #############################################################################
# ### CPU Class
class CPU(Resource):
def __init__(self, name, manufacturer, total, allocated, cores, socket, power_watts):
super().__init__(name, manufacturer, total, allocated)
if self._validate_data(cores, raise_error=True):
self._cores = cores
self._socket = str(socket).strip()
if self._validate_data(power_watts, raise_error=True):
self._power_watts = power_watts
@property
def cores(self):
return self._cores
@property
def socket(self):
return self._socket
@property
def power_watts(self):
return self._power_watts
cpu = CPU('Intel Core i9-9900k', 'Intel', 50, 20, 8, 'AM4', 94)
print(cpu.__repr__())
print(f'Start: total: {cpu.total}, allocated: {cpu.allocated}')
cpu.freeup(19)
print(f'freeup(19): total: {cpu.total}, allocated: {cpu.allocated}')
cpu.died(30)
print(f'died(30): total: {cpu.total}, allocated: {cpu.allocated}')
cpu.claim(19)
print(f'claim(19): total: {cpu.total}, allocated: {cpu.allocated}')
cpu.purchased(30)
print(f'purchased(30): total: {cpu.total}, allocated: {cpu.allocated}')
cpu.claim(100)
print(f'claim(100): total: {cpu.total}, allocated: {cpu.allocated}')
cpu.freeup(100)
print(f'freeup(100): total: {cpu.total}, allocated: {cpu.allocated}')
cpu.died(100)
print(f'died(100): total: {cpu.total}, allocated: {cpu.allocated}')
print(cpu.__str__())
print(cpu.__repr__())
# #############################################################################
# ### Storage Class
class Storage(Resource):
def __init__(self, name, manufacturer, total, allocated, capacity_GB):
super().__init__(name, manufacturer, total, allocated)
if self._validate_data(capacity_GB, raise_error=True):
self._capacity_GB = capacity_GB
@property
def capacity_GB(self):
return self._capacity_GB
storage = Storage('Fak0_A320GB_7200', 'Western Digital', 50, 20, 320)
print(storage.__repr__())
print(f'Start: total: {storage.total}, allocated: {storage.allocated}')
storage.freeup(19)
print(f'freeup(19): total: {storage.total}, allocated: {storage.allocated}')
storage.died(30)
print(f'died(30): total: {storage.total}, allocated: {storage.allocated}')
storage.claim(19)
print(f'claim(19): total: {storage.total}, allocated: {storage.allocated}')
storage.purchased(30)
print(f'purchased(30): total: {storage.total}, allocated: {storage.allocated}')
storage.claim(100)
print(f'claim(100): total: {storage.total}, allocated: {storage.allocated}')
storage.freeup(100)
print(f'freeup(100): total: {storage.total}, allocated: {storage.allocated}')
storage.died(100)
print(f'died(100): total: {storage.total}, allocated: {storage.allocated}')
print(storage.__str__())
print(storage.__repr__())
# ### HDD Class
class HDD(Storage):
def __init__(self, name, manufacturer, total, allocated, capacity_GB, size, rpm):
super().__init__(name, manufacturer, total, allocated, capacity_GB)
self._size = str(size).strip()
if self._validate_data(rpm, raise_error=True):
if not (rpm > 2000 and rpm < 10300):
raise ValueError('no such HDD available')
self._rpm = rpm
@property
def size(self):
return self._size
@property
def rpm(self):
return self._rpm
hdd = HDD('Fak0_A320GB_7200', 'Western Digital', 50, 20, 320, size=2.5, rpm=7200)
print(hdd.__repr__())
print(f'Start: total: {hdd.total}, allocated: {hdd.allocated}')
hdd.freeup(19)
print(f'freeup(19): total: {hdd.total}, allocated: {hdd.allocated}')
hdd.died(30)
print(f'died(30): total: {hdd.total}, allocated: {hdd.allocated}')
hdd.claim(19)
print(f'claim(19): total: {hdd.total}, allocated: {hdd.allocated}')
hdd.purchased(30)
print(f'purchased(30): total: {hdd.total}, allocated: {hdd.allocated}')
hdd.claim(100)
print(f'claim(100): total: {hdd.total}, allocated: {hdd.allocated}')
hdd.freeup(100)
print(f'freeup(100): total: {hdd.total}, allocated: {hdd.allocated}')
hdd.died(100)
print(f'died(100): total: {hdd.total}, allocated: {hdd.allocated}')
print(hdd.__str__())
print(hdd.__repr__())
# ### SSD Class
class SSD(Storage):
def __init__(self, name, manufacturer, total, allocated, capacity_GB, interface):
super().__init__(name, manufacturer, total, allocated, capacity_GB)
self._interface = str(interface).strip()
@property
def interface(self):
return self._interface
ssd = SSD('Evo 830i 240GB', 'Samsung', 100, 250, 240, interface='PCIe NVMe 3.0 x4')
print(ssd.__repr__())
print(f'Start: total: {ssd.total}, allocated: {ssd.allocated}')
ssd.freeup(19)
print(f'freeup(19): total: {ssd.total}, allocated: {ssd.allocated}')
ssd.died(30)
print(f'died(30): total: {ssd.total}, allocated: {ssd.allocated}')
ssd.claim(19)
print(f'claim(19): total: {ssd.total}, allocated: {ssd.allocated}')
ssd.purchased(30)
print(f'purchased(30): total: {ssd.total}, allocated: {ssd.allocated}')
ssd.claim(100)
print(f'claim(100): total: {ssd.total}, allocated: {ssd.allocated}')
ssd.freeup(100)
print(f'freeup(100): total: {ssd.total}, allocated: {ssd.allocated}')
ssd.died(100)
print(f'died(100): total: {ssd.total}, allocated: {ssd.allocated}')
print(ssd.__str__())
print(ssd.__repr__())