-1

I am playing with some programming challenges that will check the submission by:

python my_submission < in.txt > out.txt

When I try and make my submission, I want to read some cases/numbers/whatever from in.txt to see what is happening. Currently I am doing that by:

import sys
file = open('in.txt') 
sys.stdin = file  

for line in sys.stdin:
     case1 = line.split()
     some_function(case1)

So when I run my python program (hit cmd+B) in Sublime text, I can see whether I manage to read the input correctly, process one test case correctly, etc.... Then I just commend out the 2nd and 3rd line when my program should be submitted to the submission judge.

I was just wondering: is this the "preffered workflow" for dealing with this? Do pro programmers write some kind of unit test template function to do this?

luffe
  • 1,588
  • 3
  • 21
  • 32

1 Answers1

0

The preferred workflow is to let the shell doing the redirection so you don't have to change the program code all the time.

But your IDE (sublime text) doesn't allow you to specify such arguments, so it limits your options.

Solutions/workarounds:

  1. Start the program from a shell. Which means you need to switch between the terminal window and sublime all the time.
  2. Write a second program which runs the first and which sets up the input redirection. This way, you just need to switch tabs in sublime.
  3. Instead of reading from stdin directly, use the fileinput module. See How do you read from stdin in Python? This will allow you to write proper unit tests for your code. You can then use the Python Unittest Helper plugin for Sublime.
Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820