-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAppliance.py
More file actions
executable file
·75 lines (54 loc) · 1.96 KB
/
Appliance.py
File metadata and controls
executable file
·75 lines (54 loc) · 1.96 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
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
"""
>>> cooker = Cooker("C412", 895.50, "coal/wood")
>>> cooker.model, cooker.price, cooker.fuel
('C412', 895.5, 'coal/wood')
>>> cooker.price = 1265
>>> cooker.price
1265
>>> fridge = Fridge("F31", 426, 290)
>>> fridge.model, fridge.price, fridge.capacity
('F31', 426, 290)
>>> fridge.price = 399
>>> fridge.capacity = 275
>>> fridge.model, fridge.price, fridge.capacity
('F31', 399, 275)
"""
import abc
class Appliance(metaclass=abc.ABCMeta):
@abc.abstractmethod
def __init__(self, model, price):
self.__model = model
self.price = price
def get_price(self):
return self.__price
def set_price(self, price):
self.__price = price
price = abc.abstractproperty(get_price, set_price)
@property
def model(self):
return self.__model
class Cooker(Appliance):
def __init__(self, model, price, fuel):
super().__init__(model, price)
self.fuel = fuel
price = property(lambda self: super().price,
lambda self, price: super().set_price(price))
class Fridge(Appliance):
def __init__(self, model, price, capacity):
super().__init__(model, price)
self.capacity = capacity
price = property(lambda self: super().price,
lambda self, price: super().set_price(price))
if __name__ == "__main__":
import doctest
doctest.testmod()