<s> """<STR_LIT>""" <EOL> import sys <EOL> import time <EOL> import threading <EOL> import collections <EOL> import numpy as np <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> def format_interval ( time_in_seconds ) : <EOL> """<STR_LIT>""" <EOL> mins , seconds = divmod ( int ( time_in_seconds ) , <NUM_LIT> ) <EOL> hours , minutes = divmod ( mins , <NUM_LIT> ) <EOL> if hours > <NUM_LIT:0> : <EOL> return '<STR_LIT>' . format ( hours , minutes , seconds ) <EOL> return '<STR_LIT>' . format ( minutes , seconds ) <EOL> def format_error ( error ) : <EOL> """<STR_LIT>""" <EOL> if error is None : <EOL> return '<STR_LIT:?>' <EOL> if isinstance ( error , collections . Iterable ) : <EOL> error = np . atleast_1d ( error ) . item ( <NUM_LIT:0> ) <EOL> return '<STR_LIT>' . format ( error ) <EOL> def format_meter ( n_finished , n_total , elapsed , error = None ) : <EOL> """<STR_LIT>""" <EOL> if n_finished > n_total : <EOL> n_total = None <EOL> elapsed_str = format_interval ( elapsed ) <EOL> if n_total is not None : <EOL> frac = float ( n_finished ) / n_total <EOL> n_bars = <NUM_LIT:10> <EOL> bar_length = int ( frac * n_bars ) <EOL> bar = '<STR_LIT:#>' * bar_length + '<STR_LIT:->' * ( n_bars - bar_length ) <EOL> percentage = '<STR_LIT>' % ( frac * <NUM_LIT:100> ) <EOL> left_str = format_interval ( <EOL> elapsed / n_finished * ( n_total - n_finished ) <EOL> ) if n_finished else '<STR_LIT:?>' <EOL> return '<STR_LIT>' . format ( <EOL> bar , n_finished , n_total , percentage , <EOL> elapsed_str , left_str , format_error ( error ) <EOL> ) <EOL> return '<STR_LIT>' . format ( n_finished , elapsed_str ) <EOL> class StatusPrinter ( object ) : <EOL> def __init__ ( self , file ) : <EOL> self . file = file <EOL> self . last_printed_len = <NUM_LIT:0> <EOL> def write ( self , text ) : <EOL> n_spaces = max ( self . last_printed_len - len ( text ) , <NUM_LIT:0> ) <EOL> self . file . write ( '<STR_LIT:\r>' + text + '<STR_LIT:U+0020>' * n_spaces ) <EOL> self . file . flush ( ) <EOL> self . last_printed_len = len ( text ) <EOL> def clean ( self ) : <EOL> self . write ( '<STR_LIT>' ) <EOL> self . file . write ( '<STR_LIT:\r>' ) <EOL> def progressbar ( iterable , desc = '<STR_LIT>' , total = None , file = sys . stderr , <EOL> mininterval = <NUM_LIT> , miniters = <NUM_LIT:1> , init_interval = <NUM_LIT:1.> ) : <EOL> """<STR_LIT>""" <EOL> if total is None : <EOL> try : <EOL> total = len ( iterable ) <EOL> except TypeError : <EOL> total = None <EOL> prefix = desc + '<STR_LIT>' if desc else '<STR_LIT>' <EOL> printer = StatusPrinter ( file ) <EOL> status = prefix + format_meter ( <NUM_LIT:0> , total , <NUM_LIT:0> ) <EOL> timer = threading . Timer ( init_interval , printer . write , args = [ status ] ) <EOL> timer . start ( ) <EOL> start_time = last_print_time = time . time ( ) <EOL> last_print_n = <NUM_LIT:0> <EOL> n = <NUM_LIT:0> <EOL> try : <EOL> for obj in iterable : <EOL> error = ( yield ) <EOL> yield obj <EOL> timer . cancel ( ) <EOL> n += <NUM_LIT:1> <EOL> if n - last_print_n >= miniters : <EOL> current_time = time . time ( ) <EOL> if ( current_time - last_print_time ) >= mininterval : <EOL> time_delta = current_time - start_time <EOL> formated_str = format_meter ( n , total , time_delta , error ) <EOL> printer . write ( prefix + formated_str ) <EOL> last_print_n = n <EOL> last_print_time = current_time <EOL> finally : <EOL> timer . cancel ( ) <EOL> printer . clean ( ) </s>
