python script more than one function - python-3.4

If your python script contains two or more functionalities, how can you run the script continuously to allow the user to select any one of these functionalities without restarting your program in a new IDLE console?

You could ask the user for input with the input prompt and decide what to do based on the input like this:
response = input("Please enter your name: ")
if (response == "John"):
fun_1(something)
else:
fun_2(something_else)
The input prompt will wait for user input and record every input up to a carriage return (ENTER).

Related

Kernel cancelling a `input_request` at the end of the execution of a cell

I'm implementing a new Go kernel, using directly the ZMQ messages. But as an extra I want it to execute any bash command when a line is prefixed with !, similar to the usual ipython kernel.
One of the tricky parts seems to be bash scripts that take input -- there is no way (that I know of) to predict when I need to request input. So I took the following approach:
Whenever I execute a bash script, if it hasn't ended after 500ms (configurable), it issues an input_request.
If the kernel receives any input back (input_reply message), it writes the contents to the bash program's piped stdin (concurrently, not to block), and immediately issues another input_request.
Now at the end of the execution of the bash program, there is always the last input_request pending, and the corresponding widget expecting input from the user.
Jupyter doesn't drop the input_request after the execution of the cell ended, and requires the user to type enter and send an input_reply before another cell can be executed. It complains with "Cell not executed due to pending input"
Is there a way to cancel the input_request (the pending input) if the execution of the last cell already finished ?
Maybe there is some undocumented message that can be send once the bash program ends ?
Any other suggested approach ?
I know something similar works in colab.research.google.com, if I do:
!while read ii; do if [[ "${ii}" == "done" ]] ; then exit 0; fi ; echo "Input: $ii"; done
It correctly asks for inputs, and closes the last one.
But I'm not sure how that is achieved.
Jupyter's ipython notebook doesn't seem to have that smarts though, at least here the line above just locks. I suppose it never sends a input_request message.
many thanks in advance!

How do I get input from the console in Nim?

I'm new to Nim, but it appears that there is no way to get input from the console in a similar way to input() in python or Console.ReadLine() in dotnet.
I want execution of my code to pause, wait for input, then on pressing enter to continue (just like input in other langs).
Oh no never mind found it:
var consoleInput = readLine(stdin);

Telegram Bot. How set command /cmd in user text input?

I need to create command (for example - /cmd). When user click on this command in list - bot set this command text(/cmd) in user input and user should input argument for this command. How to do this?
Example:
I have command /cmd. And when user input is - "/cmd parameter1" it goes to execute command with this parameter. I need to allow the user not to enter "/cmd" - it will add it automatically.
Based on question & comments;
bot set this command text(/cmd) in user input
A regular Telegram bot can't place text in Input field. This is only possible with an Inline bot.
If you wish to 'ask' the user a specific 'argument' based on the /cmd command. You could use an Inline bot, an Keyboard to list all available 'arguments' or ask the user, and 'wait' for a reply.
You could also show all available command with an Keyboard and then change the keyboard to the available arguments with on-the-fly updatable inline-keyboard

How to get user input before saving a file in Sublime Text

I'm making a plugin in Sublime Text that prompts the user for a password to encrypt a file before it's saved. There's a hook in the API that's executed before a save is executed, so my naïve implementation is:
class TranscryptEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
# If document is set to encode on save
if view.settings().get('ON_SAVE'):
self.view = view
# Prompt user for password
message = "Create a Password:"
view.window().show_input_panel(message, "", self.on_done, None, None)
def on_done(self, password):
self.view.run_command("encode", {password": password})
The problem with this is, by the time the input panel appears for the user to enter their password, the document has already been saved (despite the trigger being 'on_pre_save'). Then once the user hits enter, the document is encrypted fine, but the situation is that there's a saved plaintext file, and a modified buffer filled with the encrypted text.
So I need to make Sublime Text wait until the user's input the password before carrying out the save. Is there a way to do this?
At the moment I'm just manually re-saving once the encryption has been done:
def on_pre_save(self, view, encode=False):
if view.settings().get('ON_SAVE') and not view.settings().get('ENCODED'):
self.view = view
message = "Create a Password:"
view.window().show_input_panel(message, "", self.on_done, None, None)
def on_done(self, password):
self.view.run_command("encode", {password": password})
self.view.settings().set('ENCODED', True)
self.view.run_command('save')
self.view.settings().set('ENCODED', False)
but this is messy and if the user cancels the encryption then the plaintext file gets saved, which isn't ideal. Any thoughts?
Edit: I think I could do it cleanly by overriding the default save command. I hoped to do this by using the on_text_command or on_window_command triggers, but it seems that the save command doesn't trigger either of these (maybe it's an application command? But there's no on_application_command). Is there just no way to override the save function?
Edit: I've ended up just overriding the default key binding to a TextCommand, and there seem to be no problems.
You would need to create a new command that overrides the existing save behavior to do what you want. As you've seen, the show_input_panel command is asynchronous. Thus, the command is "finished" after it creates the input panel. Rather than using on_pre_save you may want to try creating a TextCommand to do the save. Again, the downside to this is you would have to override the existing key binding. I suppose you could use the command listeners that are available in ST3, though I don't know if you are trying to create a plugin that is compatible with ST2 also.

How do I take keyboard input in AutoIt?

I want to write a script in AutoIt, which can take automatic input from the keyboard, let's say A-Z, without user intervention.
Is this possible?
It is unlikely that your program needs to capture all input from all keys. If you do in fact need that kind of user input AutoIt might not be for you - see the post from the author of AutoIt about keyloggers. If you need to take keyboard input of the hotkey type: doing that in AutoIt is super easy.
HotKeySet("^+{q}", "reactionFunction")
While 1
; A loop
WEnd
Func reactionFunction()
MsgBox(0, "You pressed CTRL+Shift+q", "You pressed CTRL+Shift+q")
Exit
EndFunc
If you want to take user input from an input box that is really easy also.
$data = InputBox("Enter Something", "Enter some data in the field below.")
MsgBox(0, "The String You Entered...", "The string you entered is... " & $data)
More information about HotKeySet and InputBox can be found in the AutoIt.chm help file (it's actually a great reference).
Not sure I understand your question - you want to simulate keypresses without someone actually using the keyboard? If so, that's the send command in AutoIt.
You want to let a real user submit input to the script? That's what the GUI in AutoIt is for.

Resources