FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/template.py at master · devinwang/python-patterns · GitHub
devinwang
/
python-patterns
Public
forked from
faif/python-patterns
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-patterns
/
template.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
57 lines (39 loc) · 1.19 KB
Breadcrumbs
python-patterns
/
template.py
Copy path
File metadata and controls
57 lines (39 loc) · 1.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/
An example of the Template pattern in Python"""
ingredients
=
"spam eggs apple"
line
=
'-'
*
10
# Skeletons
def
iter_elements
(
getter
,
action
):
"""Template skeleton that iterates items"""
for
element
in
getter
():
action
(
element
)
print
(
line
)
def
rev_elements
(
getter
,
action
):
"""Template skeleton that iterates items in reverse order"""
for
element
in
getter
()[::
-
1
]:
action
(
element
)
print
(
line
)
# Getters
def
get_list
():
return
ingredients
.
split
()
def
get_lists
():
return
[
list
(
x
)
for
x
in
ingredients
.
split
()]
# Actions
def
print_item
(
item
):
print
(
item
)
def
reverse_item
(
item
):
print
(
item
[::
-
1
])
# Makes templates
def
make_template
(
skeleton
,
getter
,
action
):
"""Instantiate a template method with getter and action"""
def
template
():
skeleton
(
getter
,
action
)
return
template
# Create our template functions
templates
=
[
make_template
(
s
,
g
,
a
)
for
g
in
(
get_list
,
get_lists
)
for
a
in
(
print_item
,
reverse_item
)
for
s
in
(
iter_elements
,
rev_elements
)]
# Execute them
for
template
in
templates
:
template
()
Back
|
FazBrowse Home
|
New Git URL