[Tutor] Redirecting output + calling dos commands with input tail

cino hilliard hillcino368@hotmail.com
Tue Jun 17 17:23:02 2003


Hi,
First thanks Rick for asking the question.

Magnus, maybe you can help me. The module below has functions that call dos 
commands.

pycall('dir') will list the current folder directory but all jumbled up.
pycall('dir>dir.txt') will list the current folder directory to file dir.txt 
nice and neat.
pycall('start excel') starts Excel.
pycall('start winword') starts Ms word.
pycall('start maplew8')   starts maple8
pycall('start p2017.exe') starts pari ver 2017
etc.

To see the dos shell commands type help at the cmd console prompt
c:\pari>help
For more information on a specific command, type HELP command-name
ASSOC    Displays or modifies file extension associations.
AT       Schedules commands and programs to run on a computer.
ATTRIB   Displays or changes file attributes.
[Snip]

pypari('primes(100)') list the first 100 primes. Output here isn't bad.
>>>pypari('primes(100)')
['? [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 
71,
73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 
157,
163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 
241,
251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 
347,
349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 
439,
443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 
541]\n',
'? ']
>>>pypari('factor(2^128+1)') factors the 8th Fermat prime
['? \n', '[59649589127497217 1]\n', '\n', '[5704689200685129054721 1]\n', 
'\n',
'? ']

There we have it. Python is as powerful as Pari.

Questions.
1. can the technique you used to help Rick (see redir() in the shell module) 
be used in shell to
to make pycall and pypari do what they do  here? Give me some help on 
exiting processes.

2. can the dos console output be formatted to be pretty eg., dir, help etc?

3. When I make a modification to shell.py I have to close the current 
python.exe chevron >>>
window and start another and  from shell import * again or open another >>> 
and from shell import *
Any way around this? I want to keep the last >>> because the previous 
commands can be
retreived with f3 and the arrow keys. The import shell.pycall etc doesn't 
work in xp pro p4 either.

4. Wish list. Right string,left string,mid string functions, etc  built in. 
see my instr() below

Thanks for all your work

#********************************The Shell Game 
shell.py***************************
#Usage: >>> from shell import *
# pycall('cmd[>]')
#pypari('function')

import os
import sys
def popen2(cmd, bufsize=-1, mode='t'):       #use popen2 to perform dos 
shell operations
    w,r = os.popen2(cmd, mode, bufsize)
    return r,w

def pycall(cmd):                             #call a dos file or command
    r,w = popen2(cmd)                   #seems to require both r,w
    getit = r.readlines()
    print(getit)

def pypari(cmd):                              #call and execute a pari 
command
    f=open("pari.txt","w")                    #save command to a file
    p=instr(cmd,">")                          #check for dos redirection ">" 
command
    if p > 0:                                 #if ">" exists then write it 
to file
       f.write(cmd[:p])
    else:                                     #otherwise just write pari 
command to file
       f.write(cmd)
       paristr = 'c:\pari\p -q <pari.txt'     #save the pari path with < 
file parameter
    f.close()
    if p > 0:                                       #if you want to redirect 
pari output to
       paristr = 'c:\pari\p -q <pari.txt' + cmd[p:] #a file append the > and 
file you put in cmd
       print paristr                                 #show some output for 
sanity
    r,w = popen2(paristr)
    getit = r.readlines()
    print(getit)

def redir(cmd):                              #redirect output to a file. I 
attempted to
    oldout = sys.stdout                      #use this method to do the 
above to no avail.
    print 'redirecting'
    sys.stdout = open(cmd,'w')
    print 'this text to file',cmd
    sys.stdout.close()
    sys.stdout = oldout
    print 'back to normal'


def instr(str,chr):                         #instr() get the position of a 
single character in a string
   ln = len(str)
   pos = 0
   for i in range(ln):
       if str[i] == chr:
          pos=i
          break
   return pos



  3         3        3         3        3        6            2              
  (0^0)
2   +  13  +  33  +  43  =  49   =  7    =  343   = 117649

_________________________________________________________________
MSN 8 helps eliminate e-mail viruses. Get 2 months FREE*.  
http://join.msn.com/?page=features/virus