#!/usr/bin/env python import sys import time from decimal import Decimal import empty import base def f_out(f, s, tail='\n'): f.write(s + tail) f.flush() def out(s, tail='\n'): f_out(sys.stdout, s, tail) def err(s, tail='\n'): f_out(sys.stderr, s, tail) def out_exit(s, code=0): out(s) sys.exit(code) def err_exit(s, code=1): err(s) sys.exit(code) def help_exit(help, code=1): s = ' '.join( [ 'Usage:', sys.argv[0], help ] ) err_exit(s) def out_lst(lst, pre=''): out( base.to_str( lst, pre ) ) def msg_start(msg, show=True): if show: out( msg + ' ... ', '' ) def end(): if show: out( 'ok' ) return end start_save = lambda path, show=True: msg_start( 'save ' + path, show ) start_load = lambda path, show=True: msg_start( 'load ' + path, show ) quantize= lambda v, exp: Decimal( v ).quantize( Decimal( exp ) ) # Deciaml( '123.456789' ).quantize( Decimal( '0.001' ) ) # --> 123.456 def laptime_new(s=''): lst = [] def get(s=''): inf = empty.new( s=s, tm_sec=time.time(), sec=0, dsec=0 ) if lst: inf.sec = inf.tm_sec - lst[ 0 ].tm_sec inf.dsec = inf.sec - lst[ -1 ].sec return inf get_sec = lambda : get().sec def rec(s='', do_show=False): inf = get( s ) lst.append( inf ) if do_show: show( inf ) def show(inf=None): if inf: i = lst.index( inf ) if inf in lst else -1 exp = '0.001' sec = quantize( inf.sec, exp ) dsec = quantize( inf.dsec, exp ) s = '{} {} {} {}'.format( i, inf.s, sec, dsec ) out( s ) else: out( '----' ) for inf in lst: show( inf ) rec( s ) return empty.new( locals() ) # EOF