How to Get Python Application to Communicate with Terminal - python

I am curious if it possible to make custom terminal commands for a Python application, for example, if I were to make a slideshow program, one could into the command line INDEPENDENTLY from the program and type
C:\Users\foo> Slides
And get
SLIDES v0.0.0 Example
Example command, example command
And if you typed
start unnamed.slides
The slide program would open up and run slide project “unnamed”.
Please ignore any factual errors in this if it were the actual commands for a program I’m just trying to get a point across.

You might want to take a look at the Argparse library that allows you to have command line options that are easily manageable. You can then use these options to execute certain parts of you program.

All you need to do is use terminal input functions inside a while loop with code that responds to the input accordingly. To put it simply it would be like this:
while True:
user_input = input('command: ')
# do whatever with the user input here.

Related

How do I make inputs part of a python script, so the input will forever be displayed every time the program is openned?

I'm trying to make a database-like program using python 3.8.2, and I'm trying to figure out how to make permanent changes to the script playing the role of a user. Is there a way to
A) Revieve an input from the user
B) Make that input a part of the script, so it will always show up when the program is opened
The official tutorial tells you how.
It would look something like:
import sys
for script_argument in sys.argv:
print(script_argument)
If you want something more complex, check out argparse.

Unable to get the "whole" childprocess output when using Popen

I'm trying to use a python script to automate an interactive console application.
(it's called BLEConsole and it's a microsoft store app https://www.microsoft.com/en-us/p/bleconsole/9p5662fdq58v , not sure if that's important. )
So I've been using os.popenX and subprocess.popen to launch and interact with it, and it's working fine... mostly !
inp,outp = os.popen4('BLEConsole.exe')
inp.write('ls\n') # this one work
inp.write('open #01\n') # this one don't generate any output for some reason
inp.write('format hex\n') # and it's working again ??
inp.write('quit\n')
print outp.read()
some commands work and print to stdout but some other don't.
they do work (the bluetooth device connects just fine) but i'm just unable to capture any output other than line breaks when executing this specific command. (yes it's supposed to output stuff, it does when I do it manually)
edit : yes I'm checking both stdout and stderr
Also I'm not able to capture the "BLE:" prompt using this method, I don't really need it but I don't understand why.
Thanks for your help

is it possible to launch a window that does not stop the mainloop in python

OS = windows 7
I have a python program (works) that is listening to activity on the usb bus. I want to perform a lot of tests that require a particular user input at a particular time. I would like to pop up a window that says, "press button xxx". The key point is that the mainloop needs to continue running because it's looking for events. I don't care about the window or if it remains or not and I don't need to capture any information from the window. I just want a message to the user to press the correct button at the correct time. Any type of signaling would work; it doesn't have to be a gui window. It doesn't have to look pretty. Appreciate any suggestions or links to something like this. thx
It sounds like the operation of the Python script you're running does not depend upon the user input you request. To run another process without interrupting the Python script execution you can use:
import subprocess
subprocess.Popen([exe,arg1,arg2,arg3])
where
exe = executable/script to run from your OS command line
arg1= first argument to pass to exe
arg2= second argument to pass to exe
etc... (as many arguments as your OS supports in a list)
This separate exe process could request input from the user.

Python shell issue when using Data Nitro

I am using DataNitro to write Python Script in Excel. Its very useful indeed. However, when I open the Idle editor in excel, the accompanying Python Shell is not interactive, in that it does not return print statements, show errors, nothing. It just restarts every time I run the programme. This makes it incredibly hard to debug as I can't use print statements to trace the errors.
Does anyone know if this is a bug with DataNitro, or is it supposed to be that way, or whats going on? are there any solutions?
Thanks so much
Our IDLE editor is just an editor - it doesn't work as a shell.
The best way to debug programs is to raise an exception. This will freeze the shell that opens when a script is run, and you'll be able to inspect the variables and see any print statements that were generated during execution.
For example, if you run:
print Cell("A1").value
x = Cell("B1").value
raise
You'll see the value of A1 printed to the shell, and you can enter "x" at the prompt to see the value of B1.
You can also import a script you're working on into the regular Python shell (the one that opens when you press "shell"). This will execute the code in that script.
We'll be adding a guide to debugging code to the site soon, as well as some features that make it easier.
Source: I'm one of the founders of DataNitro.
Not as knowledgeable as Ben, but have been using DataNitro quite a bit and here are some tips:
The shell automatically closes once the script has run. If you want to inspect some prints or even interact with the shell I normally place following at end of my script.
raw_input("Press Enter to Exit shell")
Not very elegant, but I have even created a small loop that displays text options in the console. Can then interact with your program and sheet from there. Clever and more elegant way would be to have your script poll an excel cell and then take action form there.
Something else that you might find nice is that it also also you to run Ipython instead of the default python shell. Cannot imagine using python without Ipython... so you get benefits of tab completion Ipython debugging etc. To activate that just click the "Use Ipython" Checkbox in DataNitro Settings (don't know if this is version dependent).

How to initiate python program with a change in the input?

I have an application made in python, this application takes input from a separate text file called input.txt, i have to design the application in such a way that application runs automatically with any input to input.txt. Such that i need not go to command prompt and run the program, if i give the input, or change the text present in input.txt, python program should start execute automatically.
Please help me out with this :)
Thank you :) :)
You can run your program once and watch the filesystem changes for new or modified file. Try using python-inotify for linux:
https://github.com/seb-m/pyinotify

Categories

Resources