<s> """<STR_LIT>""" <EOL> import time <EOL> import sys <EOL> import os <EOL> from io import UnsupportedOperation <EOL> class Prog ( ) : <EOL> def __init__ ( self , iterations , track_time , stream , title , <EOL> monitor , update_interval = None ) : <EOL> """<STR_LIT>""" <EOL> self . cnt = <NUM_LIT:0> <EOL> self . title = title <EOL> self . max_iter = float ( iterations ) <EOL> self . track = track_time <EOL> self . start = time . time ( ) <EOL> self . end = None <EOL> self . item_id = None <EOL> self . eta = None <EOL> self . total_time = <NUM_LIT:0.0> <EOL> self . last_time = self . start <EOL> self . monitor = monitor <EOL> self . stream = stream <EOL> self . active = True <EOL> self . _stream_out = self . _no_stream <EOL> self . _stream_flush = self . _no_stream <EOL> self . _check_stream ( ) <EOL> self . _print_title ( ) <EOL> self . update_interval = update_interval <EOL> if self . monitor : <EOL> import psutil <EOL> self . process = psutil . Process ( ) <EOL> if self . track : <EOL> self . eta = <NUM_LIT:1> <EOL> def update ( self , iterations = <NUM_LIT:1> , item_id = None , force_flush = False ) : <EOL> """<STR_LIT>""" <EOL> self . item_id = item_id <EOL> self . cnt += iterations <EOL> self . _print ( force_flush = force_flush ) <EOL> self . _finish ( ) <EOL> def stop ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cnt = self . max_iter <EOL> self . _finish ( ) <EOL> def _check_stream ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . stream : <EOL> try : <EOL> if self . stream == <NUM_LIT:1> and os . isatty ( sys . stdout . fileno ( ) ) : <EOL> self . _stream_out = sys . stdout . write <EOL> self . _stream_flush = sys . stdout . flush <EOL> elif self . stream == <NUM_LIT:2> and os . isatty ( sys . stderr . fileno ( ) ) : <EOL> self . _stream_out = sys . stderr . write <EOL> self . _stream_flush = sys . stderr . flush <EOL> except UnsupportedOperation : <EOL> if self . stream == <NUM_LIT:1> : <EOL> self . _stream_out = sys . stdout . write <EOL> self . _stream_flush = sys . stdout . flush <EOL> elif self . stream == <NUM_LIT:2> : <EOL> self . _stream_out = sys . stderr . write <EOL> self . _stream_flush = sys . stderr . flush <EOL> else : <EOL> if self . stream is not None and hasattr ( self . stream , '<STR_LIT>' ) : <EOL> self . _stream_out = self . stream . write <EOL> self . _stream_flush = self . stream . flush <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> def _elapsed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . last_time = time . time ( ) <EOL> return self . last_time - self . start <EOL> def _calc_eta ( self ) : <EOL> """<STR_LIT>""" <EOL> elapsed = self . _elapsed ( ) <EOL> if self . cnt == <NUM_LIT:0> or elapsed < <NUM_LIT> : <EOL> return None <EOL> rate = float ( self . cnt ) / elapsed <EOL> self . eta = ( float ( self . max_iter ) - float ( self . cnt ) ) / rate <EOL> def _calc_percent ( self ) : <EOL> """<STR_LIT>""" <EOL> return round ( self . cnt / self . max_iter * <NUM_LIT:100> , <NUM_LIT:2> ) <EOL> def _no_stream ( self , text = None ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def _get_time ( self , _time ) : <EOL> if ( _time < <NUM_LIT> ) : <EOL> return time . strftime ( "<STR_LIT>" , time . gmtime ( _time ) ) <EOL> else : <EOL> s = ( str ( int ( _time // <NUM_LIT> ) ) + '<STR_LIT::>' + <EOL> time . strftime ( "<STR_LIT>" , time . gmtime ( _time ) ) ) <EOL> return s <EOL> def _finish ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . cnt >= self . max_iter : <EOL> self . total_time = self . _elapsed ( ) <EOL> self . end = time . time ( ) <EOL> self . last_progress -= <NUM_LIT:1> <EOL> self . _print ( ) <EOL> if self . track : <EOL> self . _stream_out ( '<STR_LIT>' + <EOL> self . _get_time ( self . total_time ) ) <EOL> self . _stream_out ( '<STR_LIT:\n>' ) <EOL> self . active = False <EOL> def _print_title ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . title : <EOL> self . _stream_out ( '<STR_LIT>' . format ( self . title ) ) <EOL> self . _stream_flush ( ) <EOL> def _print_eta ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _calc_eta ( ) <EOL> self . _stream_out ( '<STR_LIT>' + self . _get_time ( self . eta ) ) <EOL> self . _stream_flush ( ) <EOL> def _print_item_id ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _stream_out ( '<STR_LIT>' % self . item_id ) <EOL> self . _stream_flush ( ) <EOL> def __repr__ ( self ) : <EOL> str_start = time . strftime ( '<STR_LIT>' , <EOL> time . localtime ( self . start ) ) <EOL> str_end = time . strftime ( '<STR_LIT>' , <EOL> time . localtime ( self . end ) ) <EOL> self . _stream_flush ( ) <EOL> time_info = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' . format ( self . title , <EOL> str_start , <EOL> str_end ) + self . _get_time ( self . total_time ) <EOL> if self . monitor : <EOL> try : <EOL> cpu_total = self . process . cpu_percent ( ) <EOL> mem_total = self . process . memory_percent ( ) <EOL> except AttributeError : <EOL> cpu_total = self . process . get_cpu_percent ( ) <EOL> mem_total = self . process . get_memory_percent ( ) <EOL> cpu_mem_info = '<STR_LIT>' '<STR_LIT>' . format ( cpu_total , mem_total ) <EOL> return time_info + '<STR_LIT:\n>' + cpu_mem_info <EOL> else : <EOL> return time_info <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) </s>
