Skip to content

Commit aef1fd8

Browse files
committed
regular expression done
1 parent 62384e5 commit aef1fd8

9 files changed

Lines changed: 165 additions & 0 deletions

do_fork.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import os
2+
3+
print("Process (%s) start..." % os.getpid())
4+
pid=os.fork()
5+
if pid==0:
6+
print("I am child process (%s) and my parent is %s." %(os.getpid(),os.getppid()))
7+
else:
8+
print("I (%s) just created a child process (%s)." % (os.getpid(),pid))

do_lock.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import time,threading
2+
3+
balance=0
4+
lock=threading.Lock()
5+
6+
def change_it(n):
7+
global balance
8+
balance=balance+n
9+
balance=balance-n
10+
11+
def run_thread(n):
12+
for i in range(100000):
13+
lock.acquire()
14+
try:
15+
change_it(n)
16+
finally:
17+
lock.release()
18+
19+
t1=threading.Thread(target=run_thread,args=(5,))
20+
t2=threading.Thread(target=run_thread,args=(8,))
21+
t1.start()
22+
t2.start()
23+
t1.join()
24+
t2.join()
25+
print(balance)

do_multiprocessing.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from multiprocessing import Process
2+
import os
3+
4+
def run_proc(name):
5+
print("run child process %s (%s)..." % (name, os.getpid()))
6+
7+
if __name__=="__main__":
8+
print("parent process %s." % os.getpid())
9+
p=Process(target=run_proc,args=("test",))
10+
print("child process will start")
11+
p.start()
12+
p.join()
13+
print("child process end")
14+

do_queue.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from multiprocessing import Process, Queue
2+
import os, time, random
3+
4+
def write(q):
5+
print("process to write: %s" % os.getpid())
6+
for value in ["A", "B", "C"]:
7+
print("Put %s to queue..." % value)
8+
q.put(value)
9+
time.sleep(random.random())
10+
11+
12+
def read(q):
13+
print("process to read: %s" % os.getpid())
14+
while True:
15+
value=q.get(True)
16+
print("get %s from queue."% value)
17+
18+
19+
if __name__=="__main__":
20+
q=Queue()
21+
pw=Process(target=write,args=(q,))
22+
pr=Process(target=read,args=(q,))
23+
pw.start()
24+
pr.start()
25+
pw.join()
26+
pr.terminate()

do_subprocess.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import subprocess
2+
3+
print("$ nslookup www.python.org")
4+
r=subprocess.call(["nslookup","www.python.org"])
5+
print("Exit code:",r)
6+
7+
print("$ nslookup ")
8+
p=subprocess.Popen(["nslookup"],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
9+
output,err=p.communicate(b"set q=mx\npython.org\nexit\n")
10+
print(output.decode("utf-8"))
11+
print("Exit code:", p.returncode)

multi_threading.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import time, threading
2+
3+
def loop():
4+
print("thread %s is running..." % threading.current_thread().name)
5+
n=0
6+
while n<5:
7+
n=n+1
8+
print("thread %s >>> %s" % (threading.current_thread().name,n))
9+
time.sleep(1)
10+
print("thread %s ended" % threading.current_thread().name)
11+
12+
13+
print("thread %s is running..." % threading.current_thread().name)
14+
t=threading.Thread(target=loop,name="LoopThread")
15+
t.start()
16+
t.join()
17+
print("thread %s ended" % threading.current_thread().name)

pooled_processing.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from multiprocessing import Pool
2+
import os,time,random
3+
4+
def long_time_task(name):
5+
print("run task %s (%s)..." % (name,os.getpid()))
6+
start=time.time()
7+
time.sleep(random.random()*3)
8+
end=time.time()
9+
print("Task %s runs %0.2f seconds." % (name,(end-start)))
10+
11+
12+
if __name__=="__main__":
13+
print("Parent process %s." % os.getpid())
14+
p=Pool(8)
15+
for i in range(9):
16+
p.apply_async(long_time_task,args=(i,))
17+
print("waiting for all subprocesses done...")
18+
p.close()
19+
p.join()
20+
print("all subprocesses done")

regex.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import re
2+
print("Test: 010-12345")
3+
m=re.match(r"^(\d{3})-(\d{3,8})","010-12345")
4+
print(m.group(1),m.group(2))
5+
6+
t="19:05:30"
7+
print("Test:",t)
8+
m=re.match(r"^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])\:(0[0-9]|1[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])$",t)
9+
print(m.groups())
10+
11+
12+
s1="someone@gmail.com"
13+
s2="bill.gates@microsoft.com"
14+
m=re.match(r"^[\w\.]*@\w*.com$",s1)
15+
if m:
16+
print("%s match" %s1)
17+
re.match(r"^[\w\.]*@\w*.com$",s2)
18+
m=re.match(r"^[\w\.]*@\w*.com$",s2)
19+
if m:
20+
print("%s match" %s2)
21+
22+
s3="<Tom Paris> tom@voyager.org"
23+
m=re.match(r"^<[\w\s]+>\s\w*@\w*.\w*$",s3)
24+
if m:
25+
print("%s match" %s3)

use_threadlocal.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import threading
2+
3+
local_school=threading.local()
4+
5+
def process_student():
6+
std=local_school.student
7+
print("Hello, %s (in %s)" % (std,threading.current_thread().name))
8+
9+
def process_thread(name):
10+
local_school.student=name
11+
process_student()
12+
13+
14+
t1=threading.Thread(target=process_thread,args=("Alice",),name="Thread_A")
15+
t2=threading.Thread(target=process_thread,args=("Bob",),name="Thread_B")
16+
t1.start()
17+
t2.start()
18+
t1.join()
19+
t2.join()

0 commit comments

Comments
 (0)