Differences between revisions 1 and 12 (spanning 11 versions)
Revision 1 as of 2002-12-05 12:55:14
Size: 1425
Editor: cs6625177-168
Comment:
Revision 12 as of 2006-01-12 02:12:06
Size: 2526
Editor: 66
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Basically some of the most common code ideas are welcomed here. you probably want python as a shell. Here's some tips: The idea: using the Python interpreter as an InteractiveShell.
Line 3: Line 3:
= Hacks =

== general ideas ==
for all your code,
== lack of symbol for command execution ==
Solution: write short method names:
<code>
= Command execution =
Use short method names:
{{{
#!python
Line 24: Line 21:
  for i in SN():   for i in SN(arg):
Line 26: Line 23:
</code> }}}
Line 28: Line 25:
== dirname comprables ==
you might not know that you have improved functionality.
os.path.split replaces dirname and basename returning a tuple containing both.
os.path.splitext splits on the extension returning a tuple
To complete the set you might want to split the path at the root, use:
<code>
Command execution is the one thing an InteractiveShell has to be good at.
Typing S("<command>") is too much overhead for command execution. Still, a mixture of
bash style command execution and shell programming with Python would be great.


----
I'd want the simple style of command execution from bash available:
{{{
cd /foo/bar
}}}

But also the Python style for more complex commands:
{{{
os.setcwd('/foo/bar')
}}}

Some ways to execute the bash-style command:
  * Use os.popen(<command>).
  * Map all commands to Python functions: cd(), less(), all taking a list of strings as arguments.
  * Completely separate bash-style commands from Python commands, executing it with bash.

-- JohannesGijsbers [[DateTime(2002-12-07T03:34:05)]]

There is a project that attempts to acheive this. Quasi (http://quasi-shell.sourceforge.net/) provides a shell within which Python can be freely mixed with OS (and certain database) commands. -- BenLast

 This sounds a lot like [:lwickjr/Modules: lwickjr/Modules]/Alias.py when coupled with a module of shell-command functions. See ["lwickjr/Modules"] for further information.
  --["lwickjr"]

= Path manipulation =
The os.path module provides a good set of functions for path manipulation,
but you might also want to split the path at the root:
{{{
#!python
Line 42: Line 66:
</code> }}}
Line 44: Line 68:
== awk comprables ==
simply use re. its a fuller set of regular expressions. create a wrapper function
= awk comparables =
Simply use the re module. It's a fuller set of regular expressions. Create a wrapper function
Line 48: Line 72:
= Limitations =
The killall function was rejected by the ["BDFL"]. As far as I can see from the
previous version of this page, Guido rejected it because it isn't in POSIX.
I couldn't find any references on this in the mailing list archives. Anyone?
Line 49: Line 77:

Limitations:
  killall functionality in bash was reject by guido himself (BS explantion) "talk to maintainers of posix"
See also:
 * [http://www.gregorpurdy.com/gregor/psh/ The Perl Shell]

The idea: using the Python interpreter as an InteractiveShell.

Command execution

Use short method names:

   1 def S(arg):
   2   """returns string of executed command arg"""
   3   return os.popen(arg).read()
   4 
   5 def SN(arg):
   6   """returns list of executed command arg"""
   7   return os.popen(arg).read().split('\n')
   8 
   9 def SP(arg):
  10   """prints string of executed command arg"""
  11   print S(arg)
  12 
  13 def SNP(arg):
  14   """prints with lines list executed command arg"""
  15   for i in SN(arg):
  16     print i

Command execution is the one thing an InteractiveShell has to be good at. Typing S("<command>") is too much overhead for command execution. Still, a mixture of bash style command execution and shell programming with Python would be great.


I'd want the simple style of command execution from bash available:

cd /foo/bar

But also the Python style for more complex commands:

os.setcwd('/foo/bar')

Some ways to execute the bash-style command:

  • Use os.popen(<command>).

  • Map all commands to Python functions: cd(), less(), all taking a list of strings as arguments.
  • Completely separate bash-style commands from Python commands, executing it with bash.

-- JohannesGijsbers DateTime(2002-12-07T03:34:05)

There is a project that attempts to acheive this. Quasi (http://quasi-shell.sourceforge.net/) provides a shell within which Python can be freely mixed with OS (and certain database) commands. -- BenLast

  • This sounds a lot like [:lwickjr/Modules: lwickjr/Modules]/Alias.py when coupled with a module of shell-command functions. See ["lwickjr/Modules"] for further information.
    • --["lwickjr"]

Path manipulation

The os.path module provides a good set of functions for path manipulation, but you might also want to split the path at the root:

   1 import re
   2 def splitroot(s):
   3   if string.find(s,"/") == -1:
   4     return '',s
   5   if s[0] == '/':
   6     s = s[1:]
   7   m = re.match("(.*?)/(.*)$",s)
   8   return m.groups()

awk comparables

Simply use the re module. It's a fuller set of regular expressions. Create a wrapper function for a utility for this if you want call it inlinegrep.

Limitations

The killall function was rejected by the ["BDFL"]. As far as I can see from the previous version of this page, Guido rejected it because it isn't in POSIX. I couldn't find any references on this in the mailing list archives. Anyone?

See also:

UsePythonAsAnInteractiveShell (last edited 2009-11-19 08:27:58 by vpn-8061f54b)

Unable to edit the page? See the FrontPage for instructions.