time
1 2 3 4
| import time
test_time = str(time.strftime("%Y-%m-%d_%H.%M'%S\'\'"))
|
logging
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import logging
def get_logging(log_path): logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(filename)s %(levelname)s\t%(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=log_path, filemode='w') logger = logging.getLogger(__name__) return logger
def print_log(print_string, logger, log_type): print("{}".format(print_string)) if(log_type == 'info'): logger.info("{}".format(print_string))
logger = get_logging('path/to/log.txt') print_log(opt, logger, 'info')
|
将会在'path/to/log.txt'
所设路径下生成日志文件,log.txt
格式如下:
1 2 3
| 2020-04-08 11:43:30 xxx.py INFO xxxxxx 2020-04-08 11:43:31 xxx.py INFO xxxxxxx ......
|
os.path
python是默认以当前Terminal的运行路径作为基路径的,若要获取某一个py文件所在的路径,os.path相关使用技巧如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import os
abspath = os.path.abspath(__file__) print(abspath) >>> /home/reborn/xxx/test.py
pwd = os.path.dirname(abspath) print(pwd) >>> /home/reborn/xxx
basename1 = os.path.basename(abspath) basename2 = os.path.basename(pwd) print(basename1) print(basename2) >>> test.py >>> xxx
|
对list型矩阵的变换技巧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| def print_matrix(mat): for x in mat: print(' '.join([str(n) for n in x]))
a = [[1,2,3], [4,5,6],[7,8,9]] print_matrix(a) >>> 1 2 3 >>> 4 5 6 >>> 7 8 9
print_matrix(zip(*a)) >>> 1 4 7 >>> 2 5 8 >>> 3 6 9
print_matrix(a[::-1]) >>> 7 8 9 >>> 4 5 6 >>> 1 2 3
print_matrix(zip(*a[::-1])) >>> 7 4 1 >>> 8 5 2 >>> 9 6 3
print_matrix(map(reversed, a[::-1])) >>> 9 8 7 >>> 6 5 4 >>> 3 2 1
|
thread —— 捕获子线程异常
当子线程中发生异常时,子线程生命周期结束,可在主进程中通过判断子线程状态来返回异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from threading import Thread
class thread1: def __init__(self): pass def start(self): self.t = Thread(target=self.update, args=()) self.t.daemon = True self.t.start() return self def update(self): pass
if(__name__ == '__main__'): tt = thread1() tt.start() while(1): if(not tt.t.is_alive()): raise Exception
|
打印异常信息
- traceback.print_exc(): 直接打印异常信息
- traceback.format_exc(): 返回异常信息的字符串,便于记录到日志中
1 2 3 4 5 6 7 8
| import traceback
try: except: print(traceback.format_exc()) traceback.print_exc()
|