whos -- a function listing objects

B

benyang22

I have been a long time Matlab user. I Python, I miss Matlab's whos
command.

So I have written a little utility whos.py, which can be downloaded
here:
http://beluga.eos.ubc.ca/~tang/softwares/python/

Here is the doc string:

# Before using whos, do:
execfile('whos.py')

#=== EXAMPLES ===
# to see a list of all objects in the global scope
whos()

# to see a list of functions
whos('f')

# to see a list of variables (also their values)
whos('v')

# to see a list of Numeric array
whos('a')

# to see a list of variables in module os
import os
whos('v', module=os)

# to see a list of variables whose name contains 'path'
whos('v', sstr='path')

# to see a list of variables whose name contains 'path' (case
insensitive)
whos('v', sstr='path', casesen=0)

Argument what can be one of the following:
A -- all
a -- array
f -- function
m -- module
v -- variable
 
P

Piet van Oostrum

b> I have been a long time Matlab user. I Python, I miss Matlab's whos
b> command.
b> So I have written a little utility whos.py, which can be downloaded
b> here:
b> http://beluga.eos.ubc.ca/~tang/softwares/python/
b> Here is the doc string:
b> # Before using whos, do:
b> execfile('whos.py')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "whos.py", line 142, in whos
iType1 = string.split(str(iType), "'")[1]
NameError: global name 'string' is not defined

Using execfile is a bad way to get the function definition. Import is the
way to go. Just add a line:
import string
to the beginning of whos.py, and the the usage should be:

from whos import whos
whos()

or
import whos
whos.whos()
 
F

Fernando Perez

I have been a long time Matlab user. I Python, I miss Matlab's whos
command.

<plug> you might want to look at ipython. whos, and a bit more come for free:

planck[~]> ipython -pylab
Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
Type "copyright", "credits" or "license" for more information.

IPython 0.6.16_cvs -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

Welcome to pylab, a matplotlib-based Python environment.
For more information, type 'help(pylab)'.

In [1]: def foo(): pass
...:

In [2]: a=rand(128,128)

In [3]: x=3.14

In [4]: import code

In [5]: whos
Variable Type Data/Info
--------------------------------
a array 128x128: 16384 elems, type `d`, 131072 bytes (128 kb)
code module <module 'code' from '/usr<...>/lib/python2.3/code.pyc'>
foo function <function foo at 0x41203cdc>
x float 3.14

In [6]: whos array
Variable Type Data/Info
-----------------------------
a array 128x128: 16384 elems, type `d`, 131072 bytes (128 kb)

In [7]: whos function
Variable Type Data/Info
--------------------------------
foo function <function foo at 0x41203cdc>



Cheers,

f
 
B

benyang22

whos.py as a module would not work for global scope, as it hides the
function whos in the module scope.

I have fixed the string bug.
 
B

benyang22

That is nice. I didn't know iPython can do whos. Will try it.

iPython seems to infinitely configurable. Hope it will not suck too
much of my time into it.
 
F

Fernando Perez

That is nice. I didn't know iPython can do whos. Will try it.

iPython seems to infinitely configurable. Hope it will not suck too
much of my time into it.

It is. It probably will. It did to me :)

At least, I hope it will have been time well spent.

Best,

f

ps - you mention using matlab. Note that ipython integrates with matplotlib in
its -pylab mode, to provide interactive plotting without blocking for almost
all GUI backends (Tk, GTK, WX and Qt are supported, FLTK is not, though it
might work).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top