0

Lets say you have a large file containing LETTER,NUMBER comma-delimited tokens. You want to write a program that reads from standard input and prints out NUMBER+1 for each line. Very trivial program, I understand. However, here is the constraint -- you can only read from this standard in pipe one-time AND you have to start out with programming an empty file.

So for example:

cat FILE.csv | python empty_program.py

This should pop up an interactive session which allows you to write what ever code you want. Since empty_program.py has not called stdin.readline(), the stdin buffer is appropriately in-tact.

Is something like this possible?

One example of something that can sort of do this is the Excel VBA debugger/IDE. It allows you to pause execution -- add new lines to the programs source code and continue exeuction.

user992592
  • 163
  • 5
  • Possible duplicate of http://stackoverflow.com/questions/9178751/use-pdb-set-trace-in-a-script-that-reads-stdin-via-a-pipe – Fabian Dec 27 '12 at 15:07

1 Answers1

0

cat FILE.csv | python empty_program.py Well, python will try to read "empty_program.py", and fail to find anything in it, assuming there is file, and then exit. If the file doesn't exist, you get an error. I tested it [you should have been able to do that as well, doesn't take that much effort - probably a lot less than it took to go to SO and write the question].

So, my next thought was to use an interactive python process, but since you are feeding things through stdin, it won't work - I didn't have a good csv file, so I did "cat somefile.c|python", and that falls over at "int main()" with "invalid syntax". I'm surprised it got as far as that, but I guess that's because #include's are seen as comments.

Most interactive programming languages read from stdin, so you can't really do what you are describing with any of them.

I'm far from sure why you'd want to either. If your first program can produce the relevant program code, why would you not just put it in a file and let python read that file... Rather than jump through hoops? Note that an IDE is not the same as a command line program. I'm pretty sure that if you work hard enough at something, you can write a C program that acesses the Eclipse IDE with Python plugins. But that's really doing things the hard way. Why anyone would WANT to spend that much effort to achieve so little, I don't see.

Sorry, but I don't really see the point of what you are trying to do - I'm sure you have some good idea in there, but I'm sure the implementation details need to be worked on.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • I would like the ability to write a python script that reads through a file, but only does that once. In other words, lets say a script takes a stream from stdin and adds one to every number. so 1\n2\n\3\n would become 2\n\3\n4\n on stdout. Could you build that program "interactively" without having to follow the traditional path of 1) write the program 2) run the program 3) if failed, go back to (1) but instead to be able to interactively change the program in case you wrote += 2 instead of +=1?? Kind of like VBA/Excel's capabilities which I mentioned in the question. – user992592 Jan 14 '13 at 20:59