how to include input values given to subprocess.run input in stdout txt file

21 Views Asked by At

A feature that I want my automatic graders to have is to write the output generated by a student's submission to a txt file. The generated txt file will then be compared to another txt file, that has the expected output, and the comparison will determine what the student's end grade is. I am able to successfully generate the student txt files, but I don't like how the txt file looks. For all the input values that I provide to the input argument of subprocess.run, the values themselves are not written and all of the prompts that ask for the values are written on the same line. An example of the resulting format of the generated txt file, for the following test cases, is...

test case 1: ["103\n", "-3\n", "85\n"]

test case 2: ["-10\n", "-1\n", "105\n" ,"98\n"]

This program calculates the letter grade from the integer quiz grade
Enter quiz grade: This program calculates the letter grade from the integer quiz grade
Enter quiz grade: This program calculates the letter grade from the integer quiz grade
Enter quiz grade: Your quiz letter grade is BThis program calculates the letter grade from the integer quiz grade
Enter quiz grade: This program calculates the letter grade from the integer quiz grade
Enter quiz grade: This program calculates the letter grade from the integer quiz grade
Enter quiz grade: This program calculates the letter grade from the integer quiz grade
Enter quiz grade: Your quiz letter grade is A

This resulting format is hard read. Is there an external feature or special argument that I can include within subprocess.run that will generate the exact output you would see if you ran the actual student submission? Desired output:

This program calculates the letter grade from the integer quiz grade
Enter quiz grade: 103
This program calculates the letter grade from the integer quiz grade
Enter quiz grade: -3
This program calculates the letter grade from the integer quiz grade
Enter quiz grade: 85
Your quiz letter grade is B
This program calculates the letter grade from the integer quiz grade
Enter quiz grade: -10
This program calculates the letter grade from the integer quiz grade
Enter quiz grade: -1
This program calculates the letter grade from the integer quiz grade
Enter quiz grade: 105
This program calculates the letter grade from the integer quiz grade
Enter quiz grade: 98
Your quiz letter grade is A

Code:

input_value_lists = [["103\n", "-3\n", "85\n"], 
                     ["-10\n", "-1\n", "105\n","98\n"]]

with open(student_txt_file, "w") as txt_file:
    for list in input_value_lists:
        for input_value in list:
            output = subprocess.run(["java", hw_file], input=input_value, capture_output=True, text=True)
            txt_file.write(output.stdout)

I originally thought that I could directly write the input value into the txt file, but that just leads having the input value before the generated output. I've also been looking through the subprocess documentation, but I'm either misunderstanding it (highly likely) or there really isn't any argument that will actually include the input values.

1

There are 1 best solutions below

0
Tim Roberts On

Your terminology is a bit off. You say "the exact output", but "103" and "-3" are not outputs. They are inputs. Yes, that's what you'd see on the terminal, but that's not the same thing.

There's no way to do what you want from Python, because the inputs are interspersed. You could change the Java code to echo the inputs. Your Java program can find out whether stdout is connected to a terminal or not and change its behavior, or even pass another parameter that tells it it's not a live terminal.