-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path04-decorators.py
More file actions
72 lines (49 loc) · 1.54 KB
/
04-decorators.py
File metadata and controls
72 lines (49 loc) · 1.54 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
from math import pi
class TypeChecker:
required_type = object
def __init__(self, name=None):
self.name = f'_{name}'
def __get__(self, instance, owner=None):
return instance.__dict__[self.name]
def __set__(self, instance, value):
assert isinstance(value, self.required_type), \
f'Booooo! Expecting a {self.required_type.__name__}'
instance.__dict__[self.name] = value
class IntType(TypeChecker):
required_type = int
def type_check(cls):
for var_name, checker in cls.__dict__.items():
if isinstance(checker, TypeChecker):
checker.name = f'_{var_name}'
return cls
@type_check
class Point:
x = IntType()
y = IntType()
def __init__(self, x, y):
self.x = x
self.y = y
def move_by(self, dx, dy):
self.x += dx
self.y += dy
def __str__(self):
return f'A Point at {self.x}, {self.y}'
def __repr__(self):
return f'{self.__class__.__name__}({self.x}, {self.y})'
class PointType(TypeChecker):
required_type = Point
@type_check
class Circle:
center = PointType()
radius = IntType()
def __init__(self, center, radius):
self.center = center
self.radius = radius
@property
def area(self):
return pi * self.radius ** 2
def __str__(self):
return f'A Circle at {self.center.x}, {self.center.y} and ' + \
f'radius {self.radius}'
def __repr__(self):
return f'{self.__class__.__name__}({self.center!r}, {self.radius!r})'