Type feedback tool?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Vilcans

    Type feedback tool?

    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.

    --
    martin@librador .com

  • Scott David Daniels

    #2
    Re: Type feedback tool?

    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.iteritem s())
    try:
    result = function(*args, **kwargs)
    except Exception, exc:
    record_exceptio n(function, type(exc), argv, argd)
    raise
    else:
    record_result(f unction, 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
    Scott.Daniels@A cm.Org

    Comment

    Working...