python tricks

打印进度条

def progress(percent,width=50):
    if percent >= 1:
        percent=1
    show_str = ('%%-%ds' % width) % (int(width*percent)*'|')
    print('\r%s %d%%' %(show_str, int(100*percent)), end='')

线程锁

from threading import RLock

lock = RLock()

def thread_A():
    lock.acquire()
    try:
        if "foo" not in global_dict:
            global_dict["foo"] = 1
    finally:
        lock.release()

def thread_B():
    lock.acquire()
    try:
        global_dict["foo"] = 2
    finally:
        lock.release()

带颜色输出

[Eva_J博客] (https://www.cnblogs.com/Eva-J/p/8330517.html "控制台输出带颜色字体")

posted @ 2019-02-27 16:11  -愚-  阅读(155)  评论(0编辑  收藏  举报