By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,594 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,594 IT Pros & Developers. It's quick & easy.

Re: Type feedback tool?

P: n/a
I think that rope has something like that; not really sure though.

On Mon, Oct 27, 2008 at 1:41 AM, <sk**@pobox.comwrote:
(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__(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

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

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).


Oct 27 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.