Il giorno 18 settembre 2012 03:41, Walter Valenti <waltervale...@yahoo.it>ha scritto:
> > > > > > 2012/9/18 Giacomo Alzetta <giacomo.alze...@gmail.com>: > >> On Tuesday 18 September 2012 10:57:41 Walter Valenti wrote: > >>> C'è un modo pulito e veloce per tracciare le chiamate ai metodi di > > una > >>> classe, a parte delle print ? > > > > Questo sicuramente. > > > > O magari intende "tracciare" nel senso dei vari trace del prolog o dei > > linguaggi funzionali? > > > > Intendo proprio loggare le chiamate Classe.metodo. > > Mi guarderò i logging. > > Walter > _______________________________________________ > Python mailing list > Python@lists.python.it > http://lists.python.it/mailman/listinfo/python > Se intendi vedere quali linee vengono eseguite, linea per linea, puoi usare qualcosa di questo tipo: # copyright Giampaolo Rodola', license MIT import sys, linecache def tracethis(callback=None): """A decorator to trace a callable. >>> @tracethis() ... def foo(): ... ret = [] ... ret.append(range(10)) ... return ret ... >>> foo() __main__:5: ret = [] __main__:6: ret.append(range(10)) __main__:7: return ret >>> Example for logging traces of a certain module only: >>> def callback(name, lineno, line): ... if name == __name__: ... print "%s:%s: %s" % (name, lineno, line) ... >>> >>> @tracethis(callback) ... def foo(): ... pass ... >>> """ def run(frame, event, arg): if event == "line": lineno = frame.f_lineno filename = frame.f_globals["__file__"] if (filename.endswith(".pyc") or filename.endswith(".pyo")): filename = filename[:-1] name = frame.f_globals["__name__"] line = linecache.getline(filename, lineno).rstrip() if callback is None: print "%s:%s: %s" % (name, lineno, line) else: callback(name, lineno, line) return run def outer(fun): def inner(*args, **kwargs): sys.settrace(run) try: return fun(*args, **kwargs) finally: sys.settrace(None) return inner return outer # test if __name__ == '__main__': @tracethis() def foo(): ret = [] ret.append(range(10)) return ret foo() --- Giampaolo http://code.google.com/p/pyftpdlib/ <http://code.google.com/p/psutil/> http://code.google.com/p/psutil/ http://code.google.com/p/pysendfile/ <http://code.google.com/p/psutil/>
_______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python