[Numpy-discussion] Unifying numpy, scipy, and matplotlib docstring formats

Jouni K. Seppänen jks@iki...
Sun Feb 25 10:44:37 CST 2007


"Barry Wark" <barrywark@gmail.com> writes:

> Yes, I agree. I wasn't coming at so much from the goal of making Pylab
> a Matlab clone (as you point out, that's silly, and misses much of the
> advantage of Python), but rather from the goal of making interactive
> use as efficient as possible. When I fire up ipython -pylab to do some
> quick exploration, it's nice not to have to type N.blah or pylab.plot

IMHO the greatest strength of Matlab in interactive use is the matrix
input format. For one thing, it is easier to type something like

  [0 1 0; 1 0 0; 0 0 1]

than

  array([[0,1,0],[1,0,0],[0,0,1]])

Granted, you can often leave out the array() wrapper, but typing all
the commas and brackets and getting the nesting right slows me down
enough that using Python feels like tedious work where Matlab is more
like an Emacs-like extension of the mind. Another neat feature is
auto-flattening: to e.g. add row- and column-wise sums and a grand
total to a matrix M, you can type

  [M sum(M,2); sum(M,1) sum(M(:))]

compared to which the r_[] and c_[] syntax feels like an ugly hack.

(Of course, the auto-flattening feature is a disaster for serious
programming (as opposed to quick interactive work), so Matlab has
added cell arrays which don't auto-flatten, leading to no end of
confusion between [] and {} indexing and the need to add {:} in
seemingly random spots in Matlab code.)

I suppose these things could be addressed quite neatly by IPython. 
It could even modify your history similarly to what it currently 
does with the %magic commands, so that when you type

  a = [0 1 0; 1 0 0; 0 0 1]

and then examine your history, you see

  a = array([[0,1,0],[1,0,0],[0,0,1]])

which you could copy-paste into the program you are developing.

Perhaps the namespace issue could also be addressed at the IPython
level. The pylab profile could import the various packages, perhaps
with some kind of abbreviated names, and rewrite commands like

  a = array(...)
  plot(sin(a))

to

  a = numpy.array(...)
  pylab.plot(numpy.sin(a))

so again you could copy/paste from the history to an editor and get
correctly Pythonic code without any "from ... import *". Probably a
100% solution is quite difficult because of the dynamic features in
Python, but it seems to me that a 80% solution should be feasible.
(Parse the input to an AST using the parser facility in the Python
library, use a tree walker to find all references to functions or
variables, and if they don't exist in locals() or globals() and are
not the target of an assignment anywhere in the AST, replace them by
references to the appropriate package.)

-- 
Jouni K. Seppänen
http://www.iki.fi/jks



More information about the Numpy-discussion mailing list