Re: Type feedback tool?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skip@pobox.com

    Re: Type feedback tool?


    (Sorry for any repeated recommendations . I'm offline until Monday morning.
    You may well see some of these suggestions in the meanwhile, but so far it
    seems you've had no nibbles.)

    MartinI'm wondering if there's a tool that can analyze a Python
    Martinprogram while it runs, and generate a database with the types of
    Martinarguments and return values for each function.

    Nothing that I'm aware of. Here are a few ideas though.

    1. Modify the source code in question to decorate any functions you're
    interested in, e.g.:

    #!/usr/bin/env python

    class ZeroDict(dict):
    def __getitem__(sel f, key):
    if key not in self:
    return 0
    return dict.__getitem_ _(self, key)

    _argtypes = ZeroDict()

    def note_types(f):
    "Decorator that keeps track of counts of arg types for various functions."
    def _wrapper(*args) :
    _argtypes[(f,) + tuple([type(a) for a in args])] += 1
    return f(*args)
    return _wrapper

    @note_types
    def fib(n):
    if n < 0:
    raise ValueError, "n < 0"
    if n 1:
    return fib(n-1) + fib(n-2)
    return 1

    @note_types
    def fib2(n):
    "fib() that guarantees it is dealing with ints."
    if n < 0:
    raise ValueError, "n < 0"
    n = int(n)
    if n 1:
    return fib2(n-1) + fib2(n-2)
    return 1

    if __name__ == "__main__":
    print "fib(5) ==", fib(5)
    print "fib(4.0) ==", fib(4.0)
    print "fib2(5) ==", fib2(5)
    print "fib2(4.0) ==", fib2(4.0)
    print _argtypes

    You can probably write a source transformation tool to decorate all
    functions (or just use Emacs macros for a 99% solution which takes a lot
    less time).

    2. Look at tools like pdb. You might be able to add a new command to
    decorate a function in much the same way that you might set a breakpoint
    at a given function.

    3. Take a look at IDEs with source (like IDLE). You might be able to coax
    them into decorating functions then display the collected statistics when
    you view the source (maybe display a tooltip with the stats for a
    particular function).

    Skip
Working...