def exec_snoop(filename, code, code_obj): snoop.formatting.Source._class_local('__source_cache', {}).pop(filename, None) config = snoop.Config( columns=(), out=sys.stdout, color=True, ) tracer = config.snoop() tracer.variable_whitelist = set() for node in ast.walk(ast.parse(code)): if isinstance(node, ast.Name): name = node.id tracer.variable_whitelist.add(name) tracer.target_codes.add(code_obj) def find_code(root_code): """ Trace all functions recursively, like trace_module_deep. """ for sub_code_obj in root_code.co_consts: if not inspect.iscode(sub_code_obj): continue find_code(sub_code_obj) tracer.target_codes.add(sub_code_obj) find_code(code_obj) with tracer: execute(code_obj)
def exec_snoop(filename, code, code_obj): snoop.formatting.Source._class_local('__source_cache', {}).pop(filename, None) config = snoop.Config( columns=(), out=sys.stdout, color=True, ) tracer = config.snoop() tracer.variable_whitelist = set() for node in ast.walk(ast.parse(code)): if isinstance(node, ast.Name): name = node.id tracer.variable_whitelist.add(name) tracer.target_codes.add(code_obj) with tracer: execute(code_obj)
def exec_snoop(runner, code_obj): class PatchedFrameInfo(snoop.tracer.FrameInfo): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.is_ipython_cell = self.frame.f_code == code_obj snoop.tracer.FrameInfo = PatchedFrameInfo snoop.formatting.Source._class_local('__source_cache', {}).pop(runner.filename, None) config = snoop.Config( columns=(), out=sys.stdout, color=True, ) tracer = config.snoop() tracer.variable_whitelist = set() for node in ast.walk(ast.parse(runner.source_code)): if isinstance(node, ast.Name): name = node.id tracer.variable_whitelist.add(name) tracer.target_codes.add(code_obj) def find_code(root_code): """ Trace all functions recursively, like trace_module_deep. """ for sub_code_obj in root_code.co_consts: if not inspect.iscode(sub_code_obj): continue find_code(sub_code_obj) tracer.target_codes.add(sub_code_obj) find_code(code_obj) with tracer: runner.execute(code_obj)
""" A simple demo to illustrate how docstringer works """ import logging import random import snoop from docstringer import docstringer from docstringer.formatters import ( LoggerFormatter, EventListFormatter, DefaultFormatter, PrintSimpleFormatter, ) snoop_formatted = snoop.Config(color=False, prefix="", columns=[]).snoop formatter = PrintSimpleFormatter() # # Using the LoggerFormatter ... # logging.basicConfig(level=logging.INFO, format="%(levelname)s - %(message)s") # formatter = LoggerFormatter(logging, "info") # # Using the EventListFormatter ... # event_list = [] # formatter = EventListFormatter(event_list) @docstringer(formatter=formatter) def roll_the_dice(rolls: int, sides: int = 6) -> tuple: """ This is a function where we roll a number of dice.