def test_dotrace(): handlers = { "fruit": lambda fruit, **kwargs: log.append(fruit), "sleep/**": lambda _event, **kwargs: log.append(_event), } log = [] with DoTrace(handlers, post=lambda: log.append("end")): day() assert log == ["banana", "apple", "enter", "dream", "dream", "exit", "end"] log = [] with DoTrace(handlers, focus="breakfast/**/"): day() assert log == ["banana", "apple"]
def test_dotrace(): handlers = { 'fruit': lambda fruit, **kwargs: log.append(fruit), 'sleep/**': lambda _event, **kwargs: log.append(_event) } log = [] with DoTrace(handlers, post=lambda: log.append('end')): day() assert log == ['banana', 'apple', 'enter', 'dream', 'dream', 'exit', 'end'] log = [] with DoTrace(handlers, focus='breakfast/**/'): day() assert log == ['banana', 'apple']
def log(path=None, *fields, **kwfields): """Log fields of interest on the given path. The breakword module is used for logging, thus it is possible to set a word upon which to enter a breakpoint (using the BREAKWORD environment variable). * When no path is given, show all events. * The "help" field shows all possible fields. """ getters = Getters(fields, kwfields) def _p(**kwargs): _curpath = kwargs['_curpath'] results = getters(kwargs) _display(_curpath, results) return DoTrace({_resolve_path(path): _p})
def compare(path=None, *fields, **kwfields): store = {} getters = Getters(fields, kwfields) def _compare(old, new): if isinstance(old, dict): return {k: _compare(v, new[k]) for k, v in old.items()} elif isinstance(old, (int, float)): diff = new - old if diff == 0: return old c = Fore.LIGHTGREEN_EX if diff > 0 else Fore.LIGHTRED_EX diff = f'+{diff}' if diff > 0 else str(diff) return f'{old} -> {new} ({_color(c, diff)})' elif hasattr(old, 'compare'): return old.compare(new) elif old == new: return old else: return f'{old} -> {new}' def _enter(_curpath, **kwargs): _path = _curpath[:-6] w = breakword.word() store[_path] = (w, getters(kwargs)) _brk(w) def _exit(_curpath, **kwargs): if 'success' in kwargs and not kwargs['success']: return _path = _curpath[:-5] w, old = store[_path] new = getters(kwargs) _display(_path, _compare(old, new), word=w, brk=False) path = _resolve_path(path, variant='cmp') return DoTrace({ f'{path}/enter': _enter, f'{path}/exit': _exit, })
def _setflag2(): def _set(**_): _flag2[0] = True return DoTrace({'/compile/exit': _set})
def _setflag1(): def _set(**_): _flag1[0] = True return DoTrace({'/compile/enter': _set})
def _pgraph(path): """Print a graph using Buche.""" def _p(graph, **_): bucheg(graph) return lambda: DoTrace({path: _p})