[Python-Dev] RE: ConfigParser patches

Fernando Perez fperez528 at yahoo.com
Sun Oct 3 06:28:48 CEST 2004


Raymond Hettinger wrote:

> My understanding was that it wasn't about a full pathname to python, it
> was about searching sys.path for the darned script so us poor Windows
> guys don't have to change directories a million times a day (no aliases
> or shebangs for us either).
> 
> I have a big pile of batch files just to invoke timeit, texchecker,
> profile, etc.  It bites the big one.

Sorry for the plug, but I hope you'll forgive it if it actually turns out to be
useful.  You may want to have a look at ipython (http://ipython.scipy.org). 
IPython has a 'magic' run command, which allows you to run any python script
from its command line:

In [1]: cat argv.py
#!/usr/bin/env python
import sys

print 'argv:',sys.argv

In [2]: run argv
argv: ['argv.py']

In its current form, it is just an execfile() on steroids.  But it would be
trivial to add to it a -m option so that it would search in sys.path if nothing
in the current directory is found matching the given name.

Ipython also gives you aliases and 'bookmarks', a persistent list of directories
you can change to with a single 'cd' command:

############### 'screenshot'

In [1]: pdoc alias
Define an alias for a system command.

'@alias alias_name cmd' defines 'alias_name' as an alias for 'cmd'

Then, typing 'alias_name params' will execute the system command 'cmd
params' (from your underlying operating system).

[... snip.  It's a long docstring]

In [6]: pdoc bookmark
Manage IPython's bookmark system.

@bookmark <name>       - set bookmark to current dir
@bookmark <name> <dir> - set bookmark to <dir>
@bookmark -l           - list all bookmarks
@bookmark -d <name>    - remove bookmark
@bookmark -r           - remove all bookmarks

You can later on access a bookmarked folder with:
  @cd -b <name>
or simply '@cd <name>' if there is no directory called <name> AND
there is such a bookmark defined.

################ /screenshot

Using ipython's profile system (referred to earlier in this thread), you can use
it as a quasi-system shell.  The provided 'pysh' profile loads all of your
$PATH (with proper extensions in Windows) as ipython aliases, and you can do
regular system things, but retaining python syntax.  It has also a mechanism
for capturing shell output into python variables and expanding back to the
shell:

############### 'screenshot'

fperez at maqroll[~/test]|4> print 'Yes, this is still %s !' % 'Python'
Yes, this is still Python !
fperez at maqroll[~/test]|5> $$files=ls
fperez at maqroll[~/test]|6> type files
------------------------> type(files)
                      <6> <type 'list'>
fperez at maqroll[~/test]|7> len files
------------------------> len(files)
                      <7> 31
fperez at maqroll[~/test]|8> for f in files:
                      |.>     if len(f)>10:
                      |.>         wc -l $f
                      |.>
      4 inspectbug.py
     73 ramptest.py
fperez at maqroll[~/test]|9> run argv.py
argv: ['argv.py']

################ /screenshot

Note that the 'run' command optionally takes a -p switch, to run code under the
profiler's control.  Ipython can also automatically activate the pdb debugger
inside any uncaught exception, thanks to the @pdb magic.

I simply point these things in the hope that they are useful to you.  From what
I've read on this thread, ipython seems to already give most of the motivations
for the -m switch, and it works today with python >= 2.2.  Try it, you might
like it :) (I'm not trying to say the -m switch is a bad idea, simply that
ipython may provide a useful alternative today).

Ok, end of blatant plug.  Sorry if it was out of line.

Cheers,

f



More information about the Python-Dev mailing list