Ask Your Question
0

Is there getchar in sage?

asked 13 years ago

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

Is there a function in sage equivalent to the getchar function from C language?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
1

answered 13 years ago

this post is marked as community wiki

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.

Preview: (hide)
link
0

answered 13 years ago

this post is marked as community wiki

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
Preview: (hide)
link
0

answered 13 years ago

kcrisman gravatar image

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.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 460 times

Last updated: Apr 13 '12