IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Using regular expressions in jython methods
khoffman
post Sep 14 2004, 11:22 AM
Post #1


Advanced Member
***

Group: Members
Posts: 55
Joined: 17-June 04
Member No.: 296



I would like to use python/jython regular expressions in a method.
However, when I try to import re I get an errror
ImportError: no module named re

Here's a toy example:
[CODE]
import re

def parseXMLElement(input):
pre = re.search("<*?>", input)
print pre

Am I mistaken about the module to import? All the online examples of python regex I could find used the re module

What I am ultimately trying to do is strip the XML tags off an element so that I have the tag and the value in two different strings.

Thanks.
Go to the top of the page
 
+Quote Post
jhendrick
post Sep 14 2004, 12:05 PM
Post #2


Expert
*****

Group: Staff
Posts: 147
Joined: 26-March 04
From: Monrovia, CA
Member No.: 203



Keith,

Do you have your scripting preferences setup (Preferences -> Scripting)? In particular do you have your "Python Home" and "Python Path" variables setup? To use these jython modules you will need to set this up. If you haven't done so, you can download and install jython which contains all the libraries you need. You can download Jython 2.1 from here http://www.jython.org/download.html and follow the directions to install it. Once installed you can set the following variables in your SOAPtest preferences:

Python Home: $JythonInstallDirectory (something like C:\jython-2.1)
Python Path: $JythonInstallDirectory\Lib (something like C:\jython-2.0\Lib)

Also note than you can always use Java Objects to do things, since this is Java Enabled Python. For example Java has regular expression Classes. Here is a quick example of regular expressions using Java Classes:

CODE
from java.util.regex import *
from java.lang import *

def REexample(input):
  p = Pattern.compile("a*b");
  m = p.matcher(String("aaaaab"));
  b = m.matches();
  print b


In this example we are compiling a regular expression (a*cool.gif and using the string ("aaaaab") as input. Then the call to m.matches() will return true since we can match the regular expression a*b in "aaaaab". So this function will output "1" (true).

Josh
Go to the top of the page
 
+Quote Post
khoffman
post Sep 14 2004, 12:30 PM
Post #3


Advanced Member
***

Group: Members
Posts: 55
Joined: 17-June 04
Member No.: 296



Josh,

I am a bit confused. We don't have an independent installation of jython or python.
My python/jython scripts work except for this one (which may be the only one using a python library).

So SOAPtest has some kind of basic jython built-in but not the libraries?
In order to use python/jython libraries, we need a separate installation?

Java regex is an option but I'd like to be able to use the jython/python facilities.

BTW, we use the linux version of SOAPtest so the windows install directions aren't too useful.
I'm sure I can translate. Just an FYI.

Thanks!
Go to the top of the page
 
+Quote Post
khoffman
post Sep 14 2004, 01:20 PM
Post #4


Advanced Member
***

Group: Members
Posts: 55
Joined: 17-June 04
Member No.: 296



After installing jython-2.1 in my home directory and pointing SOAPtest at it in the preferences menu via:
Python Home: /home/khoffman/jython-2.1
Python Path: /home/khoffman/jython-2.1/lib

I no longer get the import error.

Instead I get a traceback error about sre.py expecting a string but receiving a webtool.packet.TextUsable instead
which is interesting because when I run the same script in the jython interpreter under the linux environment where SOAPtest is running, it executes fine and produces a memory address as output (not very useful but I'm getting started on this approach).

This is a difference between an independent jython environment and SOAPtest when it was my understanding that there were not differences.
Also, is it possible to remove the jython.jar that comes with SOAPtest and use another locally installed copy? (ie
ln -s /home/khoffman/jython-2.1/jython.jar /opt/soaptest/2.6/jython.jar

Thanks.
Go to the top of the page
 
+Quote Post
jhendrick
post Sep 14 2004, 01:53 PM
Post #5


Expert
*****

Group: Staff
Posts: 147
Joined: 26-March 04
From: Monrovia, CA
Member No.: 203



It looks like the error that you need to get a string from the input value that is passed to your method tool. Something like this:

CODE
from soaptest.api import *
import re

def parseXMLElement(input):
pre = re.search("<*?>", SOAPUtil.getStringFromObject(input))
print pre


You should not need to use another Jython.jar file, and since SOAPtest has not been tested with this other jar file it is not recommened. Although, it should work correctly in theory if the API's are compatible.

SOAPtest comes with basic support for Jython, but if you would like to use python modules such as "re"then you would need to install Jython.

Josh
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 20th August 2014 - 09:00 AM