Skip to content

Commit 887582c

Browse files
authored
Merge pull request faif#255 from gyermolenko/refactor_template_pattern
Refactor Template pattern
2 parents 2e8ae94 + ac2278e commit 887582c

1 file changed

Lines changed: 42 additions & 93 deletions

File tree

behavioral/template.py

Lines changed: 42 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -2,119 +2,68 @@
22
# -*- coding: utf-8 -*-
33

44
"""
5-
http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
6-
75
An example of the Template pattern in Python
86
97
*TL;DR80
10-
Defines the skeleton of an algorithm, deferring steps to subclasses.
8+
Defines the skeleton of a base algorithm, deferring definition of exact
9+
steps to subclasses.
1110
"""
1211

13-
ingredients = "spam eggs apple"
14-
line = '-' * 10
15-
16-
17-
# Skeletons
18-
def iter_elements(getter, action):
19-
"""Template skeleton that iterates items"""
20-
for element in getter():
21-
action(element)
22-
print(line)
23-
2412

25-
def rev_elements(getter, action):
26-
"""Template skeleton that iterates items in reverse order"""
27-
for element in getter()[::-1]:
28-
action(element)
29-
print(line)
13+
def get_text():
14+
return "plain-text"
3015

3116

32-
# Getters
33-
def get_list():
34-
return ingredients.split()
17+
def get_pdf():
18+
return "pdf"
3519

3620

37-
def get_lists():
38-
return [list(x) for x in ingredients.split()]
21+
def get_csv():
22+
return "csv"
3923

4024

41-
# Actions
42-
def print_item(item):
43-
print(item)
25+
def convert_to_text(data):
26+
print("[CONVERT]")
27+
return "{} as text".format(data)
4428

4529

46-
def reverse_item(item):
47-
print(item[::-1])
30+
def saver():
31+
print("[SAVE]")
4832

4933

50-
# Makes templates
51-
def make_template(skeleton, getter, action):
52-
"""Instantiate a template method with getter and action"""
34+
def template_function(getter, converter=False, to_save=False):
35+
data = getter()
36+
print("Got `{}`".format(data))
5337

54-
def template():
55-
skeleton(getter, action)
38+
if len(data) <= 3 and converter:
39+
data = converter(data)
40+
else:
41+
print("Skip conversion")
5642

57-
return template
43+
if to_save:
44+
saver()
5845

46+
print("`{}` was processed".format(data))
5947

60-
# Create our template functions
61-
templates = [
62-
make_template(s, g, a)
63-
for g in (get_list, get_lists)
64-
for a in (print_item, reverse_item)
65-
for s in (iter_elements, rev_elements)
66-
]
6748

68-
# Execute them
69-
for template in templates:
70-
template()
49+
if __name__ == "__main__":
50+
template_function(get_text, to_save=True)
51+
print("-" * 30)
52+
template_function(get_pdf, converter=convert_to_text)
53+
print("-" * 30)
54+
template_function(get_csv, to_save=True)
7155

7256
### OUTPUT ###
73-
# spam
74-
# ----------
75-
# eggs
76-
# ----------
77-
# apple
78-
# ----------
79-
# apple
80-
# ----------
81-
# eggs
82-
# ----------
83-
# spam
84-
# ----------
85-
# maps
86-
# ----------
87-
# sgge
88-
# ----------
89-
# elppa
90-
# ----------
91-
# elppa
92-
# ----------
93-
# sgge
94-
# ----------
95-
# maps
96-
# ----------
97-
# ['s', 'p', 'a', 'm']
98-
# ----------
99-
# ['e', 'g', 'g', 's']
100-
# ----------
101-
# ['a', 'p', 'p', 'l', 'e']
102-
# ----------
103-
# ['a', 'p', 'p', 'l', 'e']
104-
# ----------
105-
# ['e', 'g', 'g', 's']
106-
# ----------
107-
# ['s', 'p', 'a', 'm']
108-
# ----------
109-
# ['m', 'a', 'p', 's']
110-
# ----------
111-
# ['s', 'g', 'g', 'e']
112-
# ----------
113-
# ['e', 'l', 'p', 'p', 'a']
114-
# ----------
115-
# ['e', 'l', 'p', 'p', 'a']
116-
# ----------
117-
# ['s', 'g', 'g', 'e']
118-
# ----------
119-
# ['m', 'a', 'p', 's']
120-
# ----------
57+
# Got `plain-text`
58+
# Skip conversion
59+
# [SAVE]
60+
# `plain-text` was processed
61+
# ------------------------------
62+
# Got `pdf`
63+
# [CONVERT]
64+
# `pdf as text` was processed
65+
# ------------------------------
66+
# Got `csv`
67+
# Skip conversion
68+
# [SAVE]
69+
# `csv` was processed

0 commit comments

Comments
 (0)