I am currently working on GT-suite (a multiphysics simulation software) and I want to be more efficient.
The software includes Python and an interface to write Python script and a console.
I would like to get an integer by the user, but when I use input(), I get the following error:
Traceback (most recent call back)
File «stdin» Line 29 in module
EOFError : EOF when reading a line
I searched a little and it seems that the console is only there for showing output and so I can’t input in it.
Is it possible to open a windows terminal when the function input() is read by Python, enter the integer in the windows terminal, press enter and then the integer input is read by the script?
I'm not familiar with the GT-suite, but if it's true the console is only used for output (and it sounds like that's the case), then you cannot used input() indeed.
Using subprocess to execute another script that uses input() will not work either, since the child process will not have access to the standard input if the parent process doesn't have it.
Some possible work-arounds I can think of are:
Look for an alternative way to get user input using the GT-suite software. If it supports such a thing, that would probably be the best and easiest solution. I searched for some information about it, but unfortunately couldn't find it. However, the website mentions a video in which "a script executes based on user inputs (...)", which suggests it should be possible (or I'm misunderstanding it). They also seem to have an API, but I couldn't acccess it without account. If you can find the API documentation, you might search for a way to get the user input.
You could create a simple GUI in Python, with a text input field for the number. That should work. However, this is not a simple/straightforward solution, and I doubt you will become "more efficient". But that depends on your use case and is for you to decide.
There are many GUI frameworks, but if you quickly want to test if entering numbers in the GUI will be possible, you could use the following code:
from tkinter import *
window = Tk()
txtfld = Entry(window, bd=5)
txtfld.place(x=0, y=0)
window.title('Enter number')
window.geometry("300x100")
window.mainloop()
This creates a small windows with a text field, where you could enter a number. It does not return the value to the main script yet. It's simply the bare minimum to test if such a solution would work in your situation. If this works, you could extend the script to your needs.
Related
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.
When reading a book or just coding on terminal/IDLE it's common to make typo, forgot brace or comma etc. After I got error and all what I wrote before is lost.
Then I have to write down code again..
Is there any way/option to return back all what write before and just edit mistake and continue to code?
In Idle (at least my version, Python 2.7.10 on windows), you can simply copy paste your code. In the python interpreter, you can't afaik, however you can use the up/down arrow keys to recall lines you previously "submitted" (i.e. typed and pressed enter).
If I understood correctly, IDLE is a GUI (graphical user interface - a visual representation of a program rather just through text) made to have a bit more features for programming in Python. You can use IDLE interactively, like in Terminal (a.k.a command line), or use it to write your script rather than in a separate text editor. Then once you save your script/program you can do neat things like run it directly from IDLE. There's nothing more special about the Terminal, you just have to do some more work.
Furthermore, all the code you have written on your GUI is on the cache memory which is used in system to store information recently accessed by a processor. So, I suggest you write again your code you can't recover them without saving.
To avoid these kind of problems use Git!
Git is a version control system that is used for software development and other version control tasks.
IDLE's Shell window is statement rather that line oriented. One can edit any line of a statement before submitting it for execution. After executing, one may recall any statement by either a) placing the cursor anywhere on the statement and hitting Enter, or b) using the history-next and history-prev actions. On Windows, these are bound, by default, to Alt-p and Alt-p. To check on your installation, Select Options => IDLE preferences on the menu. In the dialog, select the Keys tab. Under Custom Key Bindings, find the 'histor-xyz' actions in the alphabetical list.
For short, one-off scripts, I have a scratch file called tem.py. Since I use it often, it is usually accessible via File => Recent files.
To simplify, let's say I'm trying to write a command line two-way chat in Python. I would like the user to input his message with input() at the command prompt, but a listening thread could print a message at any moment. By default, this would "break" the user's input. Visually something like this:
userB>Stop interuserA wrote:Hey check this out!
rupting me!
The closest I was able to find was this answer here which is almost, but not exactly, what I'm looking for, but it did point me towards the blessings package which seems to be what I need (although I'm happy with an answer for any package, or even pure ANSII).
What I'm trying to achieve is to print incoming output from a Thread above the user's input, so that his text doesn't break. Let's say the user is typing:
userB>Stop inter
Suddenly a message comes in from the thread, but our user's input doesn't brake:
userA says: Ok I won't interrupt you
userB>Stop inter
What should my threads theoretical print_incoming_message() method look like to achieve this?
NB: I'm using Linux and am not interested in cross-platform compatibility.
There are two ways of doing this.
One is to use ncurses. There are python bindings for this. With ncurses, the terminal screen is under your complete control, and you can print characters at any point.
Without ncurses, you can't write above the current line. What you can do, however, is print a \r character and go back to the beginning of the line.
If you save the user's input (say he wrote foo), and you want to print the line bar above that, you can output:
\rbar\nfoo
This will overwrite the current line, and introduce a newline, moving the user's input down. The effect is similar, but it won't be as tamper-proof as ncurses.
This Python program enters fdisk. I see the output. fdisk is an interactive program. How do I get the Python program to pass an "m" to the first field and press enter?
import subprocess
a = "dev/sda"
x = subprocess.call(["fdisk", a])
print x
I'd rather not import a new module/library, but I could. I've tried different syntax with subprocess.call() and extra parameters in the above. Nothing seems to work. I get different errors. I've reviewed Python documentation. I want to feed input and press Enter in the subsequent, interactive menu options of fdisk.
Check out the pexpect library (I know you didn't want an extra module, but you want to use the best tool for the job). It's pure Python, with no compiled submodules, so installation is a snap. Basically, it does the same thing in Python as the classic Unix utility expect - spawns child applications, controls them, and responds to expected patterns in their output. It's great for automation, and especially application testing, where you can quickly feed the newest build of a command-line program a series of inputs and guide the interaction based on what output appears.
In case you just don't want another module at all, you can always fall back on the subprocess module's Popen() constructor. It spawns and creates a connection to a child process, allowing you to communicate with it as needed, and in fact pexpect relies a great deal on it. I personally think using pexpect is more intuitive than subprocess.Popen(), but that's just me. YMMV.
I am writing an interpreter in Python where the user has to type Prolog code and I'd like to implement dynamic syntax highlighting. So far my application (which should be cross-platform) is reading input and printing from/to the command prompt (on Windows). I've been looking at the pygments library, but I have no clue on how to modify the current line (being typed) on the command prompt.
So, I've also been looking at the curses library to implement an UI but that would leave Windows users out. Any suggestions? I just need to implement a quick UI to be able to dynamically highlight what is being typed.
It appears that you can't wait for a return. To deal with input one char at a time you need something like getch.