Skip to content

Commit 5f9224e

Browse files
Decorator Patterns
1 parent 5dd6f21 commit 5f9224e

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import time
2+
3+
def time_it(func):
4+
def wrapper():
5+
start = time.time()
6+
result = func()
7+
end = time.time()
8+
print(f'{func.__name__} took {int((end-start)*1000)}ms')
9+
return wrapper
10+
11+
@time_it
12+
def some_op():
13+
print('Starting op')
14+
time.sleep(1)
15+
print('We are done')
16+
return 123
17+
18+
if __name__ == '__main__':
19+
# some_op()
20+
# time_it(some_op)()
21+
some_op()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from abc import ABC
2+
3+
4+
class Shape(ABC):
5+
def __str__(self):
6+
return ''
7+
8+
9+
class Circle(Shape):
10+
def __init__(self, radius=0.0):
11+
self.radius = radius
12+
13+
def resize(self, factor):
14+
self.radius *= factor
15+
16+
def __str__(self):
17+
return f'A circle of radius {self.radius}'
18+
19+
20+
class Square(Shape):
21+
def __init__(self, side):
22+
self.side = side
23+
24+
def __str__(self):
25+
return f'A square with side {self.side}'
26+
27+
28+
class ColoredShape(Shape):
29+
def __init__(self, shape, color):
30+
if isinstance(shape, ColoredShape):
31+
raise Exception('Cannot apply ColoredDecorator twice')
32+
self.shape = shape
33+
self.color = color
34+
35+
def __str__(self):
36+
return f'{self.shape} has the color {self.color}'
37+
38+
39+
class TransparentShape(Shape):
40+
def __init__(self, shape, transparency):
41+
self.shape = shape
42+
self.transparency = transparency
43+
44+
def __str__(self):
45+
return f'{self.shape} has {self.transparency * 100.0}% transparency'
46+
47+
48+
if __name__ == '__main__':
49+
circle = Circle(2)
50+
print(circle)
51+
52+
red_circle = ColoredShape(circle, "red")
53+
print(red_circle)
54+
55+
# ColoredShape doesn't have resize()
56+
# red_circle.resize(3)
57+
58+
red_half_transparent_square = TransparentShape(red_circle, 0.5)
59+
print(red_half_transparent_square)
60+
61+
# nothing prevents double application
62+
mixed = ColoredShape(ColoredShape(Circle(3), 'red'), 'blue')
63+
print(mixed)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class FileWithLogging:
2+
def __init__(self, file):
3+
self.file = file
4+
5+
def writelines(self, strings):
6+
self.file.writelines(strings)
7+
print(f'wrote {len(strings)} lines')
8+
9+
def __iter__(self):
10+
return self.file.__iter__()
11+
12+
def __next__(self):
13+
return self.file.__next__()
14+
15+
def __getattr__(self, item):
16+
return getattr(self.__dict__['file'], item)
17+
18+
def __setattr__(self, key, value):
19+
if key == 'file':
20+
self.__dict__[key] = value
21+
else:
22+
setattr(self.__dict__['file'], key)
23+
24+
def __delattr__(self, item):
25+
delattr(self.__dict__['file'], item)
26+
27+
28+
if __name__ == '__main__':
29+
file = FileWithLogging(open('hello.txt', 'w'))
30+
file.writelines(['hello', 'world'])
31+
file.write('testing')
32+
file.close()

0 commit comments

Comments
 (0)