I know I can stop print from writing a newline by adding a comma
print "Hello, world!",
# print("Hello, world!", end='') for Python 3.x
But how do I stop raw_input (or input for Python 3.x) from writing a newline?
print "Hello, ",
name = raw_input()
print ", how do you do?"
Result:
Hello, Tomas
, how do you do?
Result I want:
Hello, Tomas, how do you do?
But how do I stop raw_input from writing a newline?
In short: You can't.
raw_input() will always echo the text entered by the user, including the trailing newline. That means whatever the user is typing will be printed to standard output.
If you want to prevent this, you will have to use a terminal control library such as the curses module. This is not portable, though -- for example, curses in not available on Windows systems.
I see that nobody has given a working solution, so I decided I might give it a go.
As Ferdinand Beyer said, it is impossible to get raw_input() to not print a new line after the user input. However, it is possible to get back to the line you were before.
I made it into an one-liner. You may use:
print '\033[{}C\033[1A'.format(len(x) + y),
where x is an integer of the length of the given user input and y an integer of the length of raw_input()'s string. Though it might not work on all terminals (as I read when I learned about this method), it works fine on mine. I'm using Kubuntu 14.04.
The string '\033[4C' is used to jump 4 indexes to the right, so it would be equivalent to ' ' * 4. In the same way, the string '\033[1A' is used to jump 1 line up. By using the letters A, B, C or D on the last index of the string, you can go up, down, right and left respectively.
Note that going a line up will delete the existing printed character on that spot, if there is one.
This circumvents it, somewhat, but doesn't assign anything to variable name:
print("Hello, {0}, how do you do?".format(raw_input("Enter name here: ")))
It will prompt the user for a name before printing the entire message though.
You can use getpass instead of raw_input if you don't want it to make a new line!
import sys, getpass
def raw_input2(value="",end=""):
sys.stdout.write(value)
data = getpass.getpass("")
sys.stdout.write(data)
sys.stdout.write(end)
return data
An alternative to backtracking the newline is defining your own function that emulates the built-in input function, echoing and appending every keystroke to a response variable except Enter (which will return the response), whilst also handling Backspace, Del, Home, End, arrow keys, line history, KeyboardInterrupt, EOFError, SIGTSTP and pasting from the clipboard. It's very simple.
Note that on Windows, you'll need to install pyreadline if you want to use line history with the arrow keys like in the usual input function, although it's incomplete so the functionality is still not quite right. In addition, if you're not on v1511 or greater of Windows 10, you'll need to install the colorama module (if you're on Linux or macOS, nothing needs to be done).
Also, due to msvcrt.getwch using '\xe0' to indicate special characters, you won't be able to type 'à'. You should be able to paste it though.
Below is code that makes this work on updated Windows 10 systems (at least v1511), Debian-based Linux distros and maybe macOS and other *NIX operating systems. It should also work regardless of whether you have pyreadline installed on Windows, though it'll lack some functionality.
In windows_specific.py:
"""Windows-specific functions and variables for input_no_newline."""
import ctypes
from msvcrt import getwch # pylint: disable=import-error, unused-import
from shared_stuff import ANSI
try:
import colorama # pylint: disable=import-error
except ImportError:
kernel32 = ctypes.windll.kernel32
# Enable ANSI support to move the text cursor
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
colorama.init()
def get_clipboard_data():
"""Return string previously copied from Windows clipboard.
Adapted from <http://stackoverflow.com/a/23285159/6379747>.
"""
CF_TEXT = 1
user32 = ctypes.windll.user32
user32.OpenClipboard(0)
try:
if user32.IsClipboardFormatAvailable(CF_TEXT):
data = user32.GetClipboardData(CF_TEXT)
data_locked = kernel32.GlobalLock(data)
text = ctypes.c_char_p(data_locked)
kernel32.GlobalUnlock(data_locked)
finally:
user32.CloseClipboard()
return text.value
def sigtstp():
"""Raise EOFError from Ctrl-Z since SIGTSTP doesn't exist on Windows."""
raise EOFError
input_code = {
**ANSI,
'CSI': [['\xe0', '\x00'], ''],
'up': 'H',
'down': 'P',
'right': 'M',
'left': 'K',
'end': 'O',
'home': 'G',
'backspace': '\b',
'del': 'S',
}
In unix_specific.py:
"""Functions and variables for Debian-based Linux distros and macOS."""
import sys
import os
import tty
import signal
import termios
from shared_stuff import ANSI
def getwch():
"""Return a single character from user input without echoing.
ActiveState code, adapted from
<http://code.activestate.com/recipes/134892> by Danny Yoo under
the Python Software Foundation license.
"""
file_descriptor = sys.stdin.fileno()
old_settings = termios.tcgetattr(file_descriptor)
try:
tty.setraw(file_descriptor)
char = sys.stdin.read(1)
finally:
termios.tcsetattr(file_descriptor, termios.TCSADRAIN, old_settings)
return char
def get_clipboard_data():
"""Return nothing; *NIX systems automagically change sys.stdin."""
return ''
def sigtstp():
"""Suspend the script."""
os.kill(os.getpid(), signal.SIGTSTP)
input_code = {
**ANSI,
'CSI': ['\x1b', '['],
'backspace': '\x7f',
'del': ['3', '~'],
}
In readline_available.py:
"""Provide functions for up and down arrows if readline is installed.
Basically to prevent duplicate code and make it work on systems without
readline.
"""
try:
import readline
except ImportError:
import pyreadline as readline
from shared_stuff import move_cursor
def init_history_index():
"""Return index for last element of readline.get_history_item."""
# readline.get_history_item is one-based
return readline.get_current_history_length() + 1
def restore_history(history_index, replaced, cursor_position):
"""Replace 'replaced' with history and return the replacement."""
try:
replacement = readline.get_history_item(history_index)
except IndexError:
replacement = None
if replacement is not None:
move_cursor('right', len(replaced) - cursor_position)
print('\b \b' * len(replaced), end='', flush=True)
print(replacement, end='', flush=True)
return replacement
return replaced
def store_and_replace_history(history_index, replacement, old_history):
"""Store history and then replace it."""
old_history[history_index] = readline.get_history_item(history_index)
try:
readline.replace_history_item(history_index - 1, replacement)
except AttributeError:
# pyreadline is incomplete
pass
def handle_prev_history(history_index, replaced, old_history,
input_replaced, history_modified):
"""Handle some up-arrow logic."""
try:
history = readline.get_history_item(history_index - 1)
except IndexError:
history = None
if history is not None:
if history_index > readline.get_current_history_length():
readline.add_history(replaced)
input_replaced = True
else:
store_and_replace_history(
history_index, replaced, old_history)
history_modified = True
history_index -= 1
return (history_index, input_replaced, history_modified)
def handle_next_history(history_index, replaced, old_history,
input_replaced, history_modified):
"""Handle some down-arrow logic."""
try:
history = readline.get_history_item(history_index + 1)
except IndexError:
history = None
if history is not None:
store_and_replace_history(history_index, replaced, old_history)
history_modified = True
history_index += 1
input_replaced = (not history_index
== readline.get_current_history_length())
return (history_index, input_replaced, history_modified)
def finalise_history(history_index, response, old_history,
input_replaced, history_modified):
"""Change history before the response will be returned elsewhere."""
try:
if input_replaced:
readline.remove_history_item(history_index - 1)
elif history_modified:
readline.remove_history_item(history_index - 1)
readline.add_history(old_history[history_index - 1])
except AttributeError:
# pyreadline is also missing remove_history_item
pass
readline.add_history(response)
In readline_unavailable.py:
"""Provide dummy functions for if readline isn't available."""
# pylint: disable-msg=unused-argument
def init_history_index():
"""Return an index of 1 which probably won't ever change."""
return 1
def restore_history(history_index, replaced, cursor_position):
"""Return the replaced thing without replacing it."""
return replaced
def store_and_replace_history(history_index, replacement, old_history):
"""Don't store history."""
pass
def handle_prev_history(history_index, replaced, old_history,
input_replaced, history_modified):
"""Return 'input_replaced' and 'history_modified' without change."""
return (history_index, input_replaced, history_modified)
def handle_next_history(history_index, replaced, old_history,
input_replaced, history_modified):
"""Also return 'input_replaced' and 'history_modified'."""
return (history_index, input_replaced, history_modified)
def finalise_history(history_index, response, old_history,
input_replaced, history_modified):
"""Don't change nonexistent history."""
pass
In shared_stuff.py:
"""Provide platform-independent functions and variables."""
ANSI = {
'CSI': '\x1b[',
'up': 'A',
'down': 'B',
'right': 'C',
'left': 'D',
'end': 'F',
'home': 'H',
'enter': '\r',
'^C': '\x03',
'^D': '\x04',
'^V': '\x16',
'^Z': '\x1a',
}
def move_cursor(direction, count=1):
"""Move the text cursor 'count' times in the specified direction."""
if direction not in ['up', 'down', 'right', 'left']:
raise ValueError("direction should be either 'up', 'down', 'right' "
"or 'left'")
# A 'count' of zero still moves the cursor, so this needs to be
# tested for.
if count != 0:
print(ANSI['CSI'] + str(count) + ANSI[direction], end='', flush=True)
def line_insert(text, extra=''):
"""Insert text between terminal line and reposition cursor."""
if not extra:
# It's not guaranteed that the new line will completely overshadow
# the old one if there is no extra. Maybe something was 'deleted'?
move_cursor('right', len(text) + 1)
print('\b \b' * (len(text)+1), end='', flush=True)
print(extra + text, end='', flush=True)
move_cursor('left', len(text))
And finally, in input_no_newline.py:
#!/usr/bin/python3
"""Provide an input function that doesn't echo a newline."""
try:
from windows_specific import getwch, get_clipboard_data, sigtstp, input_code
except ImportError:
from unix_specific import getwch, get_clipboard_data, sigtstp, input_code
try:
from readline_available import (init_history_index, restore_history,
store_and_replace_history,
handle_prev_history, handle_next_history,
finalise_history)
except ImportError:
from readline_unavailable import (init_history_index, restore_history,
store_and_replace_history,
handle_prev_history, handle_next_history,
finalise_history)
from shared_stuff import ANSI, move_cursor, line_insert
def input_no_newline(prompt=''): # pylint: disable=too-many-branches, too-many-statements
"""Echo and return user input, except for the newline."""
print(prompt, end='', flush=True)
response = ''
position = 0
history_index = init_history_index()
input_replaced = False
history_modified = False
replacements = {}
while True:
char = getwch()
if char in input_code['CSI'][0]:
char = getwch()
# Relevant input codes are made of two to four characters
if char == input_code['CSI'][1]:
# *NIX uses at least three characters, only the third is
# important
char = getwch()
if char == input_code['up']:
(history_index, input_replaced, history_modified) = (
handle_prev_history(
history_index, response, replacements, input_replaced,
history_modified))
response = restore_history(history_index, response, position)
position = len(response)
elif char == input_code['down']:
(history_index, input_replaced, history_modified) = (
handle_next_history(
history_index, response, replacements, input_replaced,
history_modified))
response = restore_history(history_index, response, position)
position = len(response)
elif char == input_code['right'] and position < len(response):
move_cursor('right')
position += 1
elif char == input_code['left'] and position > 0:
move_cursor('left')
position -= 1
elif char == input_code['end']:
move_cursor('right', len(response) - position)
position = len(response)
elif char == input_code['home']:
move_cursor('left', position)
position = 0
elif char == input_code['del'][0]:
if ''.join(input_code['del']) == '3~':
# *NIX uses '\x1b[3~' as its del key code, but only
# '\x1b[3' has currently been read from sys.stdin
getwch()
backlog = response[position+1 :]
response = response[:position] + backlog
line_insert(backlog)
elif char == input_code['backspace']:
if position > 0:
backlog = response[position:]
response = response[: position-1] + backlog
print('\b', end='', flush=True)
position -= 1
line_insert(backlog)
elif char == input_code['^C']:
raise KeyboardInterrupt
elif char == input_code['^D']:
raise EOFError
elif char == input_code['^V']:
paste = get_clipboard_data()
backlog = response[position:]
response = response[:position] + paste + backlog
position += len(paste)
line_insert(backlog, extra=paste)
elif char == input_code['^Z']:
sigtstp()
elif char == input_code['enter']:
finalise_history(history_index, response, replacements,
input_replaced, history_modified)
move_cursor('right', len(response) - position)
return response
else:
backlog = response[position:]
response = response[:position] + char + backlog
position += 1
line_insert(backlog, extra=char)
def main():
"""Called if script isn't imported."""
# "print(text, end='')" is equivalent to "print text,", and 'flush'
# forces the text to appear, even if the line isn't terminated with
# a '\n'
print('Hello, ', end='', flush=True)
name = input_no_newline() # pylint: disable=unused-variable
print(', how do you do?')
if __name__ == '__main__':
main()
As you can see, it's a lot of work for not that much since you need to deal with the different operating systems and basically reimplement a built-in function in Python rather than C. I'd recommend that you just use the simpler TempHistory class I made in another answer, which leaves all the complicated logic-handling to the built-in function.
Like Nick K. said, you'll need to move the text cursor back to before the newline was echoed. The problem is that you can't easily get the length of the previous line in order to move rightward, lest you store every string printed, prompted and inputted in its own variable.
Below is a class (for Python 3) that fixes this by automatically storing the last line from the terminal (provided you use its methods). The benefit of this compared to using a terminal control library is that it'll work in the standard terminal for both the latest version of Windows as well as *NIX operating systems. It'll also print the 'Hello, ' prompt before getting input.
If you're on Windows but not v1511 of Windows 10, then you'll need to install the colorama module or else this won't work, since they brought ANSI cursor movement support in that version.
# For the sys.stdout file-like object
import sys
import platform
if platform.system() == 'Windows':
try:
import colorama
except ImportError:
import ctypes
kernel32 = ctypes.windll.kernel32
# Enable ANSI support on Windows 10 v1511
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
else:
colorama.init()
else:
# Fix Linux arrow key support in Python scripts
import readline
class TempHistory:
"""Record one line from the terminal.
It is necessary to keep track of the last line on the terminal so we
can move the text cursor rightward and upward back into the position
before the newline from the `input` function was echoed.
Note: I use the term 'echo' to refer to when text is
shown on the terminal but might not be written to `sys.stdout`.
"""
def __init__(self):
"""Initialise `line` and save the `print` and `input` functions.
`line` is initially set to '\n' so that the `record` method
doesn't raise an error about the string index being out of range.
"""
self.line = '\n'
self.builtin_print = print
self.builtin_input = input
def _record(self, text):
"""Append to `line` or overwrite it if it has ended."""
if text == '':
# You can't record nothing
return
# Take into account `text` being multiple lines
lines = text.split('\n')
if text[-1] == '\n':
last_line = lines[-2] + '\n'
# If `text` ended with a newline, then `text.split('\n')[-1]`
# would have merely returned the newline, and not the text
# preceding it
else:
last_line = lines[-1]
# Take into account return characters which overwrite the line
last_line = last_line.split('\r')[-1]
# `line` is considered ended if it ends with a newline character
if self.line[-1] == '\n':
self.line = last_line
else:
self.line += last_line
def _undo_newline(self):
"""Move text cursor back to its position before echoing newline.
ANSI escape sequence: `\x1b[{count}{command}`
`\x1b` is the escape code, and commands `A`, `B`, `C` and `D` are
for moving the text cursor up, down, forward and backward {count}
times respectively.
Thus, after having echoed a newline, the final statement tells
the terminal to move the text cursor forward to be inline with
the end of the previous line, and then move up into said line
(making it the current line again).
"""
line_length = len(self.line)
# Take into account (multiple) backspaces which would
# otherwise artificially increase `line_length`
for i, char in enumerate(self.line[1:]):
if char == '\b' and self.line[i-1] != '\b':
line_length -= 2
self.print('\x1b[{}C\x1b[1A'.format(line_length),
end='', flush=True, record=False)
def print(self, *args, sep=' ', end='\n', file=sys.stdout, flush=False,
record=True):
"""Print to `file` and record the printed text.
Other than recording the printed text, it behaves exactly like
the built-in `print` function.
"""
self.builtin_print(*args, sep=sep, end=end, file=file, flush=flush)
if record:
text = sep.join([str(arg) for arg in args]) + end
self._record(text)
def input(self, prompt='', newline=True, record=True):
"""Return one line of user input and record the echoed text.
Other than storing the echoed text and optionally stripping the
echoed newline, it behaves exactly like the built-in `input`
function.
"""
if prompt == '':
# Prevent arrow key overwriting previously printed text by
# ensuring the built-in `input` function's `prompt` argument
# isn't empty
prompt = ' \b'
response = self.builtin_input(prompt)
if record:
self._record(prompt)
self._record(response)
if not newline:
self._undo_newline()
return response
record = TempHistory()
# For convenience
print = record.print
input = record.input
print('Hello, ', end='', flush=True)
name = input(newline=False)
print(', how do you do?)
As already answered, we can't stop input() from writing a newline. Though it may not satisfy your expectation, somehow the following codes satisfy the condition if -
you don't have any issue clearing the screen
import os
name = input("Hello, ")
os.system("cls") # on linux or mac, use "clear"
print(f"Hello, {name}, how do you do?")
or no issue using the gui dialog box, as dialog box disappears after taking user input, you will see exactly what you expected
import easygui
name = easygui.enterbox("Hello, ", "User Name")
print("Hello, "+name+", how do you do?")
I think you can use this:
name = input("Hello , ")
It should be something like this:-
print('this eliminates the ', end=' ')
print('new line')
The output is this:-
this eliminates the new line.
Related
I'm creating a command-line calculator tool, and I'd like to get it to format the user's input as they type, similar to what Fish and Powershell do (screenshots).
Currently, the only easy method for doing this that I can think of is getting characters one at a time, and then reprinting the formatted line to the screen.
# This code is probably very dodgy,
# it's just to give you an idea what I'm thinking
# Use external getch module to get input without echoing to screen
from getch import getch
inp = ""
while True:
char = getch()
if char == '\n': break
if char == '\b': inp = inp[:-1]
else: inp += char
# Print with carriage return so that next input overwrites it
print(colourFormatInput(inp) + '\r')
# Outside loop: process input and devliver results
Whilst this would techincally work, I feel like it is a ton of manual effort, and would only become more complex if I wanted to add functionality such as using arrow keys to move the cursor's position.
Is there a simple way to get this kind of functionality without having to code all of it up myself?
I use the library colorama.
import colorama
import sys
def hook(tp, *args):
if tp is KeyboardInterrupt:
print(colorama.Fore.RESET)
exit()
def colored_input(text: str, color):
sys.excepthook = hook
inp = input(text + color)
print(colorama.Fore.RESET, end="", flush=True)
sys.excepthook = sys.__excepthook__
return inp
name = colored_input("What's your name? ", colorama.Fore.RED)
age = colored_input("What's your age? ", colorama.Fore.YELLOW)
print(f"Nice to meet you {name}({age})")
I use sys.excepthook to catch the KeyboardInterrupt so I can reset the color back when the user types CTRL+C, and then I set the original excepthook back (sys.__excepthook__)
You can do this with prompt_toolkit
here is the documentation if you need:
Getting Started
Asking for input/prompts
You can add Syntax Highlighting to input by the following as given in examples:
Adding syntax highlighting is as simple as adding a lexer. All of the Pygments lexers can be used after wrapping them in a PygmentsLexer. It is also possible to create a custom lexer by implementing the Lexer abstract base class.
from pygments.lexers.html import HtmlLexer
from prompt_toolkit.shortcuts import prompt
from prompt_toolkit.lexers import PygmentsLexer
text = prompt('Enter HTML: ', lexer=PygmentsLexer(HtmlLexer))
print('You said: %s' % text)
In the same way as above you can create custom prompt_toolkit.lexers.Lexer for calculator highlighting just like the following example. Here I create a custom helper class:
from typing import Callable
from prompt_toolkit.document import Document
from prompt_toolkit.formatted_text.base import StyleAndTextTuples
from prompt_toolkit.formatted_text import FormattedText
from prompt_toolkit.shortcuts import prompt
import prompt_toolkit.lexers
import re
class CustomRegexLexer(prompt_toolkit.lexers.Lexer):
def __init__(self, regex_mapping):
super().__init__()
self.regex_mapping = regex_mapping
def lex_document(self, document: Document) -> Callable[[int], StyleAndTextTuples]:
def lex(_: int):
line = document.text
tokens = []
while len(line) != 0:
for pattern, style_string in self.regex_mapping.items():
match: re.Match = pattern.search(line)
if not match:
continue
else:
# print(f"found_match: {match}")
pass
match_string = line[:match.span()[1]]
line = line[match.span()[1]:]
tokens.append((style_string, match_string))
break
return tokens
return lex
Now with the above helper class implemented we can create our regex patterns and their respective styles, to learn more about what you can have in styling string go to this page
# Making regex for different operators. Make sure you add `^` anchor
# to the start of all the patterns
operators_allowed = ["+", "-", "/", "*", "(", ")", "=", "^"]
operators = re.compile("^["+''.join([f"\\{x}" for x in operators_allowed])+"]")
numbers = re.compile(r"^\d+(\.\d+)?")
text = re.compile(r"^.")
regex_mapping = {
operators: "#ff70e5", # Change colors according to your requirement
numbers: "#ffa500",
text: "#2ef5ff",
}
MyCalculatorLexer = CustomRegexLexer(regex_mapping)
With the lexers created you can now use the lexer in the function prompt:
text = prompt("Enter Equation: ", lexer=MyCalculatorLexer)
# Or
def input_maths(message):
return prompt(message, lexer=MyCalculatorLexer)
text = input_maths("Enter Equation: ")
Here is some example output:
And now everything works. Also do check out prompt_toolkit, you can
create tons of custom things as shown in there gallery
Another example:
CGREEN = '\33[32m'
CYELLOW = '\33[33m'
CBLUE = '\33[34m'
CVIOLET = '\33[35m'
CBEIGE = '\33[36m'
CWHITE = '\33[37m'
CGREY = '\33[90m'
CRED = '\033[91m'
CYELLOW = '\33[33m'
CYELLOW2 = '\33[93m'
CEND = '\033[0m'
print(CGREEN + "This is green text" + CEND)
print(CYELLOW + "This is yellow text" + CEND)
# Another interesting example (courtesy: https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html)
for i in range(0, 16):
for j in range(0, 16):
code = str(i * 16 + j)
colorCode = u"\u001b[48;5;" + code + "m"
print(colorCode + " Color {} ".format(code) + CEND)
No, I'm not done with my interpreter questions yet. Sorry.
So I've written another interpreter for a programming language I made up in Python. The programming language has an interesting concept: you only get some basic commands. To "get" more complex commands, you have to write functions and combine the simple command given to you.
Anyways, I've written a simple interpreter for the language.
The problem is: the simple commands work, but defining functions doesn't.
Here's the interpreter (I've removed code that isn't useful for solving the problem).
class Interpreter:
acc = 0
defined = {}
def read(self, cmd):
tokens = cmd.replace("\n", ";").split(";")
for token in tokens:
self.parse(token)
def parse(self, cmd):
if cmd.startswith("def(") and cmd.endswith(")"):
cmd = cmd[2:]
cmd = cmd[:-1]
cmd = cmd.split("|")
self.defined[cmd[0]] = cmd[1]
elif cmd in self.defined:
self.read(self.defined[cmd])
elif cmd == "1":
self.acc += 1
elif cmd == "2":
print(self.acc)
elif cmd == "i":
self.acc = int(input(">> "))
i = Interpreter()
while 1:
i.read(input("> "))
You can define functions using the syntax def(name|code). For example, def(cat|i;2).
Now, onto the problem I'm having. It's impossible to define functions. They just don't work. It doesn't throw an error or anything. It does nothing.
Here's the code I tried to use:
def(c|i;2)
c
It should get input and display it, but instead, it doesn't do anything.
This works, though:
i;2
It seems to me like the problem is somewhere in the if cmd.startswith("def(") if statement, because everything except the functions work.
When solving these kinds of problems it is imperative to be able to see what is going on while the program is running. You can e.g. use a debugger or you can go with the age old debug print method (like I've done below).
I've extended the interpreter with a p command that prints the acc, and made it accept any integer, otherwise it is the same.
The problem you're having is that you destroy the input before you can store it in defined. I solve it by only using \n to split outer commands and ; to split commands inside a def.
import textwrap
class Interpreter:
acc = 0
defined = {}
def read(self, cmd):
cmd = textwrap.dedent(cmd).strip()
lines = cmd.split("\n")
for line in lines:
print '==> parsing:', line
self.parse(line)
def parse(self, cmd):
if cmd.startswith("def(") and cmd.endswith(")"):
print '::found def',
name, code = cmd[4:-1].split('|') # def( is 4 characters..
self.defined[name] = code.replace(';', '\n') # read() expects commands divided by \n, so replace ; before storing in self.defined
print self.defined
elif cmd in self.defined:
print '::found defined name', cmd, '=>', `self.defined[cmd]`
self.read(self.defined[cmd])
elif cmd == "i":
self.acc = int(input(">> "))
elif cmd == "p":
print(self.acc)
else:
self.acc += int(cmd)
intp = Interpreter()
intp.read("""
def(c|i;2)
c
p
""")
the output from a run:
(dev) go|c:\srv\tmp> python pars.py
==> parsing: def(c|i;2)
::found def {'c': 'i\n2'}
==> parsing: c
::found defined name c => 'i\n2'
==> parsing: i
>> 5
==> parsing: 2
==> parsing: p
7
writing an interpreter that recursively calls itself in this way has some major limitations since every function call in the compiled language requires a function call in the host language (Python). A better way is to transform the program into a stack of commands, then pop a command from the stack and execute it. You're done when the stack is empty. Function calls will then just involve pushing the value of the defined symbol onto the stack. I've extended your interpreter to do this below. I've added a command x0 which will exit a function call if acc is zero (and I push a $marker onto the stack before calling a function so I know where the function call started):
def debug(*args):
pass
# print '[dbg]', ' '.join(str(a) for a in args)
class Interpreter:
acc = 0
defined = {}
commands = [] # the stack
def compile(self, program):
program = textwrap.dedent(program).strip()
lines = program.split("\n")
lines.reverse()
self.commands += lines
while self.commands:
command = self.commands.pop()
debug('==> running:', command, 'stack:', self.commands)
self.run_command(command)
def run_command(self, cmd):
if cmd.startswith("def(") and cmd.endswith(")"):
name, code = cmd[4:-1].split('|')
self.defined[name] = code.split(';')
debug('::found def', self.defined)
elif cmd in self.defined:
debug('::found defined name', cmd, '=>', `self.defined[cmd]`)
# mark command stack before executing function
self.commands += ['$marker']
self.commands += list(reversed(self.defined[cmd]))
elif cmd == '$marker':
pass # do nothing (we get here if a def doesn't have an x0 when the acc is zero)
elif cmd == 'x0':
# exit function call if acc is zero
if self.acc == 0:
while self.commands: # pop the stack until we get to the $marker
tmp = self.commands.pop()
if tmp == '$marker':
break
elif cmd == "i":
self.acc = int(input(">> "))
elif cmd == "p":
print(self.acc)
else:
self.acc += int(cmd)
we can now write recursive functions:
intp = Interpreter()
intp.compile("""
4
def(c|-1;x0;p;c)
c
p
""")
which outputs:
(dev) go|c:\srv\tmp> python pars.py
3
2
1
0
instead of an accumulator (acc) it is probably more expressive to use the stack for values too, so e.g. 5;p would push 5 on the stack, then p would print the top element on the stack. Then you could implement addition like 5;2;+ meaning push 5, push 2, and let + mean add top two items on stack and push the result... I'll leave that as an excercise ;-)
I'd like to be able to read data from an input file in Python, similar to the way that Fortran handles a list-directed read (i.e. read (file, *) char_var, float_var, int_var).
The tricky part is that the way Fortran handles a read statement like this is very "forgiving" as far as the input format is concerned. For example, using the previous statement, this:
"some string" 10.0, 5
would be read the same as:
"some string", 10.0
5
and this:
"other string", 15.0 /
is read the same as:
"other string"
15
/
with the value of int_var retaining the same value as before the read statement. And trickier still this:
"nother string", , 7
will assign the values to char_var and int_var but float_var retains the same value as before the read statement.
Is there an elegant way to implement this?
That is indeed tricky - I found it easier to write a pure-python stated-based tokenizer than think on a regular expression to parse each line (tough it is possible).
I've used the link provided by Vladimir as the spec - the tokenizer have some doctests that pass.
def tokenize(line, separator=',', whitespace="\t\n\x20", quote='"'):
"""
>>> tokenize('"some string" 10.0, 5')
['some string', '10.0', '5']
>>> tokenize(' "other string", 15.0 /')
['other string', '15.0', '/']
>>> tokenize('"nother string", , 7')
['nother string', '', '7']
"""
inside_str = False
token_started = False
token = ""
tokens = []
separated = False
just_added = False
for char in line:
if char in quote:
if not inside_str:
inside_str = True
else:
inside_str = False
tokens.append(token)
token = ""
just_added = True
continue
if char in (whitespace + separator) and not inside_str:
if token:
tokens.append(token)
token = ""
just_added = True
elif char in separator:
if not just_added:
tokens.append("")
just_added = False
continue
token += char
if token:
tokens.append(token)
return tokens
class Character(object):
def __init__(self, length=None):
self.length = length
def __call__(self, text):
if self.length is None:
return text
if len(text) > self.length:
return text[:self.length]
return "{{:{}}}".format(self.length).format(text)
def make_types(types, default_value):
return types, [default_value] * len[types]
def fortran_reader(file, types, default_char="/", default_value=None, **kw):
types, results = make_types(types, default_value)
tokens = []
while True:
tokens = []
while len(tokens) < len(results):
try:
line = next(file)
except StopIteration:
raise StopIteration
tokens += tokenize(line, **kw)
for i, (type_, token) in enumerate(zip(types, tokens)):
if not token or token in default_char:
continue
results[i] = type_(token)
changed_types = yield(results)
if changed_types:
types, results = make_types(changed_types)
I have not teste this thoughtfully - but for the tokenizer -
it is designed to work in a Python forstatement if the same fields are repeated over and over again - or it can be used with Python's iterators send method to change the values to be read on each iteration.
Please test, and e-mail me (address at my profile) some testing file. If there is indeed nothing similar, maybe this deserves some polishing and be published in Pypi.
Since I was not able to find a solution to this problem, I decided to write my own solution.
The main drivers are a reader class, and a tokenizer. The reader gets one line at a time from the file, passes it to the tokenizer, and assigns to the variables it is given, getting the next line as necessary.
class FortranAsciiReader(file):
def read(self, *args):
"""
Read from file into the given objects
"""
num_args = len(args)
num_read = 0
encountered_slash = False
# If line contained '/' or read into all varialbes, we're done
while num_read < num_args and not encountered_slash:
line = self.readline()
if not line:
raise Exception()
values = tokenize(line)
# Assign elements one-by-one into args, skipping empty fields and stopping at a '/'
for val in values:
if val == '/':
encountered_slash = True
break
elif val == '':
num_read += 1
else:
args[num_read].assign(val)
num_read += 1
if num_read == num_args:
break
The tokenizer splits the line into tokens in accordance with the way that Fortran performs list directed reads, where ',' and white space are separators, tokens may be "repeated" via 4*token, and a / terminates input.
My implementation of the tokenizer is a bit long to reproduce here, and I also included classes to transparently provide the functionality of the basic Fortran intrinsic types (i.e. Real, Character, Integer, etc.). The whole project can be found on my github account, currently at https://github.com/bprichar/PyLiDiRe. Thanks jsbueno for inspiration for the tokenizer.
This may be an xy problem, but I'm trying to to build a kernel based text editor, similar to vim or nano, and I know how to use the escape chars to clear the screen, then reprint, I can have it accept characters, but I'm not sure how to get it to accept arrow inputs for navigation. I thought there were ASCII values for them, but apparently not. Is there a way to use the arrows, or do I have to make a navigation mode and insert mode like vim?
I've also briefly played with curses, but that was prohibitive because, as I understood, a whole new window had to be opened for it and this is not compatible with the vision of a single terminal window that I had.
Please note that curses does not work because it cleares the window, which I don't want.
curses is exactly what you want. In fact I believe vim implements its interface with curses.
Try to put the following code into a file called test_curses.py:
import curses
screen = curses.initscr()
screen.addstr("Hello World!!!")
screen.refresh()
screen.getch()
curses.endwin()
Now open a terminal (not IDLE! a real terminal!) and run it via:
python test_curses.py
You should see that the terminal was cleared and an Hello World!!! writing appeared. Press any key and the program will stop, restoring the old terminal contents.
Note that the curses library isn't as easy and "user-friendly" as you may be accustomed to. I suggest reading the tutorial (unfortunately for the C language, but the python interface is mostly the same)
I wound up using the code from this question, and modifying the __init__ statement so that it accepted up to 3 characters in a list.
import sys
class _Getch:
"""Gets a single character from standard input. Does not echo to the
screen."""
def __init__(self):
self.impl = _GetchUnix()
def __call__(self):# return self.impl()
charlist = []
counter = 0
for i in range(3):
try:charlist.append(self.impl())
except:pass
if charlist[i] not in [chr(27),chr(91)]:#TODO sort out escape vs arrow duh use len()
break
if len(charlist) > 1:
if charlist == [chr(27),chr(27)]:
break
if len(charlist) == 3:
if charlist[2] == 'a'
return 'u-arr'
if charlist[2] == 'b'
return 'd-arr'
if charlist[2] == 'c'
return 'r-arr'
if charlist[2] == 'd'
return 'l-arr'
if len(charlist == 2):
if charlist == [chr(27),chr(27)]
return chr(27)
if len(charlist == 1)
return charlist[0]
return ''
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
This allowed me to get arrow keys as well as all other characters and escape sequences from the keyboard for the editor. It made the "Getch" class not strictly a get char clone because it returns a string, but it wound up being much more useful.
I know that I'm late to the party, but I really liked click package mentioned by #elbaschid. I don't know why he wasn't upvoted - maybe because his example doesn't show how to handle specifically cursor keys.
Here is my $0.02 on that:
#!/usr/bin/python
import click
printable = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?#[\\]^_`{|}~'
while True:
click.echo('Continue? [yn] ', nl=False)
c = click.getchar()
click.echo()
if c == 'y':
click.echo('We will go on')
elif c == 'n':
click.echo('Abort!')
break
elif c == '\x1b[D':
click.echo('Left arrow <-')
elif c == '\x1b[C':
click.echo('Right arrow ->')
else:
click.echo('Invalid input :(')
click.echo('You pressed: "' + ''.join([ '\\'+hex(ord(i))[1:] if i not in printable else i for i in c ]) +'"' )
This handles cursor keys and as a bonus prints py-string representation of any keyboard shortcut it doesn't yet recognize. For example Ctrl-s is "\x13". You can later use it inside another
elif c == ??
I've tried to add edit to #elbaschid answer but it was rejected ¯\_(ツ)_/¯. Please give him credit if you also like my answer
Awesome library for quick command-line prototyping.
The Python package click used for building commandline clients also comes with an implementation that allows you to get the key press events:
import click
key = click.getchar()
It returns the key representation as Unicode character and "things like arrow keys will show up in the platform’s native escape format.".
This is the example taken straight from the click documentation on getchar:
import click
click.echo('Continue? [yn] ', nl=False)
c = click.getchar()
click.echo()
if c == 'y':
click.echo('We will go on')
elif c == 'n':
click.echo('Abort!')
else:
click.echo('Invalid input :(')
To Perform desired Action on Arrow key or Any other key as it pressed
# key_event_handler.py
import sys
import select
import pty
import os
import time
import fcntl
import tty
import termios
def __select( iwtd, owtd, ewtd, timeout=None):
'''This is a wrapper around select.select() that ignores signals. If
select.select raises a select.error exception and errno is an EINTR
error then it is ignored. Mainly this is used to ignore sigwinch
(terminal resize). '''
# if select() is interrupted by a signal (errno==EINTR) then
# we loop back and enter the select() again.
if timeout is not None:
end_time = time.time() + timeout
while True:
try:
return select.select(iwtd, owtd, ewtd, timeout)
except select.error:
err = sys.exc_info()[1]
if err.args[0] == errno.EINTR:
# if we loop back we have to subtract the
# amount of time we already waited.
if timeout is not None:
timeout = end_time - time.time()
if timeout < 0:
return([], [], [])
else:
# something else caused the select.error, so
# this actually is an exception.
raise
STDIN_FILENO=pty.STDIN_FILENO
STDOUT_FILENO=pty.STDOUT_FILENO
string_type=bytes
sys.stdout.write(string_type())
sys.stdout.flush()
buffer = string_type()
mode = tty.tcgetattr(STDIN_FILENO)
tty.setraw(STDIN_FILENO)
try:
while True:
r, w, e = __select([STDIN_FILENO], [], [],timeout=1)
if STDIN_FILENO in r:
#It accepts all keys from keyboard
data=os.read(STDIN_FILENO, 1)
#Bellow line returns ASCII value of a charector
ascii_value=ord(data[0])
##########################################################################
## Your code goes here ##
## ##
# Do some action here by matching the ASCII value #
# you can handle your program by making use of special keys like #
# Backspace, Ctrl, Ctrl+A,Ctrl+B, Ctrl+C, ...Ctrl+Z, Esc,F1, ...,F12 ....#
# Tab,Enter,Arrow keys,Alphabetic and Numeric keys are also supported #
##########################################################################
# #
#To Print use bellow line rather than print or sys.stdout.write(data) #
#os.write(STDOUT_FILENO,data) #
## #
##########################################################################
finally:
tty.tcsetattr(STDIN_FILENO, tty.TCSAFLUSH, mode)
Then open terminal and run key_event_handler.py
This program is mainly to capture key pressed and get ascii of key pressed, This program can also be used for non-Blocking I/O in multy threaded aplications
I have snippit of Python code that AUTOMATICALLY detects one of many parameters that an external program is sending.
In this case, the parameter name is date-sent
__name__="__main__"
import sys, os, traceback
import commands
# Switch this to 0 when in production mode.
debugMode = 1
def main(args):
try:
attributeMap = parseInput(args)
dateSent = attributeMap["date-sent"]
print "Script-attribute=script value"
return
except:
error()
print "something went wrong!"
return "something went wrong!"
def parseInput(args):
attributeMap = {}
delimiter = "="
for item in args:
if delimiter in item:
tuple = item.split(delimiter)
attributeMap[tuple[0]] = tuple[1]
return attributeMap
def error():
# "SCRIPT PROCESSING ERROR"
if(debugMode):
traceback.print_exc(file=sys.stdout)
return ""
#-----------------------------------------------------------------
# DOS-style shells (for DOS, NT, OS/2):
#-----------------------------------------------------------------
def getstatusoutput(cmd):
""" Return (status, output) of executing cmd in a
shell."""
pipe = os.popen(cmd + ' 2>&1', 'r')
text = pipe.read()
sts = pipe.close()
if sts is None: sts = 0
if text[-1:] == '\n': text = text[:-1]
return sts, text
#-----------------------------------------------------------------
# Entry Point
#-----------------------------------------------------------------
if __name__ == "__main__":
if(len(sys.argv) == 0):
error()
else:
main(sys.argv)
How to do this in powershell, i.e. an external program that I have no control over send in many variables, i.e. sent-data, sender-name, sender-up, etc. How do I make my program detect only sent-data, just like this Python code did.
You use $args in Powershell, similar to sys.argv used in the Python example.
Read more here - http://technet.microsoft.com/en-us/library/ff730958.aspx