GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — python-patterns/catalog.py at master · ChristosChristofidis/python-patterns

Expand file tree

/

catalog.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
64 lines (49 loc) · 1.67 KB
/

catalog.py

Copy path

File metadata and controls

64 lines (49 loc) · 1.67 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
A class that uses different static function depending of a parameter passed in
init. Note the use of a single dictionary instead of multiple conditions
"""
__author__ = "Ibrahim Diop <http://ibrahim.zinaria.com>"
__gist__ = "<https://gist.github.com/diopib/7679559>"
class Catalog():
"""
catalog of multiple static methods that are executed depending on an init
parameter
"""
def __init__(self, param):
# dictionary that will be used to determine which static method is
# to be executed but that will be also used to store possible param
# value
self._static_method_choices = {'param_value_1': self._static_method_1,
'param_value_2': self._static_method_2}
# simple test to validate param value
if param in self._static_method_choices.keys():
self.param = param
else:
raise ValueError("Invalid Value for Param: {0}".format(param))
@staticmethod
def _static_method_1():
print("executed method 1!")
@staticmethod
def _static_method_2():
print("executed method 2!")
def main_method(self):
"""
will execute either _static_method_1 or _static_method_2
depending on self.param value
"""
self._static_method_choices[self.param]()
def main():
"""
>>> c = Catalog('param_value_1').main_method()
executed method 1!
>>> Catalog('param_value_2').main_method()
executed method 2!
"""
test = Catalog('param_value_2')
test.main_method()
if __name__ == "__main__":
main()
### OUTPUT ###
# executed method 2!
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.