Differences between revisions 1 and 3 (spanning 2 versions)
Revision 1 as of 2002-12-05 12:55:14
Size: 1425
Editor: cs6625177-168
Comment:
Revision 3 as of 2002-12-05 13:07:13
Size: 1430
Editor: cs6625177-168
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
[[TableOfContents]]
Line 9: Line 9:
<code> {{{
Line 26: Line 26:
</code> }}}
Line 33: Line 33:
<code> {{{
Line 42: Line 42:
</code> }}}

Basically some of the most common code ideas are welcomed here. you probably want python as a shell. Here's some tips: TableOfContents

Hacks

general ideas

for all your code,

lack of symbol for command execution

Solution: write short method names:

def S(arg):
  """returns string of executed command arg"""
  return os.popen(arg).read()

def SN(arg):
  """returns list of executed command arg"""
  return os.popen(arg).read().split('\n')

def SP(arg):
  """prints string of executed command arg"""
  print S(arg)

def SNP(arg):
  """prints with lines list executed command arg"""
  for i in SN():
    print i

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:

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

awk comprables

simply use re. its a fuller set of regular expressions. create a wrapper function for a utility for this if you want call it inlinegrep.

Limitations:

  • killall functionality in bash was reject by guido himself (BS explantion) "talk to maintainers of posix"

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

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