Python strip() functions

Help
ToNNiX
2013-01-30
2013-06-12
  • ToNNiX
    ToNNiX
    2013-01-30

    According to the Python documentation, the functions strip(), lstrip(), and rstrip() are supposed to be able to take input as a list of characters to be stripped from a string. However, when using any of these functions in a STAX <script> tag, adding such arguments to the function will always result in the follow type of error:

    com.ibm.staf.service.stax.STAXPythonEvaluationException:
    Traceback (innermost last):
      File "<pyExec string>", line XXX, in ?
    TypeError: strip(): expected 0 args; got 1

    I've noticed this for a long time, is there a reason for this or is it a bug?

     
  • Sharon Lucas
    Sharon Lucas
    2013-01-30

    Note that STAX uses Jython not Python to evaluate code in a <script> element within a STAX job. See "Appendix F: Jython and CPython Differences" in the STAX V3.5.4 User's Guide at http://staf.sourceforge.net/current/STAX/staxug.html#Header_CPythonDiffs which says:

    "Although in most cases Jython behavior is identical to the C-language implementation of Python (CPython), there are still cases where the two implementations differ. If you are already a CPython programmer, or are hoping to use CPython code under Jython, you need to be aware of these differences. Also, there is a time lag between a new CPython release and the corresponding Jython release. STAX uses Jython 2.5.2 which implements the same set of language features as CPython 2.5. Jython 2.5.2 cannot execute Python code that uses functions that were provided in later versions of Python, such as Python 2.6. "

    What version of STAX are you using and what version of Jython is STAX using?

    The latest version of STAX is V3.5.4 and it embeds and uses Jython 2.5.2.  For example, here's how you can see the STAX version and the version of Jython that STAX is using:

    C:\&gt;staf local stax version
    Response


    3.5.3

    C:\&gt;staf local stax version jython
    Response


    2.5.2-staf-v1

    STAX V3.5.0 and later use Jython 2.5.2.  Earlier versions of STAX use Jython 2.1.  So, my guess is that you're using a STAX version that is earlier then V3.5.0 which means STAX is using Jython 2.1 to execute Python code within a <script> element.  If you look at the documentation for Python 2.1 (as Jython 2.1 is based on Python 2.1) at http://docs.python.org/release/2.1/lib/module-string.html, you'll see that the strip functions do not support a chars parameter:

    strip(s)
    Return a copy of s without leading or trailing whitespace.

    The docs parameter has added to the strip functions in Python 2.2.3.  You can see this by looking at the documentation for Python 2.5 for example at http://docs.python.org/release/2.5/lib/node42.html.  It defines the strip function as follows:

    strip( s)

    Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on. Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

    So, using STAX V3.5.4, I can pass the chars argument to the Python strip() function since it uses Jython 2.5.2 which is based on Python 2.5.2.  For example, the following STAX job runs fine using STAX V3.5.4:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE stax SYSTEM "stax.dtd">
    <stax>
    <defaultcall function="main"/>
    <function name="main">
      <sequence>
      
        <script>
          str = "     Here is some text    "
          # Strip leading and trailing spaces from a string
          str1 = str.strip(' ')
          # Strip leading and trailing whitespace from a string
          # This is the default if don't pass the chars parameter
          str2 = str.strip()
          # Strip leading spaces from a string
          str3 = str.lstrip(' ')
          # Strip trailing spaces from a string
          str4 = str.rstrip(' ')
        </script>
        <log message="1">'str1 length: %s  str1: "%s"' % (len(str1), str1)</log>
        <log message="1">'str2 length: %s  str2: "%s"' % (len(str2), str2)</log>
        <log message="1">'str3 length: %s  str3: "%s"' % (len(str3), str3)</log>
        <log message="1">'str4 length: %s  str4: "%s"' % (len(str4), str4)</log>
      </sequence>
    </function>
    </stax>
    
     
  • ToNNiX
    ToNNiX
    2013-01-30

    OK thanks for the explanation. I am currently on STAX 3.4.4 and have been using re.sub() to strip out characters for now since other methods to do so would result in extraneous lines of code (i.e. multiple replace() calls).