Skip to content

Commit 3a07607

Browse files
author
Kevin Li
committed
add new practise materials
1 parent fd6b652 commit 3a07607

12 files changed

Lines changed: 262 additions & 0 deletions
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
__author__ = 'k22li'
2+
3+
4+
import sys, copy
5+
6+
class Animal():
7+
"""test reflection basis"""
8+
def __init__(self, name):
9+
self.name = name
10+
11+
def printName(self):
12+
print self.name
13+
14+
15+
16+
if __name__ == "__main__":
17+
"""test reflection basis -- main"""
18+
cat = Animal('kitty')
19+
# print cat.name
20+
# cat.printName()
21+
22+
23+
if hasattr(cat, 'name'):
24+
print getattr(cat, 'name')
25+
setattr(cat,'name', 'tougher')
26+
27+
cat.printName()
28+
29+
#
30+
# print dir(testInstance)
31+
#
32+
# k = globals()['testInstance']
33+
#
34+
# print 'reflection', k.printName()
1001 Bytes
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__author__ = 'k22li'
2+
3+
import imglib
4+
5+
print imglib
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
__author__ = 'k22li'
2+
3+
WRITE_SUPPORT = True
4+
from os.path import split, isdir
5+
from os import mkdir
6+
7+
8+
9+
def _bypass_ensure_directory(name, mode=0x1FF): # 0777
10+
# Sandbox-bypassing version of ensure_directory()
11+
if not WRITE_SUPPORT:
12+
raise IOError('"os.mkdir" not supported on this platform.')
13+
dirname, filename = split(name)
14+
print '*'*80
15+
print dirname
16+
print filename
17+
18+
if dirname and filename and not isdir(dirname):
19+
print '#'*80
20+
_bypass_ensure_directory(dirname)
21+
mkdir(dirname, mode)
22+
23+
24+
25+
if __name__ == '__main__':
26+
27+
_bypass_ensure_directory(r'c:\evo_testfile\PYTHON\kkk\BBB')
28+
29+
30+
31+
#################################################################################################################################################
32+
#RUN RESULT
33+
34+
#C:\apps\python27\python.exe C:/Users/k22li/workspace/gitHub/Python_Projects/python/Python_3rdParty_libs/python_pkg_resources.py
35+
#********************************************************************************
36+
#c:\evo_testfile\java
37+
#kkk
38+
#################################################################################
39+
#********************************************************************************
40+
#c:\evo_testfile
41+
#java
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
__author__ = 'k22li'
2+
3+
import sys
4+
5+
print sys.version_info
6+
= sys.version_info
7+
8+
print len(k)
9+
10+
print k > (3, 7)
11+
12+
# tuple compare
13+
14+
15+
a = (1, 3, 4, 32)
16+
b = (2, 3, 4)
17+
18+
print a < b
19+
20+
21+
k

Python_Basic/appen_vs_extend.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
__author__ = 'k22li'
2+
"""
3+
difference between extend & append
4+
"""
5+
6+
a = list(('a', 'b', 'c'))
7+
8+
print a
9+
10+
a.append('d')
11+
12+
print a
13+
14+
a.extend('f')
15+
16+
print a
17+
18+
a.append(['h'])
19+
20+
print a
21+
22+
a.extend(['i', 'j'])
23+
24+
print a

Python_Basic/class_inheritation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
__author__ = 'k22li'
2+
3+
import os.path as path
4+
5+
print path.splitdrive(r'c:\abc\sde')
6+
7+
print path.splitext(r'c:\abc\sde')
8+
print path.splitext(r'c:\abc\sde.asdfa')
9+
10+
print path.dirname(r'c:\abc\ada')
11+
12+
print path.exists(r'c:\abc')
13+
14+
print path.isabs(r'..\abc')
15+
16+
17+
print path.splitext('PMCL01.jar')
18+
19+
print path.splitdrive(r'.\asdfas')

Python_Basic/py2exe_test.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__author__ = 'k22li'
2+
3+
import py2exe

Python_Basic/python_callback.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
__author__ = 'k22li'
2+
3+
4+
class test:
5+
6+
def my_callback(self, input):
7+
print "function my_callback was called with %s input" % (input,)
8+
9+
def caller(self, input, func):
10+
func(input)
11+
12+
13+
def test(self):
14+
self.caller(5, self.my_callback)
15+
16+
17+
if __name__ == '__main__':
18+
19+
t = test()
20+
21+
# for i in range(5):
22+
# t.caller(i, t.my_callback)
23+
t.caller()
24+
25+
import atexit as ae
26+
print dir(ae)

Python_Basic/python_callback_1.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class CallbackBase:
2+
def __init__(self):
3+
4+
self.__callbackMap = {}
5+
6+
for k in (getattr(self, x) for x in dir(self)):
7+
if hasattr(k, "bind_to_event"):
8+
self.__callbackMap.setdefault(k.bind_to_event, []).append(k)
9+
elif hasattr(k, "bind_to_event_list"):
10+
for j in k.bind_to_event_list:
11+
self.__callbackMap.setdefault(j, []).append(k)
12+
13+
## staticmethod is only used to create a namespace
14+
@staticmethod
15+
def callback(event):
16+
17+
def f(g, ev = event):
18+
g.bind_to_event = ev
19+
return g
20+
return f
21+
22+
@staticmethod
23+
def callbacklist(eventlist):
24+
25+
def f(g, evl = eventlist):
26+
g.bind_to_event_list = evl
27+
return g
28+
return f
29+
30+
def dispatch(self, event):
31+
l = self.__callbackMap[event]
32+
f = lambda *args, **kargs:\
33+
map(lambda x: x(*args, **kargs), l)
34+
return f
35+
36+
## Sample
37+
class MyClass(CallbackBase):
38+
EVENT1 = 1
39+
EVENT2 = 2
40+
41+
@CallbackBase.callback(EVENT1)
42+
def handler1(self, param = None):
43+
print "handler1 with param: %s" % str(param)
44+
return None
45+
46+
@CallbackBase.callbacklist([EVENT1, EVENT2])
47+
def handler2(self, param = None):
48+
print "handler2 with param: %s" % str(param)
49+
return None
50+
51+
def run(self, event, param = None):
52+
self.dispatch(event)(param)
53+
54+
55+
if __name__ == "__main__":
56+
a = MyClass()
57+
a.run(MyClass.EVENT1, 'mandarina')
58+
a.run(MyClass.EVENT2, 'naranja')

0 commit comments

Comments
 (0)