[IPython-user] Inconsistency in "!!" ?

Walter Dörwald walter at livinglogic.de
Fri Jun 23 03:01:45 CDT 2006


Krishna Mohan Gundu wrote:
> Hello,
> 
>>> For more info see http://projects.scipy.org/ipython/ipython/wiki/UsingIPipe
> 
> Thanks for the link Walter. Interesting stuff in there.
> 
>> Implementing !/!! so that they would work inside arbitrary statements
>> would be very complex, now it's just
> 
> I have been making a note to myself as to what I felt are
> inconsistencies in regards to the shell behavior. Probably this is the
> best place to mention them.
> 
> 1) var = !command => both outputs and assigns the result. Should there
> be output?
> 2) \tab+ $[$]var = command => syntax error.
> 3) no variable substitution in sout(command_string) and lout(command_string)
> 4) absence of sout() and lout() in 'sh' profile
> 
> Currently it all looks complicated. I suggest the following. Instead
> of making the shell command syntax open ended (actually ending in
> '\n') we use a token to signify the start and  the end of a shell
> command. I suggest ``, as it is an illegal python syntax

Actually it isn't, it's a (deprecated) way of writing repr():

In [1]: `"foo"`
Out[1]: "'foo'"

> and resembles
> shell execute syntax. Something like
> 
> [var =] ``[keyword] command``
> 
> keyword could be (note that quotes are required to allow
> disambiguation from a command name)
> "list" - return the output as a list splitting at \n (default)
> "string" - return the output as a string
> "result" - return the exit status of command
> 
> Each of these commands are replaced with, say, _ip.getoutput() or
> _ip.getresult() so they can be directly invoked if desired. This can
> be done with a prefilter hook. I think this reads much cleaner. What
> do you say?

Why not use plain Python syntax?

    foo = `"string" ls`

isn't that much shorter than

    foo = ipstring("ls")

Of course `ls` (without keyword) is hard to beat, but typing ` on a 
german keyboard is cumbersome.

2/3rds of what you want is rather simple (albeit a bit longer) with ipipe:

In [2]: foo = list(ix("ls"))
Out[2]:
['ChangeLog',
  'IPython',
  'MANIFEST.in',
  'README',
  'README_Windows.txt',
  'debian',
  'doc',
  'eggsetup.py',
  'ipython.py',
  'scripts',
  'setup.py',
  'setup_bdist_egg.py',
  'setupext',
  'test',
  'tools',
  'win32_manual_post_install.py']
In [3]: "\n".join(ix("ls"))
Out[3]: 
'ChangeLog\nIPython\nMANIFEST.in\nREADME\nREADME_Windows.txt\ndebian\ndoc\neggsetup.py\nipython.py\nscripts\nsetup.py\nsetup_bdist_egg.py\nsetupext\ntest\ntools\nwin32_manual_post_install.py'

Would it make sense to add shortcuts for that?

Servus,
    Walter




More information about the IPython-user mailing list