FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-patterns/factory_method.py at master · thujeevan/python-patterns · GitHub
thujeevan
/
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
/
factory_method.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
33 lines (25 loc) · 906 Bytes
Breadcrumbs
python-patterns
/
factory_method.py
Copy path
File metadata and controls
33 lines (25 loc) · 906 Bytes
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
#encoding=utf-8
"""http://ginstrom.com/scribbles/2007/10/08/design-patterns-python-style/"""
class
GreekGetter
:
"""A simple localizer a la gettext"""
def
__init__
(
self
):
self
.
trans
=
dict
(
dog
=
"σκύλος"
,
cat
=
"γάτα"
)
def
get
(
self
,
msgid
):
"""We'll punt if we don't have a translation"""
try
:
return
self
.
trans
[
msgid
]
except
KeyError
:
return
str
(
msgid
)
class
EnglishGetter
:
"""Simply echoes the msg ids"""
def
get
(
self
,
msgid
):
return
str
(
msgid
)
def
get_localizer
(
language
=
"English"
):
"""The factory method"""
languages
=
dict
(
English
=
EnglishGetter
,
Greek
=
GreekGetter
)
return
languages
[
language
]()
# Create our localizers
e
,
g
=
get_localizer
(
"English"
),
get_localizer
(
"Greek"
)
# Localize some text
for
msgid
in
"dog parrot cat bear"
.
split
():
print
(
e
.
get
(
msgid
),
g
.
get
(
msgid
))
Back
|
FazBrowse Home
|
New Git URL