Source code for freeflux.utils.progress

'''Define the Progress class.'''


[docs] __author__ = 'Chao Wu'
from time import time, sleep from datetime import timedelta from threading import Thread
[docs] class Progress(): ''' Progress instances can be either used in a WITH statement or assigned to a variable. In the second case, the start and stop method should be called explicitely. ''' def __init__(self, descp, silent = False): ''' Parameters ---------- descp: str Description. silent: bool Whether to show progress bar. '''
[docs] self.descp = descp
[docs] self.count = 0
[docs] self.is_running = False
[docs] self.silent = silent
[docs] def __enter__(self): self.start()
[docs] def __exit__(self, type, value, traceback): self.stop()
[docs] def _sleeptime(self): if self.descp == 'optimization': sleeptime = 0.1 elif self.descp == 'fitting with CIs': sleeptime = 10 else: sleeptime = 2 return sleeptime
[docs] def _show_progress(self, descp): t1 = time() while self.is_running: sleep(1) t2 = time() elapsed = timedelta(seconds = round(t2 - t1)) print(f'{descp} [elapsed: {elapsed}]', end = '\r') print('')
[docs] def start(self): if not self.silent: print('') self.is_running = True self.thread = Thread(target = self._show_progress, args = (self.descp,)) self.thread.start() else: pass
[docs] def stop(self): if not self.silent: self.is_running = False self.thread.join() else: pass