#!/usr/bin/env python# -*- coding: utf-8 -*-## This file is part of the `pypath` python module## Copyright 2014-2023# EMBL, EMBL-EBI, Uniklinik RWTH Aachen, Heidelberg University## Authors: see the file `README.rst`# Contact: Dénes Türei (turei.denes@gmail.com)## Distributed under the GPLv3 License.# See accompanying file LICENSE.txt or copy at# https://www.gnu.org/licenses/gpl-3.0.html## Website: https://pypath.omnipathdb.org/#importtimeimportosimportimportlibasimpimporttqdmimportpypath.share.settingsassettings__all__=['Progress']
[docs]classProgress(object):""" Before I had my custom progressbar here. Now it is a wrapper around the great progressbar `tqdm`. Old implementation moved to `OldProgress` class. """
[docs]defstep(self,step=1,msg=None,status='busy',force=False):""" Updates the progressbar by the desired number of steps. :param int step: Number of steps or items. """self.done+=stepifself.off:returnif(forceor(self.done%self.interval<1.0andtime.time()-self.last_updated>self.min_update_interval)):self.set_status(status)this_update=max(0,self.done-self.last_printed_value)ifthis_update==0:self.tqdm.refresh()self.tqdm.fp.flush()else:self.tqdm.update(int(this_update))self.last_printed_value=self.doneself.last_updated=time.time()
[docs]defterminate(self,status='finished'):""" Terminates the progressbar and destroys the tqdm object. """ifself.off:returnself.step(self.total-self.done,force=True,status=status)self.tqdm.close()
[docs]defset_total(self,total):""" Changes the total value of the progress bar. """ifself.off:returnself.total=totalself.tqdm.total=totalself.step(0)
[docs]defset_done(self,done):""" Sets the position of the progress bar. """ifself.off:returnself.done=doneself.tqdm.n=self.doneself.tqdm.last_print_n=self.doneself.step(0)
[docs]defset_status(self,status):""" Changes the prefix of the progressbar. """ifnotself.offandstatus!=self.status:self.status=statusself.tqdm.set_description(self.get_desc())self.tqdm.refresh()
[docs]defget_desc(self):""" Returns a formatted string of the description, consisted of the name and the status. The name supposed something constant within the life of the progressbar, while the status is there to give information about the current stage of the task. """return'%s%s%s%s'%(' '*8,self.name,' -- 'iflen(self.name)else'',self.status,)