<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import time <EOL> import itertools <EOL> import select <EOL> import decimal <EOL> Infinity = decimal . Decimal ( '<STR_LIT>' ) <EOL> class Timer ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def decorate ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( callback ) : <EOL> return cls ( callback , * args , ** kwargs ) <EOL> return decorator <EOL> def __init__ ( self , callback , interval , repeat = False , <EOL> _time_function = time . time ) : <EOL> assert interval > <NUM_LIT:0> <EOL> self . _callback = callback <EOL> self . _interval = interval <EOL> self . _repeat = repeat <EOL> self . _now = _time_function <EOL> self . reset ( ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _expiry = self . _now ( ) + self . _interval <EOL> def cancel ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _expiry = None <EOL> def __call__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _callback ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _expiry is None : <EOL> return False <EOL> if self . _now ( ) < self . _expiry : <EOL> return True <EOL> if self . _repeat : <EOL> times = ( self . _now ( ) - self . _expiry ) // self . _interval + <NUM_LIT:1> <EOL> self . _expiry += times * self . _interval <EOL> else : <EOL> self . _expiry = None <EOL> self ( ) <EOL> return self . _expiry is not None <EOL> def sleep_time ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _expiry is None : <EOL> return Infinity <EOL> else : <EOL> return max ( self . _expiry - self . _now ( ) , <NUM_LIT:0> ) <EOL> class TimerManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , _time_function = time . time ) : <EOL> """<STR_LIT>""" <EOL> self . _timers = [ ] <EOL> self . _time_function = _time_function <EOL> def add_timer ( self , timeout , callback , repeat = False ) : <EOL> """<STR_LIT>""" <EOL> timer = Timer ( callback , timeout , repeat = repeat , <EOL> _time_function = self . _time_function ) <EOL> self . _timers . append ( timer ) <EOL> return timer <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _timers = [ timer for timer in self . _timers if timer . run ( ) ] <EOL> def sleep_time ( self ) : <EOL> """<STR_LIT>""" <EOL> return min ( itertools . chain ( <EOL> [ Infinity ] , <EOL> ( timer . sleep_time ( ) for timer in self . _timers ) <EOL> ) ) <EOL> class EventLoop ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _timers = TimerManager ( ) <EOL> self . _readers = { } <EOL> def add_timer ( self , timeout , repeat = False ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( callback ) : <EOL> return self . _timers . add_timer ( timeout , callback , repeat ) <EOL> return decorator <EOL> def watch_for_reading ( self , file_descriptor ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( file_descriptor , ( int , long ) ) : <EOL> file_descriptor = file_descriptor . fileno ( ) <EOL> def decorator ( callback ) : <EOL> self . _readers [ file_descriptor ] = callback <EOL> return callback <EOL> return decorator <EOL> def block_reader ( self , file_descriptor , max_block_size = <NUM_LIT:8> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( callback ) : <EOL> @ self . watch_for_reading ( file_descriptor ) <EOL> def reader ( fd ) : <EOL> data = os . read ( fd , max_block_size ) <EOL> callback ( data ) <EOL> return callback <EOL> return decorator <EOL> def push_back_reader ( self , file_descriptor , max_block_size = <NUM_LIT:8> * <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( callback ) : <EOL> pushed_back = [ ] <EOL> @ self . block_reader ( file_descriptor , max_block_size ) <EOL> def reader ( data ) : <EOL> if pushed_back : <EOL> pushed_back . append ( data ) <EOL> data = '<STR_LIT>' . join ( pushed_back ) <EOL> pushed_back [ : ] = [ ] <EOL> callback ( data , pushed_back . append ) <EOL> return callback <EOL> return decorator <EOL> def line_reader ( self , file_descriptor , max_block_size = <NUM_LIT:8> * <NUM_LIT> ) : <EOL> r"""<STR_LIT>""" <EOL> def decorator ( callback ) : <EOL> partial_line_fragments = [ ] <EOL> @ self . block_reader ( file_descriptor , max_block_size ) <EOL> def reader ( data ) : <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> end = data . index ( '<STR_LIT:\n>' ) <EOL> except ValueError : <EOL> break <EOL> else : <EOL> end += <NUM_LIT:1> <EOL> partial_line_fragments . append ( data [ : end ] ) <EOL> line = '<STR_LIT>' . join ( partial_line_fragments ) <EOL> partial_line_fragments [ : ] = [ ] <EOL> callback ( line ) <EOL> data = data [ end : ] <EOL> if data : <EOL> partial_line_fragments . append ( data ) <EOL> return callback <EOL> return decorator <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _running = True <EOL> while self . _running : <EOL> timeout = self . _timers . sleep_time ( ) <EOL> if timeout == Infinity : <EOL> timeout = None <EOL> if self . _readers : <EOL> ready , _ , _ = select . select ( <EOL> self . _readers . keys ( ) , [ ] , [ ] , timeout ) <EOL> else : <EOL> assert timeout is not None , '<STR_LIT>' <EOL> time . sleep ( timeout ) <EOL> ready = [ ] <EOL> self . _timers . run ( ) <EOL> for fd in ready : <EOL> self . _readers [ fd ] ( fd ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _running = False <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> loop = EventLoop ( ) <EOL> @ loop . add_timer ( <NUM_LIT:5> , repeat = True ) <EOL> def timeout ( ) : <EOL> print '<STR_LIT>' <EOL> loop . stop ( ) <EOL> @ loop . line_reader ( sys . stdin ) <EOL> def new_line ( line ) : <EOL> timeout . reset ( ) <EOL> print '<STR_LIT>' , line . strip ( ) <EOL> print '<STR_LIT>' <EOL> loop . run ( ) <EOL> print '<STR_LIT>' </s>
