Is there getchar in sage?
Is there a function in sage equivalent to the getchar function from C language?
answered 2012-04-13 00:15:16 +0100
This post is a wiki. Anyone with karma >750 is welcome to improve it.
What sort of thing are you trying to do? Maybe there is a more natural way in python than getchar...
To get a user string in python, often the raw_input function is used.
answered 2012-04-13 16:11:36 +0100
This post is a wiki. Anyone with karma >750 is welcome to improve it.
Modify to your tastes:
def key_pressed(self):
"""
Tests whether we received a key press without blocking.
This works by making stdin temporarily non-blocking, and then
reading a single char.
"""
import fcntl
fl = fcntl.fcntl(sys.stdin, fcntl.F_GETFL)
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fl | os.O_NONBLOCK)
try:
key = os.read(sys.stdin.fileno(), 1)
except OSError:
return False
finally:
fcntl.fcntl(sys.stdin, fcntl.F_SETFL, fl)
print 'User pressed key '+key
return True
You really want this function in Python, I guess. Here are two relevant links I found, knowing nothing about getchar
; I bet you will be able to do better.
Apparently it's nontrivial to get something that works exactly the same.
Please start posting anonymously - your entry will be published after you log in or create a new account.
Asked: 2012-04-12 14:31:59 +0100
Seen: 441 times
Last updated: Apr 13 '12