Capturing stdout from a class method

Discussion in 'Python' started by Falcolas, Jun 27, 2007.

  1. Falcolas

    Falcolas Guest

    I have a rather strange situation, and I'm not sure my brief
    experience of Python will let me handle it properly.

    The situation is this: I have a Java class "X" which I need to call in
    a Jython script. The output of "X" is sent to stdout using the java
    call System.out. I need to capture this output, and preform tests on
    it in the Jython script.

    My first pass at a design involves two jython scripts. One (we'll call
    it "Y") whose sole function is to instantiate and call "X", and die.
    The second script will call "Y" using a method which captures stdout
    to a pipe. The second script would then read the stdout from the pipe
    and act accordingly.

    Can anybody suggest a better way to handle this? The Java class, "X",
    can not be modified for this test.
    Falcolas, Jun 27, 2007
    #1
    1. Advertising

  2. Falcolas wrote:
    > I have a rather strange situation, and I'm not sure my brief
    > experience of Python will let me handle it properly.
    >
    > The situation is this: I have a Java class "X" which I need to call in
    > a Jython script. The output of "X" is sent to stdout using the java
    > call System.out. I need to capture this output, and preform tests on
    > it in the Jython script.
    >
    > My first pass at a design involves two jython scripts. One (we'll call
    > it "Y") whose sole function is to instantiate and call "X", and die.
    > The second script will call "Y" using a method which captures stdout
    > to a pipe. The second script would then read the stdout from the pipe
    > and act accordingly.
    >
    > Can anybody suggest a better way to handle this? The Java class, "X",
    > can not be modified for this test.


    How about subprocess.Popen ? I'm thinking of something like this:

    import subprocess

    X_proc = subprocess.Popen(['java', 'Xstarter'])
    for line in Xproc.stdout:
    # do funky stuff
    pass




    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.6 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFGgr8lJpinDvQhQ0sRAjyMAJwJyAxe8odd8z7rDSt2T60G0j1ELwCeKCL6
    LstzYFWFalmBPrTKfUB6nFI=
    =/N6M
    -----END PGP SIGNATURE-----
    Thomas Jollans, Jun 27, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moosebumps

    Capturing stdout incrementally

    Moosebumps, Apr 3, 2004, in forum: Python
    Replies:
    5
    Views:
    517
    David Bolen
    Apr 7, 2004
  2. Replies:
    2
    Views:
    346
  3. Luigi
    Replies:
    5
    Views:
    404
    Donn Cave
    Apr 3, 2006
  4. Fuzzyman
    Replies:
    3
    Views:
    637
    Gabriel Genellina
    Jan 7, 2007
  5. Nigel Wade
    Replies:
    8
    Views:
    1,090
    Lawrence D'Oliveiro
    Mar 18, 2011
Loading...

Share This Page