1. 正則表達式
import re
content = """Python was created in the early 1990s by Guido van Rossum at Stichting Mathematisch Centrum (CWI, see http://www.cwi.nl) in the Netherlands as a successor of a language called ABC. Guido remains Python's principal author, although it includes many contributions from others."""
# re module function: compile
# re module / regex object functins: match, search, findall, finditer,
# matching object functins: split, sub, group, groups
print "RE learning"
print "1."
m = re.match("Python", content) # return match obj
if m is not None:
print type(m.group())
print m.group() # get a string
m = re.search(r"\bthe\b", content)
if m is not None:
print type(m.group())
print m.group()
print "2."
m = re.match('(\w\w\w)-(\d\d\d)', 'abc-123')
print m.group()
print m.group(1) # get the first s ubgroup string
print m.group(2)
print m.groups() # get the strings in a tuple
print "3."
m = re.search('^The', 'The end.')
if m is not None: print m.group()
m = re.search(r'\bthe', 'in the room')
if m is not None: print m.group()
m = re.findall(r'\bthe\b', content) # return a list of string
if m is not None: print m
print "4."
m = re.sub('X', 'Mr. Zhang', 'to: X\nDear X\n') # return a string
print m
m = re.subn('X', 'Mr. Zhang', 'to: X\nDear X\n') # return a tuple
print m
print "5."
m = re.split(':', 'str1:str2:str3') # return a list, and can use RE as delimiter
print m
2. 多線程
python虛擬機的訪問由全局解釋器鎖(GIL)來控制,保證同一時刻只有一個線程在運行。
流程:1. 設置GIL 2. 運行線程 3. 運行指定數量的指令或者主動讓出控制(例如time.sleep(0)) 4. 設置線程爲休眠狀態 5. 解鎖GIL
在調用外部代碼(如C/C++擴展函數)的時候,GIL將會被鎖定,直到這個函數結束爲止。
不使用thread模塊的考慮:
(1) threading跟完善
(2) threading模塊有豐富的同步原語
(3) 對進程什麼時候結束完全沒有控制,threading模塊可以確保重要的子線程退出後進程才退出。
print "Thread learning"
print "a running function"
import threading
from time import sleep, ctime
loops = [4,2]
def loop(nloop, nsec):
print "start loop", nloop, "at time:", ctime()
sleep(nsec)
print "loop", nloop, "Done at time:", ctime()
def main():
print "main starting at:", ctime()
threads = []
nloops = range(len(loops))
for i in nloops:
t = threading.Thread(target = loop, args = (i, loops[i]))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print "all Done at time:", ctime()
#if __name__ == '__main__':
# main()
print "an class object"
class ThreadFunc(object):
def __init__(self, func, args, name=''):
self.name = name
self.func = func
self.args = args
def __call__(self):
apply(self.func, self.args)
def main2():
# t = threading.Thread(target = ThreadFunc(loop, (i, loops[i]), loop.__name__))