调递归用bdb不用自己print
import bdb import linecache class Cdb(bdb.Bdb): def __init__(m, skip=None): print('''<html><style> *{font: 10pt "Source Code Pro", Hack, mono} b{color:black} i{color:green} </style><pre>''') bdb.Bdb.__init__(m, skip) m.level = m.cnt = 0 m.ns = set(); m.ns.add('cdb'); m.ns.add('Cdb'); m.ns.add('bdb'); m.ns.add('linecache') def __del__(m): print('</pre>') def ignore(m, name): m.ns.add(name) def user_call(m, f, arg_list): m.level += 1; print('\n' if m.cnt > 1 else '', end='') def user_line(m, f): def sd(m, dic): return str({k:v for (k,v) in dic.items() if not k.startswith('_') and not k in m.ns}) m.cnt += 1 if m.cnt == 1: return fn = m.canonic(f.f_code.co_filename) d = '<i>'+sd(m, f.f_globals)+sd(m, f.f_locals)+'</i>' c = linecache.getline(fn, f.f_lineno).rstrip('\n').lstrip(' \t') print(m.level, m.level*' ', d.ljust(40), '<b>'+c+'</b>') def user_return(m, frame, ret_val): m.level -= 1 def user_exception(frame, exc_info): pass def do_clear(m, arg): pass if __name__ == '__main__': m = 0 def fn(n): if n == 0: return 1 x = 1 << (n + m) x += fn(n - 1) return x from cdb import Cdb cdb = Cdb(); cdb.ignore('fn'); cdb.run('fn(3)')
The trace module allows you to trace program execution, generate annotated statement coverage listings, print caller/callee relationships and list functions executed during a program run. It can be used in another program or from the command line. 我真是闲得蛋疼。在Windows 10的cmd里彩色print的版本:

# The bdb module handles basic debugger functions, like setting breakpoints or managing execution via the debugger. # The class bdb.Bdb() acts as a generic Python debugger base class. It takes care of the details of the trace facility; # a derived class should implement user interaction. The standard debugger class (pdb.Pdb) is an example. import bdb import linecache from cmdcolor import Color as C O = 'rgb' class Cdb(bdb.Bdb): def __init__(m, skip=None): bdb.Bdb.__init__(m, skip); m.framecnt = 0; m.ns = set(); m.ns.add('cdb'); m.ns.add('Cdb') def ignore(m, name): m.ns.add(name) def pr_lines(m, f): def sl(m, obj): return [x for x in dir(obj) if not x.startswith('_')] # short list def sd(m, dic): return {k:v for (k,v) in dic.items() if not k.startswith('_') and not k in m.ns} # short dict fn = m.canonic(f.f_code.co_filename) for i in range(f.f_lineno - 1, f.f_lineno + 2): cur = i == f.f_lineno C('GB' if cur else 'g'),print(linecache.getline(fn, i), end=''), C(O) C('RGB', 'G'), print(' ', sd(m, f.f_globals), sd(m, f.f_locals), ' '), C(O) # Derived classes should override these methods to gain control over debugger operation. def user_call(m, frame, arg_list): 'Called from dispatch_call() when there is the possibility that a break might be necessary anywhere inside the called function.' m.framecnt += 1 def user_line(m, frame): 'Called from dispatch_line() when either stop_here() or break_here() yields True.' m.pr_lines(frame) def user_return(m, frame, ret_val): 'Called from dispatch_return() when stop_here() yields True.' if ret_val == None: return m.framecnt -= 1; C('R'); print(m.framecnt * ' ', 'Ret ', ret_val, sep=''); C(O) def user_exception(frame, exc_info): 'Called from dispatch_exception() when stop_here() yields True.' def do_clear(m, arg): 'Handle how a breakpoint must be removed when it is a temporary one.' 'This method MUST be implemented by derived classes.' # termcolor outputs stuff like '\033[0m'. It doesn't work in Windows 10's cmd. class Color: kernel32 = hStdOut = None def init(): import ctypes Color.user32 = ctypes.CDLL('user32.dll'); Color.kernel32 = ctypes.CDLL('kernel32.dll') # https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute # HANDLE WINAPI GetStdHandle(DWORD nStdHandle) # BOOL WINAPI SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes) # WINCON.H: #define FOREGROUND_BLUE 0x0001 // text color contains blue. #define FOREGROUND_GREEN 0x0002 // text color contains green. #define FOREGROUND_RED 0x0004 // text color contains red. #define FOREGROUND_INTENSITY 0x0008 // text color is intensified. #define BACKGROUND_BLUE 0x0010 // background color contains blue. #define BACKGROUND_GREEN 0x0020 // background color contains green. #define BACKGROUND_RED 0x0040 // background color contains red. #define BACKGROUND_INTENSITY 0x0080 // background color is intensified. STD_OUTPUT_HANDLE = -11 Color.hStdOut = Color.kernel32.GetStdHandle(STD_OUTPUT_HANDLE) def __init__(m, fg_str_or_color, bg = ''): if isinstance(fg_str_or_color, int): import sys sys.stdout.flush() Color.kernel32.SetConsoleTextAttribute(Color.hStdOut, fg_str_or_color) return def f(s): t = { 'b':1, 'B':8+1, 'g':2, 'G':8+2, 'r':4, 'R':8+4 } d = 0 for c in s: d |= t.get(c, 0) #print(s, hex(d)) return d m.__init__(f(fg_str_or_color) | (f(bg) << 4)) # 注意缩进和空行!缩进不对__str__成了__init__的内部函数,折腾了半天。 def __str__(m): return 'Color' Color.init() if __name__ == '__main__': Color('G'); print('Green ', end=''); Color('rgb'); print('gray') print(Color('RGB', 'G'), 'Hello', Color('rgb'), 'world') # Color Hello Color world # python先把参数求值完再去调print。 # CMD的color命令也可以设颜色。color/? # 如果有人想做个cmdcolor包,请把代码拿去自便。甚至可以扩展为WFP - Windows Fun Pack :-) # 比如一个线程空白DialogBox,另一个干事情。GetWindowDC()后就可以画画了。哪怕GetDC(NULL) # 在屏幕上画呢。pip install pywin32; D:\Python39\Lib\site-packages\PyWin32.chm # 简历里一句"开发过一个python wheel,在github上有n颗星"?I admit我爱幻想。 # win32console里一堆函数,如SetConsoleTitle,就是没有SetConsoleTextAttribute。 # 四子棋: https://www.cnblogs.com/funwithwords/p/15626636.html # 画线和实心椭圆…… GDI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?