[Tutor] Newbie: Passing variable into re.compile

2006-03-29 Thread Jerome Jabson
Hello,

 

I’m having trouble trying to pass a variable into
re.compile(), using the “r” option. Let’s say I
have the following:

 

import re

arg1 = ‘10”

p = re.compile(r + arg1 + '\.(\d+\.\d+\.\d+)')

 

This works if I do:

 

p = re.compile(r ‘10\.(\d+\.\d+\.\d+)')

 

Is there something special I need to do for the
“r” option? Like escape it when using a variable?

 

Thanks,
Jerome


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] string formatting question

2006-04-07 Thread Jerome Jabson
Hello,

I'm trying to replace some strings in a line of text,
using some regex functions. My question is: If there's
more then one regex grouping I want to replace in one
line of a file, how can I use the String Formatting
operator (%s) in two places?

Here's the line it matches in the file:



Here's the regex:
m_sock = re.compile('(portNumber=)"\d+"
(tcpORudp=)"[A-Z]+"')

My replace should look like this:
\1 "112" \2 "TCP" 
(obviously "112" and "TCP" would be varibles)

My problem now is how do I construct the replace
statement?
twork = m_sock.sub('\1 %s \2 %s', % port_num % proto,
twork)

But of course this does not work! :-( Is there a
better way to do this? Or am I just doing this all
wrong?

Thanks in advance!

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string formatting question

2006-04-07 Thread Jerome Jabson
Hi Kent,

Sorry I didn't make my question clearer. Bascially I
want to replace this line:



With:



So the regex grouping are that I want to keep
portNumber= and tcpORudp= and replace the values.
Which will be varibles in my code. 

The question is more on the string formatting in the
replace. How do use two %s in one statement? 

i.e.: re.sub('\1 %s \2 %s' % var1 % var2, line)

Thanks again!


> Hello,
> 
> I'm trying to replace some strings in a line of
text,
> using some regex functions. My question is: If
there's
> more then one regex grouping I want to replace in
one
> line of a file, how can I use the String Formatting
> operator (%s) in two places?

Hi Jerome,

I don't understand your question. Can you give a
complete example of 
the 
  line from the file and the new line you want to
create?
> 
> Here's the line it matches in the file:
> 
>  address="64.41.134.60"/>
> 
> Here's the regex:
> m_sock = re.compile('(portNumber=)"\d+"
> (tcpORudp=)"[A-Z]+"')

You have put parentheses around fixed strings, so your
groups will 
always be the same. Is that what you want?
> 
> My replace should look like this:
> \1 "112" \2 "TCP" 
> (obviously "112" and "TCP" would be varibles)

This looks like you want to make the string
portNumber= 112 tcpORudp= TCP

but that doesn't have any variable text from the
original string so I 
think I must not understand.

Kent

> 
> My problem now is how do I construct the replace
> statement?
> twork = m_sock.sub('\1 %s \2 %s', % port_num %
proto,
> twork)
> 
> But of course this does not work! :-( Is there a
> better way to do this? Or am I just doing this all
> wrong?
> 
> Thanks in advance!
> 

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] HELP: using popen2/popen3

2006-05-04 Thread Jerome Jabson
Hello,

I'm trying to start a shell script from my python
script using os.spawnlp. But I'm not getting msgs from
stderr. I thought that I could use popen2/popen3 to
replace os.spawnlp but I'm having problems with syntax
and how to do this. Can someone point me in the right
direction? Here's my original script (w/o using
popen2/popen3):


import os
import sys


 Definition 


def set_up():
   print "=== Starting Setup ==="
   os.chdir('/home/qauser/jerome')
   os.spawnlp(os.P_WAIT,
'/home/qauser/jerome/qaSetup.sh')
   print "=== Setup  Complete ==="


##  Main  ##


set_up()


I was trying to replace the os.spawnlp with:

sout, sin, serr =
os.open('/home/qauser/jerome/qaSetup.sh')
sout.readlines()
serr.readlines()
sout.close()
serr.close()
sin.close()

Many thanks,
Jerome

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] using variables as args

2006-05-08 Thread Jerome Jabson
Hello,

I'm trying to start a shell script with 4 arguments
from my python script. I'm having problems trying to
figure out the best way to do this. I'm using
variables as the arguments to the shell script. I want
to use popen3 to keep track of stdin, stdout, and err,
but from the docs I only see using spawnlp to use args
in starting an other process. Can someone help me
choice the best way to do this and the correct syntax
how?

Your help is much appreciated,
Jerome

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using variables as args

2006-05-08 Thread Jerome Jabson
Thank you, Alan! I'll try using POPEN.

Jerome

--- Alan Gauld <[EMAIL PROTECTED]> wrote:

> > I'm trying to start a shell script with 4
> arguments
> > from my python script. I'm having problems trying
> to
> > figure out the best way to do this. 
> 
> Use a format string:
> 
> cmd = "myscript %s %s %s %s"
> os.system(cmd % (p1,p2,p3,p4))
> 
> OR
> 
> cmdstr = cmd % p1,p2,p3,p4
> os.system(cmdstr)
> 
> The second form is easier to debug...
> 
> > to use popen3 to keep track of stdin, stdout, and
> err,
> 
> Ok, substitute popen3 for system().
> Or better still use the subprocess module and 
> the Popen class.
> 
> See the OS topic in my tutor for more on the
> subprocess 
> module.
> 
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> 
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with erros using subprocess module

2006-05-09 Thread Jerome Jabson
Hi,

I've been trying to write a wrapper around some shells
scripts using the subprocess module. But I'm getting
errors I quite don't know how to debug. I've only been
writing Python for a few months and starting processes
are new to me. Here's my code:

import os
import re
import subprocess


 Definition 


def proc(cmd_in):
   cmd = cmd_in
   os.chdir("/home/qauser/jerome")
   outFile = os.path.join(os.curdir, "output.log")
   outptr = file(outFile, "w")
   errFile = os.path.join(os.curdir, "error.log")
   errptr = file(errFile, "w")
   retval = subprocess.call(cmd, 0, None, None,
outptr, errptr)
   errptr.close()
   outptr.close()
   if not retval == 0:
  errptr = file(errFile, "r")
  errData = errptr.read()
  errptr.close()
  raise Exception("Error executing command: " +
repr(errData))

def setup():
   print "=== Starting Setup ==="
   proc("/home/qauser/jerome/qaSetup.sh")
   print "=== Setup  Complete ==="

def run_junit():
   file_in =
open("/home/qauser/automation/testdata/junit_master_file",
"r")
   match = re.compile('#+')
   work_list = []
   for line in file_in:
  work = line
  work = work.rstrip('\n')
  if match.search(work):
 found = False
  else:
 found = True
  if found == True:
 work_list = work.split(',')
 arg1 = work_list[0]
 arg2 = work_list[1]
 arg3 = work_list[2]
 arg4 = work_list[3]
 cmd = "/home/qauser/jerome/qaRun.sh %s %s %s
%s"
 cmdstr = cmd % (arg1,arg2,arg3,arg4)
 print "=== Starting JUnit Run ==="
 proc(cmdstr)
 print "=== JUnit Run Complete ==="



##  Main  ##


setup()
run_junit()

The setup() def seems to work great, but the
run_junit() seems to have problems. I believe due to
the params I'm passing. Here are the errors I'm
getting:

[EMAIL PROTECTED] automation]$ ./runJunit.py 
=== Starting JUnit Run ===
Traceback (most recent call last):
  File "/home/qauser/automation/runJunit.py", line 63,
in ?
run_junit()
  File "/home/qauser/automation/runJunit.py", line 54,
in run_junit
proc(cmdstr)
  File "/home/qauser/automation/runJunit.py", line 18,
in proc
retval = subprocess.call(cmd, 0, None, None,
outptr, errptr)
  File
"/opt/python-2.4.3/lib/python2.4/subprocess.py", line
412, in call
return Popen(*args, **kwargs).wait()
  File
"/opt/python-2.4.3/lib/python2.4/subprocess.py", line
542, in __init__
errread, errwrite)
  File
"/opt/python-2.4.3/lib/python2.4/subprocess.py", line
975, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

I can't seem to figure out what file or directory is
missing. 

Your help is greatly appreciated,
Jerome

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] help with erros using subprocess module

2006-05-10 Thread Jerome Jabson
Hi Bo,

Jerome Jabson 写道:
> Hi,
>
> I've been trying to write a wrapper around some
shells
> scripts using the subprocess module. But I'm getting
> errors I quite don't know how to debug. I've only
been
> writing Python for a few months and starting
processes
> are new to me. Here's my code:
>
> import os
> import re
> import subprocess
>
> 
>  Definition 
> 
>
> def proc(cmd_in):
>cmd = cmd_in
>os.chdir("/home/qauser/jerome")
>outFile = os.path.join(os.curdir, "output.log")
>outptr = file(outFile, "w")
>errFile = os.path.join(os.curdir, "error.log")
>errptr = file(errFile, "w")
>retval = subprocess.call(cmd, 0, None, None,
> outptr, errptr)
>errptr.close()
>outptr.close()
>if not retval == 0:
>   errptr = file(errFile, "r")
>   errData = errptr.read()
>   errptr.close()
>   raise Exception("Error executing command: " +
> repr(errData))
>
> def setup():
>print "=== Starting Setup ==="
>proc("/home/qauser/jerome/qaSetup.sh")
>print "=== Setup  Complete ==="
>
> def run_junit():
>file_in =
>
open("/home/qauser/automation/testdata/junit_master_file",
> "r")
>   
Could you put the contents in the junit_master_file
here ?
Are the first field in every line where the cmd name
locates contain the 
full path to the
command ?

Here is the content of the junit_master_file:


# This is the input file parameter for the JUnit test.
Here is how #
# the prameters are used: 
#
# 
#
#  qaRun.sh
#
# 
#
#  There are 3 different test_type:   
#
#  netmap_perf
#
#  threatmap_perf 
#
#  netmap_func
#

netmap_func,/home/qauser/junit/static_nat/simple_nat,10.1.1.0,172.16.200.0

Basically once I have this running there will be 100s
of lines that map to the comma separated line. From
the comments you can tell what the qaRun.sh script is
looking for as far as params, and that's what this
file is for.

Thanks for your help!
Jerome

>match = re.compile('#+')
>work_list = []
>for line in file_in:
>   work = line
>   work = work.rstrip('\n')
>   if match.search(work):
>  found = False
>   else:
>  found = True
>   if found == True:
>  work_list = work.split(',')
>  arg1 = work_list[0]
>  arg2 = work_list[1]
>  arg3 = work_list[2]
>  arg4 = work_list[3]
>  cmd = "/home/qauser/jerome/qaRun.sh %s %s
%s
> %s"
>  cmdstr = cmd % (arg1,arg2,arg3,arg4)
>  print "=== Starting JUnit Run ==="
>  proc(cmdstr)
>  print "=== JUnit Run Complete ==="
>
>
> 
> ##  Main  ##
> 
>
> setup()
> run_junit()
>
> The setup() def seems to work great, but the
> run_junit() seems to have problems. I believe due to
> the params I'm passing. Here are the errors I'm
> getting:
>
> [qauser at gary automation]$ ./runJunit.py 
> === Starting JUnit Run ===
> Traceback (most recent call last):
>   File "/home/qauser/automation/runJunit.py", line
63,
> in ?
> run_junit()
>   File "/home/qauser/automation/runJunit.py", line
54,
> in run_junit
> proc(cmdstr)
>   File "/home/qauser/automation/runJunit.py", line
18,
> in proc
> retval = subprocess.call(cmd, 0, None, None,
> outptr, errptr)
>   File
> "/opt/python-2.4.3/lib/python2.4/subprocess.py",
line
> 412, in call
> return Popen(*args, **kwargs).wait()
>   File
> "/opt/python-2.4.3/lib/python2.4/subprocess.py",
line
> 542, in __init__
> errread, errwrite)
>   File
> "/opt/python-2.4.3/lib/python2.4/subprocess.py",
line
> 975, in _execute_child
> raise child_exception
> OSError: [Errno 2] No such file or directory
>
> I can't seem to figure out what file or directory is
> missing. 
>
> Your help is greatly appreciated,
> Jerome

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with erros using subprocess module

2006-05-11 Thread Jerome Jabson
Hi Bo,

Thank you very much for all your help!! But I'm having
some problems with the line of code you recommended:

   if isinstance(cmd, types.StringTypes):
  cmd = cmd.split(' ')

I'm getting the following error:

NameError: global name 'types' is not defined

As you instructed, I put in the beginning of the
functon proc:

def proc(cmd_in):
   cmd = cmd_in
   if isinstance(cmd, types.StringTypes):
  cmd = cmd.split(' ')
   os.chdir("/home/qauser/jerome")
   outFile = os.path.join(os.curdir, "output.log")
   outptr = file(outFile, "w")
   errFile = os.path.join(os.curdir, "error.log")
   errptr = file(errFile, "w")
   retval = subprocess.call(cmd, 0, None, None,
outptr, errptr)
   errptr.close()
   outptr.close()
   if not retval == 0:
  errptr = file(errFile, "r")
  errData = errptr.read()
  errptr.close()
  raise Exception("Error executing command: " +
repr(errData))

Am I missing some module you are referencing with
"types"?

Thanks again,
Jerome


>I think I find what is wrong here !
>
>First , I run the program in windows , and it execute
>well without any 
>error and execption .
>Second , I run it in Linux and the error you provide
>occured .
>
>And then I debug the script in Linux , and found that
>below :
>The subprocess.call method pass its arguments to the
>Popen object and 
>then the later fork the process , in the python
>library reference I
>found that :
>
>args should be a string, or a sequence of program
>arguments. The 
>program 
>to execute is normally the first item in the args
>sequence or string, 
>but can be explicitly set by using the executable
>argument.
>
>
>If the first argument is a string , then the Popen
>class assume it is 
>only the command without arguments , and if the first
>argument is a 
>sequence , the Popen class
>assume that the first elment in the sequence is a
>command name and 
>others else are arguments for the command .
>So , if you want to pass arguments to your shell ,
you >must pass the 
>call method a sequence instead of a command .
>I add a clause in the begin of the function proc :
>
>if isinstance( cmd , types.StringTypes):
>cmd = cmd.split(' ')
>
>and everything is OK !
>
>And I also there is a function called
>list2cmdline(args) , which will 
>be 
>called in the windows version of Popen , and it is
>this function make 
>the program through in windows with no error .

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Running Java process doesn't return to subprocess.call

2006-05-15 Thread Jerome Jabson
Hello,

I'm running a Java process from my python script using
the subprocess module. But there seems to be some
problem with the return code to subprocess and the
Java program is just hanging waiting for something. If
I run the Java program from shell it terminates
normally and if run the same program through a shell
script it does the same thing. Is there something
different I need to handle when running a Java program
vs other programs? Here's my code:

def proc(cmd_in):
   cmd = cmd_in
   if isinstance(cmd, types.StringTypes):
  cmd = cmd.split(' ')
   print cmd
   os.chdir("/home/qauser/jerome")
   outFile = os.path.join(os.curdir, "output.log")
   outptr = file(outFile, "w")
   errFile = os.path.join(os.curdir, "error.log")
   errptr = file(errFile, "w")
   retval = subprocess.call(cmd, 0, None, None,
outptr, None)
   errptr.close()
   outptr.close()
   if retval != 0:
  errptr = file(errFile, "r")
  errData = errptr.read()
  errptr.close()
  raise Exception("Error executing command: " +
repr(errData))

def setClass():
   print "=== Setting Classpath ==="
   src = "/home/qauser/automation/setClass.sh"
   dst = "/home/qauser/jerome/setClass.sh"
   os.chdir("/home/qauser/jerome")
   shutil.copyfile(src, dst)
   os.chmod(dst, 0777)
   proc("/home/qauser/jerome/setClass.sh")
   classFile = open("/home/qauser/jerome/output.log",
"r")
   CP = classFile.read()
   CP = CP.rstrip('\n')
   print "=== Complete Classpath Setup ==="
   return CP

def run_junit(classpath):
   file_in =
open("/home/qauser/automation/testdata/junit_master_file",
"r")
   match = re.compile('#+')
   work_list = []
   for line in file_in:
  work = line
  work = work.rstrip('\n')
  if match.search(work):
 found = False
  else:
 found = True
  if found == True:
 work_list = work.split(',')
 arg1 = work_list[0]
 arg2 = work_list[1]
 arg3 = work_list[2]
 arg4 = work_list[3]
 os.chdir("/home/qauser/jerome")
 cmd = "java -cp %s
com.redsealsys.srm.server.analysis.NetmapTest %s %s %s
%s"
 cmdstr = cmd %
(classpath,arg1,arg2,arg3,arg4)
 print "=== Starting JUnit Run ==="
 proc(cmdstr)
 print "=== JUnit Run Complete ==="


##  Main  ##


cp = setClass()
run_junit(cp)

The junit_master_file is just a comma separted file:

netmap_func,/home/qauser/junit/static_nat/simple_nat,10.1.1.0,172.16.200.0

which has the prameters that I need to pass into the
Java program. I would appreciate any insight or help
in this matter.

Much Thanks,
Jerome

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor