Martin Vilcans wrote:
Hi list,

I'm wondering if there's a tool that can analyze a Python program
while it runs, and generate a database with the types of arguments and
return values for each function. In a way it is like a profiler, that
instead of measuring how often functions are called and how long time
it takes, it records the type information. So afterwards, when I'm
reading the code, I can go to the database to see what data type
parameter "foo" of function "bar" typically has. It would help a lot
with deciphering old code.

When I googled this, I learned that this is called "type feedback",
and is used (?) to give type information to a compiler to help it
generate fast code. My needs are much more humble. I just want a
faster way to understand undocumented code with bad naming.

It should be pretty easy to do this as a decorator, then (perhaps
automatically) sprinkle it around your source.

    def watch(function):
        def wrapped(*args, **kwargs):
            argv = map(type, args)
            argd = dict((key, type(value)) for key, value
                         in kwargs.iteritems())
            try:
                result = function(*args, **kwargs)
            except Exception, exc:
                record_exception(function, type(exc), argv, argd)
                raise
            else:
                record_result(function, type(result), argv, argd)
        return wrapped

then fill your normal code with:

   @watch
   def somefun(arg, defarg=1):
       ...
   ...


Finally record_* could write to a data structure (using bits like
function.__name__, and f.__module__, and possibly goodies from inspect).
Then you wrap your actual code start with something like:

try:
    main(...)
finally:
    <write data structure to DB>


--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to