Type feedback tool?
Orestis Markou
orestis at orestis.gr
Mon Oct 27 09:54:46 EDT 2008
I think that rope has something like that; not really sure though.
On Mon, Oct 27, 2008 at 1:41 AM, <skip at pobox.com> wrote:
>
> (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.)
>
> Martin> I'm wondering if there's a tool that can analyze a Python
> Martin> program while it runs, and generate a database with the types of
> Martin> arguments 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__(self, 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
orestis at orestis.gr
http://orestis.gr
More information about the Python-list
mailing list