Calling specific python methods from STAX xml

  • Mayura

    Mayura - 2012-10-08


    We have a big python file written. Is it possible to call specific method from that .py file through STAX xml. We usually call the complete python file using
    <command mode="'shell'">'python /usr/local/Project/ %s' % (STAXJobID)</command>

    But now we want to know if we can call specific method/function using STAX. Please help


  • Arun Singh

    Arun Singh - 2012-10-08

    you can create a function in STAX itself.. and call that function :-)

    <!- SNIP
    <function name = "Run_script" scope="local">
    <function-required-arg name="command"/>
                <function-required-arg name="testfile"/>
                <function-optional-arg name="staxMachine" default="'local'"/>           
                    commandString = '%s %s %s %s %s %s %s %s %s %s ' % (command, testfile, staxMachine)
                <call function = "'stafmsg'">{
                    'msg': "Shell command string - %s" % commandString
    <command mode="'shell'">commandString</command>
    <stderr mode="'stdout'"></stderr>
    exeResult = STAFResult
    resultList =
    staxJobId = STAXJobID
    <message>'STAF execution results - %s' % resultList</message>
    <message>'STAX JOB ID - %s' % STAXJobID</message>
    <if expr="RC != 0">
    <message>'Error: RC=%s, STAXResult=%s' % (RC, STAXResult)</message>
    <message>'Process RC was 0. STAXResult=%s STAFResult=%s' % (STAXResult, STAFResult)</message>


  • Arun Singh

    Arun Singh - 2012-10-08

    oh.. I gave wrong answer… I have given you the solution to call multiple python script… i

    To call specific method .. You can import your python file in script tag and and call the specific method .

  • Mayura

    Mayura - 2012-10-08

    Thank you for the quick response!

    Actually our xml requires to call one specific method of the python file multiple times and writing it in xml will increase the size considerably as it is already in huge size. That is the reason we were wondering if there is any way we could call it in xml

  • Mayura

    Mayura - 2012-10-08

    oh got it. let us try it. Thank You!! By any chance do you have any sample xml which has imported the python file and calling the methods?

  • Arun Singh

    Arun Singh - 2012-10-08

    I don't have at the moment .. Will create one and provide you by tom :-)

  • Sharon Lucas

    Sharon Lucas - 2012-10-08

    Here's an example of a STAX job that imports a .py file named located on the STAX service machine and runs functions in it:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE stax SYSTEM "stax.dtd">
       <defaultcall function="Main"/>
       <function name="Main">
             # Directory containing Python file(s) (e.g. to be imported
             myPythonDir = 'C:\stax'
             import sys
             pythonpath = sys.path
             # Append myPythonDir to sys.path
             # (if not already present)
             if myPythonDir not in pythonpath:
             # Import the MyTests module so that you can call functions from it
             import MyTests
           <log message="1">MyTests.test1()</log>  
           <log message="1">MyTests.test2()</log>
           <log message="1">MyTests.test3()</log>

    Here's the content of C:\stax\

    def test1():
        return 'Ran test1'
    def test2():
        return 'Ran test2'
    def test3():
        return 'Ran test3'
    def main():
        return 'Ran main'

    The STAF/STAX FAQ contains another example of importing a Python file within a STAX job as well how to reload Python modules after making changes to them so that the changes are picked up in the STAX job.  See section "4.1.6 Why aren't changes to imported Python modules picked up in my STAX job?" in the STAF/STAX FAQ at

  • Mayura

    Mayura - 2012-10-12

    Thank You for your help!

    One quick question..Is there any way to call the python methods in a process element? As we have to capture the RC value also as per requirement of our test case. Or any other way in which we can capture the RC for that particular method.

  • Sharon Lucas

    Sharon Lucas - 2012-10-12

    Yes, your Python methods can return a value (e.g. return 0) and it will be returned when you call the Python method so you can then access it.  For example:

      pythonRC = MyTests.test1()

  • Mayura

    Mayura - 2012-10-17

    <command mode="'shell'">'python -c "import TestCase2; TestCase2.obj.eR_I2()" %s %s %s %s' % (STAXJobID,Configure.rID3,Configure.Interface0,Configure.Interface1)</command>

    With the help of above statement we are able to call a particular method by   importing that partcular file.But we are facing issues  while passing a parameter with the help of above command for example:
    <command mode="'shell'">'python -c "import TestCase2; TestCase2.obj.eR_I2("")" %s %s %s %s' %  (STAXJobID,Configure.rID3,Configure.Interface0,Configure.Interface1)</command> as by doing stax job is throwing a error. Could you please suggest a way through which we can pass parameters through our methods.

    Best Regards,

  • Sharon Lucas

    Sharon Lucas - 2012-10-17

    It would be helpful next time if you post the error that you are getting.  But, it would appear that you are getting an Python Syntax Error because you tried to enclose double quotes within double quotes in your command value.  In order to do this, Python requires that you escape the enclosed double quotes with a backslash (e.g. \").  For example:

    <command mode="'shell'"> 'python -c "import TestCase2; TestCase2.obj.eR_I2(\"\")" %s %s %s %s' %  STAXJobID,Configure.rID3,Configure.Interface0,Configure.Interface1)</command>

  • Mayura

    Mayura - 2012-10-18

    20121018-12:02:09 TestStep 1 failed: Python Script RC=1, STAFResult=None, STAXResult=
    []            Above error is Coming after using backlash.   

  • Sharon Lucas

    Sharon Lucas - 2012-10-22

    I'm not sure why you specified a forward slash (e.g.  / when I said you can escape an inner quote by using a back slash (e.g, \").  It's also helpful to post both your stax code and the error message.

    I think it will be cleaner if you simply assign a Python variable containing the ip address and pass that Python variable to the Python function (as then you won't need to escape any quotes).  For example:

           <script>ipAddr = ''</script>

             <command mode="'shell'">'python -c "import MyTests; MyTests.test1(ipAddr)"'</command>


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks