<s> import commands <EOL> from datetime import datetime <EOL> import functools <EOL> from time import mktime , sleep , time <EOL> import pytz <EOL> def format_time ( seconds ) : <EOL> """<STR_LIT>""" <EOL> dt = datetime . fromtimestamp ( seconds , pytz . utc ) <EOL> formatted_dt = dt . strftime ( "<STR_LIT>" ) <EOL> return "<STR_LIT::>" . join ( [ formatted_dt [ : - <NUM_LIT:2> ] , formatted_dt [ - <NUM_LIT:2> : ] ] ) <EOL> def datetime_to_millis ( dt ) : <EOL> return mktime ( dt . timetuple ( ) ) * <NUM_LIT> + dt . microsecond / <NUM_LIT> <EOL> def wait_until ( cond_func , timeout = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> start_time = time ( ) <EOL> while not cond_func ( ) : <EOL> if time ( ) - start_time > timeout : <EOL> raise Exception ( "<STR_LIT>" . format ( timeout ) ) <EOL> sleep ( <NUM_LIT:0.1> ) <EOL> def cmd_exists ( cmd ) : <EOL> """<STR_LIT>""" <EOL> ( status , output ) = commands . getstatusoutput ( "<STR_LIT>" . format ( cmd ) ) <EOL> return status == <NUM_LIT:0> <EOL> class memoize ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . func = func <EOL> self . memoized = { } <EOL> self . method_cache = { } <EOL> def __call__ ( self , * args ) : <EOL> return self . cache_get ( self . memoized , args , <EOL> lambda : self . func ( * args ) ) <EOL> def __get__ ( self , obj , objtype ) : <EOL> return self . cache_get ( self . method_cache , obj , <EOL> lambda : self . __class__ ( functools . partial ( self . func , obj ) ) ) <EOL> def cache_get ( self , cache , key , func ) : <EOL> try : <EOL> return cache [ key ] <EOL> except KeyError : <EOL> cache [ key ] = func ( ) <EOL> return cache [ key ] <EOL> def running_avg ( ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> total = <NUM_LIT:0.0> <EOL> while True : <EOL> total += ( yield ) <EOL> count += <NUM_LIT:1> <EOL> yield total / count </s>
