Advertisement
Guest User

Untitled

a guest
May 26th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import fcntl
  2. import os
  3. import select
  4. import sys
  5. import time
  6. import termios
  7. import tty
  8.  
  9. if not sys.stdin.isatty():
  10.     raise RuntimeError('consoleio on non-tty')
  11.  
  12. fd = sys.stdin.fileno()
  13. termstash = termios.tcgetattr(fd)
  14. tty.setraw(fd)
  15. flagstash = fcntl.fcntl(fd, fcntl.F_GETFL)
  16. fcntl.fcntl(fd, fcntl.F_SETFL, flagstash | os.O_NONBLOCK)
  17.  
  18. score = 0
  19. buf = b''
  20.  
  21. try:
  22.     t0 = time.time()
  23.     tend = t0 + 3
  24.     sys.stdout.buffer.raw.write(b'Mash digits as fast as you can')
  25.     while True:
  26.         tnow = time.time()
  27.         if tnow >= tend:
  28.             break
  29.         r, _, _ = select.select([sys.stdin.fileno()], [], [], tend - tnow)
  30.         if r:
  31.             ch = sys.stdin.buffer.raw.read(1)
  32.             buf += ch
  33.             score += sum(chr(c).isdigit() for c in ch)
  34. finally:
  35.     termios.tcsetattr(fd, termios.TCSANOW, termstash)
  36.     fcntl.fcntl(fd, fcntl.F_SETFL, flagstash)
  37.  
  38. print('\nScore:', score)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement