forked from bedroombuilds/python2rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigher_order.py
More file actions
53 lines (39 loc) · 1.38 KB
/
higher_order.py
File metadata and controls
53 lines (39 loc) · 1.38 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
def HasColor(name, default=None, doc=None):
propname = "__color_{}".format(name)
colors = {
"black": 0x00000000,
"white": 0x00ffffff,
"red": 0x00ff0000,
"green": 0x0000ff00,
"blue": 0x000000ff
}
def getter(self):
return getattr(self, propname, default)
def setter(self, value):
if isinstance(value, int):
if isinstance(value, bool):
raise TypeError("integer colors must be ints")
if value < 0 or value > 0x00ffffff:
raise ValueError("integer colors must be 24-bit")
se
4C07
tattr(self, propname, value)
elif isinstance(value, str):
if value not in colors:
raise ValueError("unknown color '{}'".format(value))
setattr(self, propname, colors[value])
else:
raise TypeError("color specifications must be ints or strs")
class Inner:
pass
setattr(Inner, name, property(getter, setter, None, doc))
return Inner
class Canvas(HasColor("foreground"), HasColor("background")):
pass
class Button(Canvas, HasColor("border"), HasColor("text")):
pass
if __name__ == "__main__":
c = Canvas()
c.foreground = 'red'
b = Button()
b.foreground = 'black'
b.text = 'green'
print(f"canvas fg: {c.foreground:06x}, button txt: {b.text:06X}, button fg: {b.foreground}")