FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/patterns/behavioral/template.py at master · botdigit/python-patterns · GitHub
botdigit
/
python-patterns
Public
forked from
faif/python-patterns
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-patterns
/
patterns
/
behavioral
/
template.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
75 lines (51 loc) · 1.31 KB
Breadcrumbs
python-patterns
/
patterns
/
behavioral
/
template.py
Copy path
File metadata and controls
75 lines (51 loc) · 1.31 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
An example of the Template pattern in Python
*TL;DR80
Defines the skeleton of a base algorithm, deferring definition of exact
steps to subclasses.
*Examples in Python ecosystem:
Django class based views: https://docs.djangoproject.com/en/2.1/topics/class-based-views/
"""
def
get_text
():
return
"plain-text"
def
get_pdf
():
return
"pdf"
def
get_csv
():
return
"csv"
def
convert_to_text
(
data
):
print
(
"[CONVERT]"
)
return
"{} as text"
.
format
(
data
)
def
saver
():
print
(
"[SAVE]"
)
def
template_function
(
getter
,
converter
=
False
,
to_save
=
False
):
data
=
getter
()
print
(
"Got `{}`"
.
format
(
data
))
if
len
(
data
)
<=
3
and
converter
:
data
=
converter
(
data
)
else
:
print
(
"Skip conversion"
)
if
to_save
:
saver
()
print
(
"`{}` was processed"
.
format
(
data
))
def
main
():
"""
>>> template_function(get_text, to_save=True)
Got `plain-text`
Skip conversion
[SAVE]
`plain-text` was processed
>>> template_function(get_pdf, converter=convert_to_text)
Got `pdf`
[CONVERT]
`pdf as text` was processed
>>> template_function(get_csv, to_save=True)
Got `csv`
Skip conversion
[SAVE]
`csv` was processed
"""
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
Back
|
FazBrowse Home
|
New Git URL