|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 |
|
4 | 4 | """ |
5 | | -http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/ |
6 | | -
|
7 | 5 | An example of the Template pattern in Python |
8 | 6 |
|
9 | 7 | *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. |
11 | 10 | """ |
12 | 11 |
|
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 | | - |
24 | 12 |
|
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" |
30 | 15 |
|
31 | 16 |
|
32 | | -# Getters |
33 | | -def get_list(): |
34 | | - return ingredients.split() |
| 17 | +def get_pdf(): |
| 18 | + return "pdf" |
35 | 19 |
|
36 | 20 |
|
37 | | -def get_lists(): |
38 | | - return [list(x) for x in ingredients.split()] |
| 21 | +def get_csv(): |
| 22 | + return "csv" |
39 | 23 |
|
40 | 24 |
|
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) |
44 | 28 |
|
45 | 29 |
|
46 | | -def reverse_item(item): |
47 | | - print(item[::-1]) |
| 30 | +def saver(): |
| 31 | + print("[SAVE]") |
48 | 32 |
|
49 | 33 |
|
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)) |
53 | 37 |
|
54 | | - def template(): |
55 | | - skeleton(getter, action) |
| 38 | + if len(data) <= 3 and converter: |
| 39 | + data = converter(data) |
| 40 | + else: |
| 41 | + print("Skip conversion") |
56 | 42 |
|
57 | | - return template |
| 43 | + if to_save: |
| 44 | + saver() |
58 | 45 |
|
| 46 | + print("`{}` was processed".format(data)) |
59 | 47 |
|
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 | | -] |
67 | 48 |
|
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) |
71 | 55 |
|
72 | 56 | ### 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