<s> """<STR_LIT>""" <EOL> from subprocess import Popen , PIPE , STDOUT <EOL> from time import time , sleep <EOL> RATELIMIT = ( <NUM_LIT:1> / <NUM_LIT> , <NUM_LIT:0.1> ) <EOL> class TimerTimeout ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Timer ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , message = '<STR_LIT>' , rl_min_dur = <NUM_LIT:0> , <EOL> indent = <NUM_LIT:0> , timeout = None , quiet = False ) : <EOL> """<STR_LIT>""" <EOL> self . start = time ( ) <EOL> self . last = self . start <EOL> self . message = '<STR_LIT:U+0020>' * indent + message <EOL> self . quiet = quiet <EOL> self . min_dur = rl_min_dur <EOL> self . timeout = timeout <EOL> assert ( RATELIMIT and RATELIMIT [ <NUM_LIT:0> ] ) <EOL> def step ( self ) : <EOL> """<STR_LIT>""" <EOL> now = time ( ) <EOL> if self . timeout and now - self . start > self . timeout : <EOL> raise TimerTimeout <EOL> if ( not self . min_dur or now - self . start > self . min_dur ) and now - self . last > RATELIMIT [ <NUM_LIT:0> ] : <EOL> sleep ( RATELIMIT [ <NUM_LIT:1> ] ) <EOL> self . last = now <EOL> def check_expired ( self ) : <EOL> """<STR_LIT>""" <EOL> now = time ( ) <EOL> if self . timeout and now - self . start > self . timeout : <EOL> raise TimerTimeout <EOL> def time ( self ) : <EOL> """<STR_LIT>""" <EOL> return time ( ) - self . start <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % self . time ( ) <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> if self . quiet : <EOL> pass <EOL> elif not exc_type and not exc_value and not traceback : <EOL> print self . message % self <EOL> else : <EOL> print '<STR_LIT>' % self <EOL> def open_compressed ( filename = None , fileobj = None , mode = '<STR_LIT:r>' , <EOL> compressor = ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> if filename and not fileobj : <EOL> fileobj = open ( filename , mode ) <EOL> elif filename or not fileobj : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if '<STR_LIT:r>' in mode : <EOL> return Popen ( tuple ( compressor ) + ( '<STR_LIT>' , ) , <EOL> stdin = fileobj , stdout = PIPE , stderr = STDOUT ) . stdout <EOL> elif '<STR_LIT:w>' in mode : <EOL> return Popen ( tuple ( compressor ) , stdin = PIPE , stdout = fileobj ) . stdin <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) </s>
