[SciPy-user] going through a lot of plots

Zachary Pincus zachary.pincus at yale.edu
Wed May 13 21:27:21 EDT 2009


>> I find myself in this situation a lot: I'm looking at a sequence of
>> plots, one for each piece of data in a collection. I usually find
>> myself writing a loop with a plot command followed by raw_input()so
>> that I hit enter in the terminal window IPython session to move to  
>> the
>> next item.  I usually make this conditional so that I can process in
>> batch without looking at the plots if I choose.
>>
>> This has the effect of producing a newline in the terminal every time
>> I want to move on to the next plot, which is far from ideal,
>> especially in the situation where I'm not printing anything else in
>> that window.


Old-school alternative is to put the TTY into cbreak (aka "rare" mode,  
between "raw" and "cooked"), and capture a single key-hit. (Except  
that ^C still breaks, which is handy.) For windows, the C runtime has  
a similar getkey function.

Here's windows / posix code for that that I've assembled from various  
snippets online; note that the latter uses the well-known decorator  
module. I've also included an "iskeydown" function which I find useful  
in various situations...

Zach


import os

if os.name == 'nt':
   import msvcrt
   def getkey():
     c = msvcrt.getch()
     if c == '\x00' or c == '\xE0':    #functions keys
         msvcrt.getch()
     return c

   def iskeydown():
     return msvcrt.kbhit()

elif os.name == 'posix':
   import tty, sys, select
   import decorator

   @decorator
   def _in_cbreak(func, *args, **kws):
     fd = sys.stdin.fileno()
     old = tty.tcgetattr(fd)
     tty.setcbreak(fd, tty.TCSANOW)
     try:
       return func(*args, **kws)
     finally:
       tty.tcsetattr(fd, tty.TCSAFLUSH, old)

   @_in_cbreak
   def getkey():
     return sys.stdin.read(1)

   @_in_cbreak
   def iskeydown():
     if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
       return sys.stdin.read(1)
     else:
       return False



More information about the SciPy-User mailing list