Skip to content

Commit a566106

Browse files
committed
initial
1 parent 5ac9449 commit a566106

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

05-interlude_decorators.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from functools import wraps
2+
import logging
3+
import time
4+
5+
6+
def timeit(logger):
7+
def wrapper(fn):
8+
9+
@wraps(fn)
10+
def inner_wrapper(*args, **kwargs):
11+
t0 = time.time()
12+
result = fn(*args, **kwargs)
13+
logger(f'{fn.__name__} took {time.time() - t0:.02f} seconds')
14+
return result
15+
return inner_wrapper
16+
return wrapper
17+
18+
19+
@timeit(print)
20+
def foo(n):
21+
time.sleep(n)
22+
return n
23+
24+
25+
@timeit(logging.warning)
26+
def bar(n):
27+
time.sleep(n)
28+
return n
29+
30+
31+
def time_all(logger):
32+
def wrapper(cls):
33+
for name, obj in vars(cls).items():
34+
if not name.startswith('_') and callable(obj):
35+
setattr(cls, name, timeit(logger)(obj))
36+
return cls
37+
return wrapper
38+
39+
40+
@time_all(print)
41+
class Foo:
42+
def bar(self, n):
43+
time.sleep(n)
44+
return n
45+
46+
def baz(self, n):
47+
m = n * 2
48+
time.sleep(m)
49+
return m

06-decorators.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from math import pi
2+
3+
4+
class TypeChecker:
5+
required_type = object
6+
7+
def __init__(self, name=None):
8+
self.name = name
9+
10+
def __get__(self, instance, owner=None):
11+
return instance.__dict__[self.name]
12+
13+
def __set__(self, instance, value):
14+
assert isinstance(value, self.required_type), \
15+
f'Booooo! Expecting a {self.required_type.__name__}'
16+
instance.__dict__[self.name] = value
17+
18+
19+
class IntType(TypeChecker):
20+
required_type = int
21+
22+
23+
def type_check(**kwargs):
24+
def wrapper(cls):
25+
for var_name, checker_class in kwargs.items():
26+
setattr(cls, var_name, checker_class(var_name))
27+
return cls
28+
return wrapper
29+
30+
31+
@type_check(x=IntType, y=IntType)
32+
class Point:
33+
def __init__(self, x, y):
34+
self.x = x
35+
self.y = y
36+
37+
def move_by(self, dx, dy):
38+
self.x += dx
39+
self.y += dy
40+
41+
def __str__(self):
42+
return f'A Point at {self.x}, {self.y}'
43+
44+
def __repr__(self):
45+
return f'{self.__class__.__name__}({self.x}, {self.y})'
46+
47+
48+
class PointType(TypeChecker):
49+
required_type = Point
50+
51+
52+
@type_check(center=PointType, radius=IntType)
53+
class Circle:
54+
center = PointType('center')
55+
radius = IntType('radius')
56+
57+
def __init__(self, center, radius):
58+
self.center = center
59+
self.radius = radius
60+
61+
@property
62+
def area(self):
63+
return pi * self.radius ** 2
64+
65+
def __str__(self):
66+
return f'A Circle at {self.center.x}, {self.center.y} and ' + \
67+
f'radius {self.radius}'
68+
69+
def __repr__(self):
70+
return f'{self.__class__.__name__}({self.center!r}, {self.radius!r})'
71+
72+
73+
# Fewer lines of code!
74+
# Mind-bending?

0 commit comments

Comments
 (0)