Shift + Return to insert linebreak in python - python

I'm trying to get the behaviour of typical IM clients that use Return to send a text and Shift + Return to insert a linebreak. Is there a way to achieve that with minimal effort in Python, using e.g. readline and raw_input?

Ok, I heard it can be accomplished also with the readline, in a way.
You can import readline and set in configuration your desired key (Shift+Enter) to a macro that put some special char to the end of the line and newline. Then you can call raw_input in a loop.
Like this:
import readline
# I am using Ctrl+K to insert line break
# (dont know what symbol is for shift+enter)
readline.parse_and_bind('C-k: "#\n"')
text = []
line = "#"
while line and line[-1]=='#':
line = raw_input("> ")
if line.endswith("#"):
text.append(line[:-1])
else:
text.append(line)
# all lines are in "text" list variable
print "\n".join(text)

I doubt you'd be able to do that just using the readline module as it will not capture the individual keys pressed and rather just processes the character responses from your input driver.
You could do it with PyHook though and if the Shift key is pressed along with the Enter key to inject a new-line into your readline stream.

I think that with minimal effort you can use urwid library for Python. Unfortunately, this does not satisfy your requirement to use readline/raw_input.
Update: Please see also this answer for other solution.

import readline
# I am using Ctrl+x to insert line break
# (dont know the symbols and bindings for meta-key or shift-key,
# let alone 4 shift+enter)
def startup_hook():
readline.insert_text('» ') # \033[32m»\033[0m
def prmpt():
try:
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('set editing-mode vi')
readline.parse_and_bind('C-x: "\x16\n"') # \x16 is C-v which writes
readline.set_startup_hook(startup_hook) # the \n without returning
except Exception as e: # thus no need 4 appending
print (e) # '#' 2 write multilines
return # simply use ctrl-x or other some other bind
while True: # instead of shift + enter
try:
line = raw_input()
print '%s' % line
except EOFError:
print 'EOF signaled, exiting...'
break
# It can probably be improved more to use meta+key or maybe even shift enter
# Anyways sry 4 any errors I probably may have made.. first time answering

Related

How can i print on a new line without skipping?

I have a bit of code from a class which prints a line, and every line is followed by an empty line.
Is there a way to adjust the following code so that I don't have to have those empty lines?
def bfield(self):
self.n=0
for i in self.whole:
for j in i:
print("{:>4}".format(j), end='')
self.n=self.n+1
if self.n==len(i):
print('\n')
self.n=0
I'll agree with Rahul Chowdhury, remove the \n. Pythons print command, by default, will always start a new line after each print call. Hence your addition of '\n' will always result in an empty line.
If you wanted to look into how to get around the whole newline thing python does (every call in its own line), I found this link for you. It is fairly simple to do!
EDIT: It just occurred to me I should maybe list a few of the options just in case the link goes down. Here is one example:
print("Hello ", end = '')
print("World!")
With this, you overwrite the usual python lineend with your end = '' argument.
Another option would be to use the sys library
import sys
and then call the stdout.write() function, like so:
sys.stdout.write("Hello ")
sys.stdout.write("World!")

Loading message in Python 2.7?

I am making a personal assistant in Python 2.7 using the modules 'wikipedia', 'wolframalpha' and 'pyttsx3'. I am making so that the user can ask a question and the computer will then search Wikipedia and Wolfram and speak the answer using Pyttsx. This all works fine but the computer takes a while to fetch the results for the question and I was wondering if it would be possible to add a simple '...loading...' message while is does this. I have added the code below and it would be great if you could respond.
import wikipedia
import wolframalpha
import pyttsx3;
engine = pyttsx3.init();
while True:
my_input = raw_input("Question: ")
try:
#wolframalpha code here
app_id = "Q2HXJ5-GYYYX6PYYP"
client = wolframalpha.Client(app_id)
res = client.query(my_input)
answer = next(res.results).text
print(answer)
engine.say(answer);
engine.runAndWait();
except:
try:
#wikipedia code here
print(wikipedia.summary(my_input))
except:
print("Sorry nothing can be found from your query")
If you want to remove Loading... after the API call is completed, you can just move the cursor to the start of that line using the escape code ESC[1000D. Note that you must use sys.stdout.write() as opposed to print here, as we want this all to happen on the same line.
import sys
// Before API Call
sys.stdout.write("Loading...")
sys.stdout.flush()
// After API Call
sys.stdout.write(u"\u001b[1000D")
print "Done! "
Note the u proceeding the double-quoted string. This is required in Python 2.x, as it includes special characters, but can be omitted in Python 3.
(By the way, the extra spaces on Done are only there so that the string is longer than Loading... so that it replaces it completely, without leaving ng... on the end)

Python : correct use of set_completion_display_matches_hook

I'm trying to write a function to display a custom view when users press the tab button. Apparently "set_completion_display_matches_hook" function is what I need, I can display a custom view, but the problem is that I have to press Enter to get a prompt again.
The solution in Python2 seems to be that (solution here):
def match_display_hook(self, substitution, matches, longest_match_length):
print ''
for match in matches:
print match
print self.prompt.rstrip(),
print readline.get_line_buffer(),
readline.redisplay()
But it doesn't work with Python3. I made these syntax changes :
def match_display_hook(self, substitution, matches, longest_match_length):
print('\n----------------------------------------------\n')
for match in matches:
print(match)
print(self.prompt.rstrip() + readline.get_line_buffer())
readline.redisplay()
Any ideas please ?
First, the Python 2 code uses commas to leave the line unfinished. In Python 3, it's done using end keyword:
print(self.prompt.rstrip(), readline.get_line_buffer(), sep='', end='')
Then, a flush is required to actually display the unfinished line (due to line buffering):
sys.stdout.flush()
The redisplay() call does not seem to be needed.
The final code:
def match_display_hook(self, substitution, matches, longest_match_length):
print()
for match in matches:
print(match)
print(self.prompt.rstrip(), readline.get_line_buffer(), sep='', end='')
sys.stdout.flush()
The redisplay() function
voidrl_redisplay (void)
Change what's displayed on the screen to reflect the current contents of rl_line_buffer.
In your example you have written to stdout, but not changed that buffer.
Print and flush as described by in other answer should work.
One issue you will have, however, is cursor position. Say you have this scenario:
$ cmd some_file
^
+---- User has back-tracked here and want to insert an option.
<TAB> completion with print and flush will put cursor
at end of `some_file' and the line will get an extra 15
spaces after that ...
To remedy this one way is to first get cursor position, then use ANSI sequences to re-position the cursor.
buf = readline.get_line_buffer()
x = readline.get_endidx()
print(self.prompt + buf, end = '')
if x < len(buf):
""" Set cursor at old column position """
print("\r\033[%dC" % (x + len(self.prompt)), end = '')
sys.stdout.flush()
Now, of course, you get another issue if prompt has ANSI sequences in-iteself. Typically color or the like. Then you can not use len(prompt) but have to find printed / visible length.
One has to use open and close bytes elsewhere, typically \0x01 and \0x02 respectively.
So one typically get:
prompt = '\001\033[31;1m\002VISIBLE_TEXT\001\033[0m\002 '
instead of:
prompt = '\033[31;1mVISIBLE_TEXT\033[0m '
With those guards it should be easy enough to strip out the visible text.
Typically something like:
clean_prompt = re.sub(r'\001[^\002]*\002', '', prompt))
Cache the length of that and use when printing the readline manually. Note that you also have to remove the guards when using it manually - as in the hook function. (But it is needed in input(prompt)
this one worked for me for redisplaying substitution and the end of matches display for python3:
def match_display_hook(self, substitution, matches, longest_match_length):
print("")
for match in matches:
print(match)
print("")
sys.stdout.write(substitution)
sys.stdout.flush()
return None
while previous ones using print prompt didn't. (didn't get to the bottom of the problem)

Python command Line - multiple Line Input

I'm trying to solve a Krypto Problem on https://www.spoj.pl in Python, which involves console input.
My Problem is, that the Input String has multiple Lines but is needed as one single String in the Programm.
If I just use raw_input() and paste (for testing) the text in the console, Python threats it like I pressed enter after every Line -> I need to call raw_input() multiple times in a loop.
The Problem is, that I cannot modify the Input String in any way, it doesn't have any Symbol thats marks the End and I don't know how many Lines there are.
So what do I do?
Upon reaching end of stream on input, raw_input will return an empty string. So if you really need to accumulate entire input (which you probably should be avoiding given SPOJ constraints), then do:
buffer = ''
while True:
line = raw_input()
if not line: break
buffer += line
# process input
Since the end-of-line on Windows is marked as '\r\n' or '\n' on Unix system it is straight forward to replace those strings using
your_input.replace('\r\n', '')
Since raw_input() is designed to read a single line, you may have trouble this way.
A simple solution would be to put the input string in a text file and parse from there.
Assuming you have input.txt you can take values as
f = open(r'input.txt','rU')
for line in f:
print line,
Using the best answer here, you will still have an EOF error that should be handled. So, I just added exception handling here
buffer = ''
while True:
try:
line = raw_input()
except EOFError:
break
if not line:
break
buffer += line

Using the input collected from raw_input in Python

If I do the following in python,
string = raw_input('Enter the value')
it will return
Enter the value
and wait until I enter something into the prompt.
Is there a way to retrieve/collect the input I entered in a variable string?
I would like to use the entered value in the following way :
if dict.has_key('string'):
print dict[string]
Note: I previously made the error of using raw_string but I meant to say raw_input
there's no raw_string function in python's stdlib. do you mean raw_input?
string = raw_input("Enter the value") # or just input in python3.0
print(string)
This is very confusing ... I'm not familiar with a raw_string function in Python, but perhaps it's application-specific?
Reading a line of input in Python can be done like this:
import sys
line = sys.stdin.readline()
Then you have that line, terminating linefeed and all, in the variable line, so you can work with it, e.g. use it has a hash key:
line = line.strip()
if line in dict: print dict[line]
The first line strips away that trailing newline, since you hash keys probably don't have them.
I hope this helps, otherwise please try being a bit clearer in your question.
Is this what you want to do?
string = raw_input('Enter a value: ')
string = string.strip()
if string in dict: print dict[string]
import readline provides a full terminal prompt to the raw_input function. You get control keys and history this way. Otherwise, it's no better than the sys.stdin.readline() option.
import readline
while True:
value = raw_input('Enter a value: ')
value = value.strip()
if value.lower() == 'quit': break

Categories