Reading stdin in Spyder - python-3.x

I have some programming assignments which read input data from stdin like this:
text = sys.stdin.readline().strip()
I am using Spyder as my development environment, and am not able to find a way to give the stdin. So, I added a -infile option to the code, and am reading from the file.
However, when submitting, I need to revert back to reading the stdin. How can I read stdin in the first place? I know how to do this from linux command line, but for now have to use windows.

(Spyder maintainer here) The code you posted only works to read text when you run it in a system terminal (i.e. bash or cmd.exe).
It doesn't work in Spyder because its terminals are implemented in a different technology (PyQt vs. readline).

Related

How to navigate from running python script in ubuntu terminal?

I am currently running a python3 script in Ubuntu server 18.04. When i type new commands into the command line it just prints the commands. My terminal window looks like this:
mitch#server:`$ cd /home/mitch/folder
mitch#server:`/folder$ python3 main.py
file running ...
text i input just shows like this
I need to keep the script running and run other commands, how do i navigate back to:
mitch#server:`
I'm new to servers/Ubuntu/commands so this may seem trivial! Thank you
So you can't "navigate" back to that, since you're technically already there, you're just running a script in your shell which is occupying your shell - think of it like you opened a program in full screen.
But you have a few options:
The most basic is to run the script in the 'background' this is a simple as adding a & to the end of your command (note that it will still send any message from the script into your terminal - if your script is programmed to send messages that is).
Another option is to use a terminal multiplex like which lets you have multiple terminals open, as well as split screen terminals and many other features. One of the more popular multiplexers is called tmux, just keep in mind that it does have a bit of a learning curve to it, but is extremely useful once you learn it.

How do I use output from SPARC solver as input to a python file?

My question: I need to save the output from a SPARC solver (which is currently appearing as text in the terminal) as a variable in my Python code. How can I do this?
Quick note: SPARC is a solver used for ASP (answer-set-programming) files - just mentioning this so that people don't get confused and think I am referring to asp.net.
I am running a Python file and an ASP file (in query mode) simultaneously, in the same terminal, using the command python pythonfile.py | java -jar sparc.jar aspfile.sp. Output from the python file in the form of sys.stdout.write() is being redirected as the input to the SPARC solver; i.e. the text I output becomes the query that is solved using my ASP code. This is working, and generating the output I want, but I can't figure out how to use that output in my Python code.
This is a follow-up to another question I have asked, found here. In that question I was trying to find out how to run an ASP file from my C++ code. I went with option 2 in the answer I was given, and am using redirected stdout with the two files running as separate processes. Please note one major change since the original question: I am now using Python instead of C++.
Further details if required: My Python version is 2.7 and my operating system is Ubuntu 14.04. I don't think it's relevant but, in case it is, you should know that my Python code is also being used to control a Gazebo Turtlebot simulation, and I am using ROS Indigo to run that simulation. I won't post my code unless someone requests it, as I just want an idea of what method I could use (I can't find anything that works on the internet), rather than needing my code debugged / assistance writing it. However I will post below an example of what is output to the terminal when my code runs, as this is the information I am trying to 'capture'.
SPARC V2.52
program translated
?- yes
?- no
It is the answers 'yes' and 'no' that I want to save as variables in my Python file.
SOLUTION:
For anyone wanting to do the same thing, I followed the answer provided by CaptainTrunky.
First I run the command python pythonfile.py | java -jar sparc.jar aspfile.sp > sparc.out, saving the SPARC output to the text file sparc.out.
Then I run python outputParser.py to run a script that prints the contents of the text file, allowing me to check that I'm manipulating the data correctly. The script is very simple:
lines = [line.strip('?- ') for line in open('sparc.out')]
lines = [line.strip('\n') for line in lines]
print lines
You can use python to read from sys.stdin with a script similar to the following (filter.py):
import sys
for line in sys.stdin.readlines():
if line.startswith('?- '):
print line.strip()
Then invoke your pipeline like this:
python pythonfile.py | java -jar sparc.jar aspfile.sp | python filter.py
I would suggest you do dump SPARK output to text file and then to parse it with you tool.
Write a shell script that does it for you:
python pythonfile.py | java -jar sparc.jar aspfile.sp > spark.out
python parse_out.py spark.out

Launch interactive shell programmatically

I need to do a interactive shell programmatically (in Node.js, but any language will do).
The most important is that I want to start a REPL of any language (node, ruby, erlang etc.) and be able to get autocompletion
pri\t
which I want to get output in stderr (for instance)
print() println() printf()
And because it's in language REPL compgen cannot be used.
I've tried many ways but it seems that it fails because shell can't be interactive while opened programmatically.
Launchin /bin/sh -i just yeilds errors like:
bash: cannot set terminal process group (XXXX): Inappropriate ioctl for device
I've heard that I can't start interactive shell without the terminal, but when doing SSH autocompletion does work which means it can be emulated in some way.
I'd appreciate Your help. I can't continue my work without finding a solution to this.
You can emulate a terminal using a pseudoterminal. This is how ssh does it. There's a lot to using a pseudoterminal though and the SO answer box isn't big enough for all of it. So check the man page for pty(7).

Prolog Programming in Ubuntu

I have an interest in playing and fuxing with prolog, I have installed the swi-prolog and added the repository, just in case anyone is interested on which one commands I used:
% sudo apt-add-repository ppa:swi-prolog/stable
% sudo apt-get update
% sudo apt-get install swi-prolog
How do I actually begin to write prolog codes on my linux machine? for my regular programming I use VIM to write/edit/debug and terminal to compile. Can I use vim to write prolog? How do i compile or use the prolog interpreter(i think that is what it is called)?
Yes, you can use any text editor, incl. VIM. Once you have written a Prolog source file, say, file.pl, you can load it into SWI-Prolog like so:
swipl -s file.pl
This will compile your file and take you to an interactive shell where you can then ask queries against the definitions in your file.
If you want to use your Prolog program in batch mode, you can use:
swipl -s file.pl -t goal
where goal is the goal/query you want to evaluate. Note that in this case you won't be getting the option to ask for alternative solutions.
On Ubunutu, I started off using emacs, which at least does syntax highlighting:
http://www.swi-prolog.org/FAQ/GnuEmacs.html
(2 emacs suggestions on that page ^)
But now I use prolog in anger, I use an Eclipse plugin called PDT:
http://sewiki.iai.uni-bonn.de/research/pdt/docs/v2.1/start
Especially useful is the real-time line by line debug and trace, so you can step into, step over individual predicates, monitor variables names etc.. just like an other real IDE you would find in eclipse.
Probably only worth installing if you're going to use it a LOT, since the install is a lot of work, but it's a great IDE.
But if you like your low level editors like VIM, you will have to use the debug and trace tools built into swi-prolog, see:
http://www.swi-prolog.org/pldoc/man?section=debugger
To work out how the strange and beautiful prolog interpreter works, using a tracer of some kind is a must-have.
I personally use gprolog or swipl in the interpreted environment.
So you write facts and rules in a mydb.pl file, and open the interpreter in the same directory.
Once the prompt shows up you can query
['mydb.pl'].
for loading your database. now you can either see the warnings\errors or start querying from inside the prolog interpreter.
buddy I also use vim to edit prolog code, What I personally do is I save my prolog file with the '.pl' extension, and then on the terminal, I use prolog interactive environment to consult my file
e.g:
To initiate a prolog interactive environment just type On terminal:
prolog
Now that you have entered in SWI-prolog you can use 'consult' i.e pre-defined pseudo-predicates allow one to load Prolog code into a running Prolog interpreter:
?- consult("filename.pl")
that's it!
You can use any text editor to write your code. Just make sure to save your code with the .pl extension like fibo.pl.
After that open the terminal and go to the location where you have saved your code.
After that type prolog
After that write the name of your file without .pl extension and end it with . ['fibo']. and press return
Eg - cd /home/student/14917
prolog
['fibo'].
Here fibo.pl is my program name
I use SWI prolog with Sublime Text on mac. Works really nice. In Sublime Text you just hit cmd - B to run the code, and the output appears in a window within Sublime Text. There is a package for it here.

Mount executable as file on Linux/Unix filesystem

Is it possible to make an executable look like a read-only file on Linux, such that opening the "file" for reading actually executes the file and makes its stdout available for reading as if it were data in the "file"? It should be openable by any program that knows how to open a file for reading, for example 'cat'.
Look at popen. Basically what you are describing is a pipe.
P.S. If you need language specific help, edit the question and add the language/environment you're working in and I'll try to provide more specifics.
Use FUSE
On unix-like OS's you can send the output of a program to a named pipe that is opened by another program. Look at the mkfifo command to create the named pipe. The named pipe works a lot like a file, with some limitations. For example, it is not seekable.
Seems like you could pipe the output of the program into whatever you are using to "read". The problem is if you want to open the executable in say emacs or vim or whatever, it's not a matter of the executable so much as the editor doesn't know any other way to interpret it.

Resources