Reference
Introduction to datetime object
JerryCheng 發表在 痞客邦 留言(0) 人氣(22)
Return Value of threads
# -*- coding: UTF-8 -*-
def foo(bar, result, index):
print 'hello {0}'.format(bar)
result[index] = "foo"
from threading import Thread
threads = [None] * 10
results = [None] * 10
for i in range(len(threads)):
threads[i] = Thread(target=foo, args=('world!', results, i))
threads[i].start()
# do some other stuff
for i in range(len(threads)):
threads[i].join()
print " ".join(results) # what sound does a metasyntactic locomotive make?
JerryCheng 發表在 痞客邦 留言(0) 人氣(75)
class base:
def __init__(self, name):
self.name = name
def show(self):
print "Base Class is here!!"
def show_name(self):
print "name:", self.name
def process(self):
self.show_name()
self.show()
class sub(base):
def show(self): // override show method
print "Sub class is here!!"
base_class = base("Base Class")
base_class.process()
sub_class = sub("Sub Class")
sub_class.process()
JerryCheng 發表在 痞客邦 留言(0) 人氣(10)
d = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted(d.items(), key=lambda x: x[1])
JerryCheng 發表在 痞客邦 留言(0) 人氣(1)
# -*- coding: UTF-8 -*-
import time
import threading
def myfunc(i):
time.sleep(5)
print "finished sleeping from thread %d"%(i)
def main():
for i in range(10):
t = threading.Thread(target = myfunc , args=(i,))
time.sleep(1)
t.start()
print t.name
if __name__ == '__main__':
main()
JerryCheng 發表在 痞客邦 留言(0) 人氣(62)
import time
import datetime
def interval_secs(time_start , time_end):
diff = time_end - time_start
return diff.seconds + diff.days * 24 * 60 * 60
time_start = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S")
time_start = datetime.datetime.strptime(time_start , "%y-%m-%d-%H-%M-%S")
print time_start
time.sleep(3)
time_end = datetime.datetime.now()
print interval_secs(time_start , time_end)
JerryCheng 發表在 痞客邦 留言(0) 人氣(14)
在python中 , 若要像c一樣傳入command line的arguments ,
可以藉由sys.argv的方式來取得argument的value:
import sys
for arg in sys.argv:
JerryCheng 發表在 痞客邦 留言(0) 人氣(816)
在python中 , 我們若要將輸入的內容一行行的讀取 ,
可以使用splitlines()這個method ,
for example :
infors=str(execSystemCmd('cat /etc/hosts')).splitlines()
JerryCheng 發表在 痞客邦 留言(0) 人氣(41)
在python中 , 我們可以藉由strip()來去掉字串頭尾的空白,
for example:
def execSystemCmd(cmd):
proc = subprocess.Popen([cmd],shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
JerryCheng 發表在 痞客邦 留言(0) 人氣(367)