目前分類:Python (9)
- Oct 06 Mon 2014 17:25
python - time
- Sep 16 Tue 2014 17:39
python - thread
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?
Basic example:
- Apr 14 Mon 2014 16:31
python - inheritance & template pattern
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()
- Apr 08 Tue 2014 18:03
sorting by value of dictionary
d = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted(d.items(), key=lambda x: x[1])
- Apr 07 Mon 2014 14:27
python - thread example
# -*- 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()
- Apr 07 Mon 2014 14:23
python - time
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)
- Oct 17 Wed 2012 16:50
python - command line arguments
- Oct 17 Wed 2012 13:48
python - splitlines()
- Oct 17 Wed 2012 13:35
python - strip()