Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Using regular expressions in jython methods

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in SOAtest
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.

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    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* 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
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    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!
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    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.
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    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