forked from ydf0509/python36patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkey_print2.py
34 lines (28 loc) · 985 Bytes
/
monkey_print2.py
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
30
31
32
33
34
# -*- coding: utf-8 -*-
# @Author : ydf
# @Time : 2019/5/9 19:02
"""
不直接给print打补丁,自己重新赋值。
"""
import sys
import time
# noinspection PyProtectedMember,PyUnusedLocal,PyIncorrectDocstring
def nb_print(*args, sep=' ', end='\n', file=None):
"""
超流弊的print补丁
:param x:
:return:
"""
# 获取被调用函数在被调用时所处代码行数
line = sys._getframe().f_back.f_lineno
# 获取被调用函数所在模块文件名
file_name = sys._getframe(1).f_code.co_filename
# sys.stdout.write(f'"{__file__}:{sys._getframe().f_lineno}" {x}\n')
args = (str(arg) for arg in args) # REMIND 防止是数字不能被join
sys.stdout.write(f'"{file_name}:{line}" {time.strftime("%H:%M:%S")} \033[0;94m{"".join(args)}\033[0m\n') # 36 93 96 94
print = nb_print # 这是打猴子补丁。
if __name__ == '__main__':
print(0)
nb_print(123, 'abc')
print = nb_print
print(456, 'def')