#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0

    Exclamation using popen to run java in python...HELP


    if I have a java program, which takes in a standard input or text file at the command line, process the input, then returns a string, how do i use the program in python and compare the output with a python program of the same functionality (using popen)??

    eg. my java program can be run from the command line under unix as follows:
    test1.java < sometext.txt
    (some output returned)
    and the python program can be run from unix as well by typing:
    python test1.py sometext.txt
    (some output returned)

    and i need to know how to run both of these under a python module to compare the results. thx~
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    It all depends on the names of the programs and the commands used to invoke them but you can use popens read() method to collect the output from running these scripts. It would then be as simple as comparing the two strings. (see difflib below.)

    You could also dump the output of both of these programs info a file (like your currently doing) and use the difflib or filecmp modules to do the comparisons.

    http://www.python.org/doc/2.3.4/lib/module-difflib.html
    http://www.python.org/doc/2.3.4/lib/module-filecmp.html

    If you need more info let me know,

    Hope this helps,

    Mark.
    Last edited by netytan; October 27th, 2004 at 11:02 AM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0

    Lightbulb


    Thanks netylan, i think my actual problem is that i don't know the code for using popen to collect the output from both programs

    if both my python and my java file are named test1.py and test1.java, would my module be something like:

    from os import popen
    import sys
    import test1.py

    def compareJavaPython(textfile):
    inputjava = popen("javac test1.class")
    resultjava = inputjava > textfile

    inputPython = popen("test1.py")
    resultPython = inputPython textfile
    return (resultjava == resultPython)

    if __name__ == "__main__":
    print compareJavaPython(textfile)


    thanks for your help~
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Personally in this case I would use:

    os.system("test1.java < sometext.txt > log1")
    os.system("python test1.py < sometext.txt > log2")

    Then read in both log1 and log2:
    data1 = file("log1","r").readlines()
    data2 = file("log2","r").readlines()

    And then you can compare in whatever way you like.

    grim
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Assuming test1.java and test1.py are the two programs who's output that you want to compare, you might lay our your module something like this:

    Code:
    pseudo module.py
    import sys,...
    
    def compareProgramOutput(fileName1, fileName2):
        ...
        start the programs and save the output from the two
        programs.
        ...
        read the output from the files and compare the
        it.
        ...
        return whatever
    You would then import this into your main program to use the compareProgramOutput() function. This might be overkill depending on the size of the script but you should get the idea.

    As for the popen() function, it works in pretty much the same way as a file() object, with all the same methods. See the os module:

    http://www.python.org/doc/2.3.4/lib/os-newstreams.html

    Anyway, I would go with Grims idea since it makes things as simple as possible; starting the programs from the command line and reading the output from the output files for comparison.

    Hope this helps,

    Mark.
    Last edited by netytan; October 27th, 2004 at 04:58 PM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo