How to send command in separate python window - python

Searching isn't pulling up anything useful so perhaps my verbiage is wrong.
I have a python application that I didn't write that takes user input and performs tasks based on the input. The other script I did write watches the serial traffic for a specific match condition. Both scripts run in different windows. What I want to do is if I get a match condition from my script output a command to the other script. Is there a way to do this with python? I am working in windows and want to send the output to a different window.

Since you can start the script within your script, you can just follow the instructions in this link: Read from the terminal in Python
old answer:
I assume you can modify the code in the application you didn't write. If so, you can tell the code to "print" what it's putting on the window to a file, and your other code could constantly monitor that file.

Related

Is there a way to continuously collect output from a Python script I'm running into a c++ program?

So, I'm currently trying to construct a c++ aplication that calls for a python script. The main idea is that the python script runs a loop and prints decisions based on user input. I want the cpp program to be able to wait and read(if there s an output from python). I tried to make them "talk" via a file but it turned out bad. Any ideas?
PS: im calling the script using system("start powershell.exe C:\\python.exe C:\\help.py");
If there is any better way please let me know! Thanks
You could write to a file from python and check every certain amount of time in the c++ program to check for any changes in the file.
No, there is no standard way to capture the output if you start the program using std::system.
The operating system facility that you're looking for is "stream". The standard C++ provides access only to the standard streams, which you can use if you redirect the output of the python program when you start the C++ program. Example shell command:
python help.py > cpp_program
If you do this, then you can continuously read the output from the standard input stream in the C++ program. There is no standard way to create extra streams in C++, although that possibility is typically provided by the operating system.

Python: call and run a process with input files

I am looking for a solution to run a process with input files in python:
in my script I call a process using sub-process:
import subprocess as sp
sp.call(['C:\EnergyPlusV8-8-0\EP-Launch.exe'])
So the program I would like to launch is open, but then I need to choose 2 input files and then press the button "Simulate.." to execute the program(Energy Plus).
***comment:
I mean, after those code lines, the interface of the program(Energy Plus) is open, then I choose in that window which input files the program has to use. After that in the same interface of the program I start the simulation. I want to do these steps just in the python code, without interacte with the EnergyPlus interface. I hope I clearify the ambiguities
I would like to do the last steps automatically(knowing the input files location) in the python code.
How can I do this?
You won't be able to do this unless EnergyPlus is providing some kind of API, or you are prepared to write UI manipulation code, which would really depend on the type of application it is. Without more information I'm going to have to say what you want to do is not possible.

How to start and stop scripts using another script?

I don't have a lot of experience coding so I'm sorry if this has been answered before; I couldn't find anything that helped.
I just completed a project on a Raspberry Pi that runs some RGB LED strips via PWM. I have a program that runs the lights and works fine with a few different modes (rainbow shifting, strobe, solid color) but with each new mode I add the program get longer and more convoluted. I would like to have each separate mode be its own script that gets started or stopped by a sort of master script. That way I could easily add a new mode by simply writing a separate program and adding it to the list on the master script instead of mucking around inside a giant program with everything in it and hoping I don't break something. I guess what I want is a simple way to start a python script with some specific setting (Determined by variables passed from the master script) and be able to kill that script when the master script receives the command to change modes.
Keeping your code modulable is indeed a good practice ! If your code is not Objet oriented, the best way is to create another python file (let's call it util.py) in the same directory as your "main". You can simply include util.py with the following command at the beginning of your main code :
import util
And then when you want to use a function that you've defined in your util.py file, juste use :
util.myFunction(param1, param2,...)

Running a Perl script and entering commands into it from Python

I'm a week into learning Python and am trying to write a piece of code that allows me to run a text-based Perl script in LXTerminal automatically. I have a couple of questions regarding some specifics.
I need my code to start the Perl script with a user-inputted environment file, enter a few specific settings into the Perl script, and then read in many .txt files, one at a time, into the Perl script. It also needs to restart the process for every single .txt file and capture each individual output (it would help if every output could be written to a single .csv file).
To call the Perl script, I'm starting with the following:
alphamelts="/home/melts/Desktop/alphamelts"
pipe=subprocess.Popen(["perl", "/home/Desktop/melts/alphaMELTS", "run_alphamelts.command -f %s"]) % raw_input("Enter an environment file:"), stdout=PIPE
Assuming that's correct, I now need it to read in a .txt file, enter number-based commands, have my code wait for the Perl script to finish its calculations, and I need it to write the output to a .csv file. If it helps, the Perl script I'm running automatically generates a space delimited file containing the results of its calculations once the program exists, but it would be super helpful if only a few of its outputs were written onto a single seperate .csv file for each .txt file processed.
No idea where to go from here but I absolutely have to get this working. Sorry for the complexity.
Thank you!
you can do some really cool stuff in ipython. Check out this notebook for some specific examples. As far as waiting for a subprocess to finish, I think you need to put a pause in your script. Also, for data handling and export to csv and excel, I'd recommend pandas
Just something to get you started.

Vim (macvim): Alternately read input from keyboard and external program

I've got a python program that reads input from a midi device and produces text output derived from the incoming MIDI messages. As a simple example, let's say that it's simply mapping MIDI Note On events to note names e.g. note_on(60) --> 'C'. I'd like to capture the output in real time to a GVIM (actually MacVim) window without losing the ability to edit the output with a computer keyboard, i.e. I need for MacVim to read from both an external program and from the computer keyboard.
What's the cleanest general way to implement that under the assumption that the MIDI reader will never generate output while I'm trying to type and vice-versa? I'd prefer to be able to give the python script a filename and have it start MacVim with that file open, but doing it with shell commands or connecting from within MacVim would also be acceptable.
Based on the answers to How do I read and write repeatedly from a process in vim?, it looks like vim does not easily support 2 input sources asynchronously. I'll leave the question open in case someone happens to know an elegant solution, but for now it seems like the best approach is have my python program write to a normal file, use 'tail -f' for real-time viewing, and edit afterwards.

Categories

Resources