Ask Your Question
0

Is there getchar in sage?

asked 2012-04-12 14:31:59 +0200

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?

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
0

answered 2012-04-13 16:11:36 +0200

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
edit flag offensive delete link more
1

answered 2012-04-13 00:15:16 +0200

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.

edit flag offensive delete link more
0

answered 2012-04-12 15:10:27 +0200

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.

edit flag offensive delete link more

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: 2012-04-12 14:31:59 +0200

Seen: 371 times

Last updated: Apr 13 '12