Skip to content

Commit fe24b54

Browse files
meta class
1 parent 1c1bece commit fe24b54

File tree

3 files changed

+267
-1
lines changed

3 files changed

+267
-1
lines changed

.vscode/launch.json

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python",
9+
"type": "python",
10+
"request": "launch",
11+
"stopOnEntry": true,
12+
"pythonPath": "${config:python.pythonPath}",
13+
"program": "${file}",
14+
"cwd": "${workspaceRoot}",
15+
"env": {},
16+
"envFile": "${workspaceRoot}/.env",
17+
"debugOptions": [
18+
"WaitOnAbnormalExit",
19+
"WaitOnNormalExit",
20+
"RedirectOutput"
21+
]
22+
},
23+
{
24+
"name": "PySpark",
25+
"type": "python",
26+
"request": "launch",
27+
"stopOnEntry": true,
28+
"osx": {
29+
"pythonPath": "${env:SPARK_HOME}/bin/spark-submit"
30+
},
31+
"windows": {
32+
"pythonPath": "${env:SPARK_HOME}/bin/spark-submit.cmd"
33+
},
34+
"linux": {
35+
"pythonPath": "${env:SPARK_HOME}/bin/spark-submit"
36+
},
37+
"program": "${file}",
38+
"cwd": "${workspaceRoot}",
39+
"env": {},
40+
"envFile": "${workspaceRoot}/.env",
41+
"debugOptions": [
42+
"WaitOnAbnormalExit",
43+
"WaitOnNormalExit",
44+
"RedirectOutput"
45+
]
46+
},
47+
{
48+
"name": "Python Module",
49+
"type": "python",
50+
"request": "launch",
51+
"stopOnEntry": true,
52+
"pythonPath": "${config:python.pythonPath}",
53+
"module": "module.name",
54+
"cwd": "${workspaceRoot}",
55+
"env": {},
56+
"envFile": "${workspaceRoot}/.env",
57+
"debugOptions": [
58+
"WaitOnAbnormalExit",
59+
"WaitOnNormalExit",
60+
"RedirectOutput"
61+
]
62+
},
63+
{
64+
"name": "Integrated Terminal/Console",
65+
"type": "python",
66+
"request": "launch",
67+
"stopOnEntry": true,
68+
"pythonPath": "${config:python.pythonPath}",
69+
"program": "${file}",
70+
"cwd": "",
71+
"console": "integratedTerminal",
72+
"env": {},
73+
"envFile": "${workspaceRoot}/.env",
74+
"debugOptions": [
75+
"WaitOnAbnormalExit",
76+
"WaitOnNormalExit"
77+
]
78+
},
79+
{
80+
"name": "External Terminal/Console",
81+
"type": "python",
82+
"request": "launch",
83+
"stopOnEntry": true,
84+
"pythonPath": "${config:python.pythonPath}",
85+
"program": "${file}",
86+
"cwd": "",
87+
"console": "externalTerminal",
88+
"env": {},
89+
"envFile": "${workspaceRoot}/.env",
90+
"debugOptions": [
91+
"WaitOnAbnormalExit",
92+
"WaitOnNormalExit"
93+
]
94+
},
95+
{
96+
"name": "Django",
97+
"type": "python",
98+
"request": "launch",
99+
"stopOnEntry": true,
100+
"pythonPath": "${config:python.pythonPath}",
101+
"program": "${workspaceRoot}/manage.py",
102+
"cwd": "${workspaceRoot}",
103+
"args": [
104+
"runserver",
105+
"--noreload",
106+
"--nothreading"
107+
],
108+
"env": {},
109+
"envFile": "${workspaceRoot}/.env",
110+
"debugOptions": [
111+
"WaitOnAbnormalExit",
112+
"WaitOnNormalExit",
113+
"RedirectOutput",
114+
"DjangoDebugging"
115+
]
116+
},
117+
{
118+
"name": "Flask",
119+
"type": "python",
120+
"request": "launch",
121+
"stopOnEntry": false,
122+
"pythonPath": "${config:python.pythonPath}",
123+
"program": "fully qualified path fo 'flask' executable. Generally located along with python interpreter",
124+
"cwd": "${workspaceRoot}",
125+
"env": {
126+
"FLASK_APP": "${workspaceRoot}/quickstart/app.py"
127+
},
128+
"args": [
129+
"run",
130+
"--no-debugger",
131+
"--no-reload"
132+
],
133+
"envFile": "${workspaceRoot}/.env",
134+
"debugOptions": [
135+
"WaitOnAbnormalExit",
136+
"WaitOnNormalExit",
137+
"RedirectOutput"
138+
]
139+
},
140+
{
141+
"name": "Flask (old)",
142+
"type": "python",
143+
"request": "launch",
144+
"stopOnEntry": false,
145+
"pythonPath": "${config:python.pythonPath}",
146+
"program": "${workspaceRoot}/run.py",
147+
"cwd": "${workspaceRoot}",
148+
"args": [],
149+
"env": {},
150+
"envFile": "${workspaceRoot}/.env",
151+
"debugOptions": [
152+
"WaitOnAbnormalExit",
153+
"WaitOnNormalExit",
154+
"RedirectOutput"
155+
]
156+
},
157+
{
158+
"name": "Pyramid",
159+
"type": "python",
160+
"request": "launch",
161+
"stopOnEntry": true,
162+
"pythonPath": "${config:python.pythonPath}",
163+
"cwd": "${workspaceRoot}",
164+
"env": {},
165+
"envFile": "${workspaceRoot}/.env",
166+
"args": [
167+
"${workspaceRoot}/development.ini"
168+
],
169+
"debugOptions": [
170+
"WaitOnAbnormalExit",
171+
"WaitOnNormalExit",
172+
"RedirectOutput",
173+
"Pyramid"
174+
]
175+
},
176+
{
177+
"name": "Watson",
178+
"type": "python",
179+
"request": "launch",
180+
"stopOnEntry": true,
181+
"pythonPath": "${config:python.pythonPath}",
182+
"program": "${workspaceRoot}/console.py",
183+
"cwd": "${workspaceRoot}",
184+
"args": [
185+
"dev",
186+
"runserver",
187+
"--noreload=True"
188+
],
189+
"env": {},
190+
"envFile": "${workspaceRoot}/.env",
191+
"debugOptions": [
192+
"WaitOnAbnormalExit",
193+
"WaitOnNormalExit",
194+
"RedirectOutput"
195+
]
196+
},
197+
{
198+
"name": "Attach (Remote Debug)",
199+
"type": "python",
200+
"request": "attach",
201+
"localRoot": "${workspaceRoot}",
202+
"remoteRoot": "${workspaceRoot}",
203+
"port": 3000,
204+
"secret": "my_secret",
205+
"host": "localhost"
206+
}
207+
]
208+
}

python/jinja2/test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22

33
from jinja2 import Environment, FileSystemLoader
4+
import os
45
tpl_vars={
56
'user':{
67
'name' : 'shambhu',
@@ -11,7 +12,8 @@
1112
]
1213
}
1314
if __name__ == '__main__':
14-
env = Environment(loader=FileSystemLoader('./templates'))
15+
dirname= os.path.dirname(os.path.abspath(__file__))
16+
env = Environment(loader=FileSystemLoader(os.path.join(dirname, 'templates')))
1517
template = env.get_template('config.json')
1618
print(template.render(tpl_vars))
1719

python/metaclass/test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
4+
def create_class():
5+
class_name = "Foo"
6+
class_parents = (object,)
7+
class_body = """
8+
def __init__(self, name):
9+
self.name = name
10+
11+
def print_name(self):
12+
print(self.name)
13+
"""
14+
# a new dict is used as local namespace
15+
class_dict = {}
16+
17+
#the body of the class is executed using dict from above as local
18+
# namespace
19+
exec(class_body, globals(), class_dict)
20+
21+
# final step of class creation
22+
Foo = type(class_name, class_parents, class_dict)
23+
return Foo
24+
25+
26+
class DocMeta(type):
27+
def __init__(self, name, bases, attrs):
28+
for key, value in attrs.items():
29+
print(key)
30+
# skip special and private methods
31+
if key.startswith("__"): continue
32+
# skip any non-callable
33+
if not hasattr(value, "__call__"): continue
34+
# check for a doc string. a better way may be to store
35+
# all methods without a docstring then throw an error showing
36+
# all of them rather than stopping on first encounter
37+
if not getattr(value, '__doc__'):
38+
raise TypeError("%s must have a docstring" % key)
39+
type.__init__(self, name, bases, attrs)
40+
41+
class MyClass(metaclass=DocMeta):
42+
def __init__(self, a):
43+
self.a = a
44+
45+
def print_a(self):
46+
''' prints the value of a'''
47+
print(a)
48+
49+
if __name__=='__main__':
50+
#Dynamically creating classes
51+
my_class = create_class()
52+
a = my_class('hello')
53+
a.print_name()
54+
55+
56+
#Custom Metaclass

0 commit comments

Comments
 (0)