Keeping Python loaded

Matt Gerrans mgerrans at ix.netcom.com
Wed Nov 28 04:22:40 EST 2001


Thanks, Kragen!

I made a mini-command interpreter to keep the engine going and run scripts on
the "command line."   It works great!  It is super fast (compared to the
normal start-up time for an individual script) and doesn't eat any CPU while
waiting for the next script to run.   I've included the script below, for
those who are interested.

- mfg

#!/usr/bin/python

import sys

def runScript(filename):
   namespace = { '__name__' : '__main__' }
   execfile(filename, namespace)
   return namespace

def readCommands( verbose = 0 ):
   while 1:
      cmd = raw_input('--> ')
      args = cmd.split()
      if len(args) > 0:
         if args[0].lower() in ('exit','quit','bye','adios'):
            print 'Bye!'
            return
         else:
            if (len(args[0]) < 3) or (args[0][-3:].lower() != 'py'):
               args[0] = args[0] + '.py'
            try:
               sys.argv = args
               if verbose:
                  print 'Running ' + args[0] + ' with args = ' + `sys.argv`
               runScript( args[0] )
            except:
               print '*'*20,'Bronk!','*'*20
               print 'Exception:  ', sys.exc_type
               print 'Extra clues:', sys.exc_value

def main():
   if '-v' in sys.argv:
      print 'Running in verbose mode!'
      readCommands(1)
   else:
      readCommands()

if __name__ == '__main__':
   main()







More information about the Python-list mailing list