Discussion:
whos and clear (as in matlab) functionality
Fernando Perez
2003-03-20 17:22:28 UTC
Permalink
I'm a python newbie, currently in the process of moving
parts of my work from Matlab to python + Numeric.
When using python interactively, I often need something
like the Matlab "whos" command.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 4;
b = [1 2 3; 4 5 6];
whos
Name Size Bytes Class
a 1x1 8 double array
b 2x3 48 double array
Grand total is 7 elements using 56 bytes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
is there any way of doing something similar in python?
Would this work for you?

In [1]: a=4;

In [2]: b=array([[1,2,3],[4,5,6]]);

In [3]: whos
Variable Type Data/Length
------------------------------
a int 4
b array [[1 2 3]
[4 5 6]]


Take a look at http://ipython.scipy.org/

Cheers,

f
Thomas Knudsen
2003-03-20 10:02:57 UTC
Permalink
I'm a python newbie, currently in the process of moving
parts of my work from Matlab to python + Numeric.

When using python interactively, I often need something
like the Matlab "whos" command.

In Matlab "whos" returns a list of all variables:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 4;
b = [1 2 3; 4 5 6];
whos
Name Size Bytes Class

a 1x1 8 double array
b 2x3 48 double array

Grand total is 7 elements using 56 bytes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

is there any way of doing something similar in python?
A recent post by Michael Chermside, in the thread
"How to get the 'name' of an int" suggests the following for
the slightly different task of listing names and values:

for name, value in locals().iteritems():
print "%s = %s" % (name, value)

any suggestions of how I could change this to name, type?

On a related note, the Matlab commands "clear all; pack"
clears all variables in the current namespace and starts
garbage collection. Is there a corresponding python idiom?
--
thomas knudsen - kms, copenhagen, denmark - http://research.kms.dk/~thk
Fernando Perez
2003-03-20 17:22:28 UTC
Permalink
I'm a python newbie, currently in the process of moving
parts of my work from Matlab to python + Numeric.
When using python interactively, I often need something
like the Matlab "whos" command.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 4;
b = [1 2 3; 4 5 6];
whos
Name Size Bytes Class
a 1x1 8 double array
b 2x3 48 double array
Grand total is 7 elements using 56 bytes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
is there any way of doing something similar in python?
Would this work for you?

In [1]: a=4;

In [2]: b=array([[1,2,3],[4,5,6]]);

In [3]: whos
Variable Type Data/Length
------------------------------
a int 4
b array [[1 2 3]
[4 5 6]]


Take a look at http://ipython.scipy.org/

Cheers,

f
Oren Tirosh
2003-03-20 11:44:41 UTC
Permalink
I'm a python newbie, currently in the process of moving
parts of my work from Matlab to python + Numeric.
When using python interactively, I often need something
like the Matlab "whos" command.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a = 4;
b = [1 2 3; 4 5 6];
whos
Name Size Bytes Class
a 1x1 8 double array
b 2x3 48 double array
Grand total is 7 elements using 56 bytes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
is there any way of doing something similar in python?
A recent post by Michael Chermside, in the thread
"How to get the 'name' of an int" suggests the following for
print "%s = %s" % (name, value)
any suggestions of how I could change this to name, type?
for name, value in locals().iteritems():
print "%s : %s" % (name, type(value))

You can also use type(value).__name__ for a more compact output.
On a related note, the Matlab commands "clear all; pack"
clears all variables in the current namespace and starts
garbage collection. Is there a corresponding python idiom?
Not really. You can do vars().clear() but then you will lose important
reserved variables like __builtins__. I guess you could do something
like the following one-liner abomination:

[vars().__delitem__(_k) for _k in vars().keys() if not _k.startswith('_')]

The equivalent of pack would be 'import gc; gc.collect()' but it's
rarely necessary because reference counting usually disposes of
garbage immediately.

Oren

Continue reading on narkive:
Loading...