Cython: Progress bar: added a cut option to choose whether to do line

breaks every tenth of the full bar, set to False by default for lighter
printing
This commit is contained in:
Celine Mercier
2018-10-17 15:52:26 +02:00
parent 343dbc7e4d
commit eb6d5581bd
2 changed files with 30 additions and 23 deletions

View File

@ -51,6 +51,7 @@ cdef class ProgressBar:
cdef int lastlog cdef int lastlog
cdef bint ontty cdef bint ontty
cdef int fd cdef int fd
cdef bint cut
cdef bytes _head cdef bytes _head
cdef char *chead cdef char *chead

View File

@ -6,11 +6,14 @@ Created on 27 mars 2016
@author: coissac @author: coissac
''' '''
from ..utils cimport str2bytes, bytes2str from ..utils cimport str2bytes, bytes2str
from .config cimport getConfiguration from .config cimport getConfiguration
import sys import sys
cdef class ProgressBar: cdef class ProgressBar:
cdef clock_t clock(self): cdef clock_t clock(self):
cdef clock_t t cdef clock_t t
cdef timeval tp cdef timeval tp
@ -22,11 +25,14 @@ cdef class ProgressBar:
return t return t
def __init__(self, def __init__(self,
off_t maxi, off_t maxi,
dict config={}, dict config={},
str head="", str head="",
double seconde=0.1): double seconde=0.1,
cut=False):
self.starttime = self.clock() self.starttime = self.clock()
self.lasttime = self.starttime self.lasttime = self.starttime
self.tickcount = <clock_t> (seconde * CLOCKS_PER_SEC) self.tickcount = <clock_t> (seconde * CLOCKS_PER_SEC)
@ -37,17 +43,16 @@ cdef class ProgressBar:
if not config: if not config:
config=getConfiguration() config=getConfiguration()
self.ontty = sys.stderr.isatty() self.ontty = sys.stderr.isatty()
if (maxi<=0): if (maxi<=0):
maxi=1 maxi=1
self.maxi = maxi self.maxi = maxi
self.head = head self.head = head
self.chead= self._head self.chead = self._head
self.cut = cut
self.logger=config[config["__root_config__"]]["logger"] self.logger=config[config["__root_config__"]]["logger"]
self.wheel = '|/-\\' self.wheel = '|/-\\'
@ -61,8 +66,9 @@ cdef class ProgressBar:
'##########' \ '##########' \
'##########' \ '##########' \
'##########' '##########'
def __call__(self,object pos):
def __call__(self, object pos):
cdef off_t ipos cdef off_t ipos
cdef clock_t elapsed cdef clock_t elapsed
cdef clock_t newtime cdef clock_t newtime
@ -111,7 +117,7 @@ cdef class ProgressBar:
self.arrow=(self.arrow+1) % 4 self.arrow=(self.arrow+1) % 4
if days: if days:
<void>fprintf(stderr,b'\r%s %5.1f %% |%.*s%c%.*s] remain : %d days %02d:%02d:%02d', <void>fprintf(stderr,b'\r%s %5.1f %% |%.*s%c%.*s] remain : %d days %02d:%02d:%02d\t',
self.chead, self.chead,
percent*100, percent*100,
fraction,self.diese, fraction,self.diese,
@ -119,7 +125,7 @@ cdef class ProgressBar:
50-fraction,self.spaces, 50-fraction,self.spaces,
days,hour,minu,sec) days,hour,minu,sec)
else: else:
<void>fprintf(stderr,b'\r%s %5.1f %% |%.*s%c%.*s] remain : %02d:%02d:%02d', <void>fprintf(stderr,b'\r%s %5.1f %% |%.*s%c%.*s] remain : %02d:%02d:%02d\t',
self.chead, self.chead,
percent*100., percent*100.,
fraction,self.diese, fraction,self.diese,
@ -127,25 +133,25 @@ cdef class ProgressBar:
50-fraction,self.spaces, 50-fraction,self.spaces,
hour,minu,sec) hour,minu,sec)
tenth = int(percent * 10) if self.cut:
if tenth != self.lastlog: tenth = int(percent * 10)
if tenth != self.lastlog:
if self.ontty:
<void>fputs(b'\n',stderr)
self.logger.info('%s %5.1f %% remain : %02d:%02d:%02d' % ( if self.ontty:
bytes2str(self._head), <void>fputs(b'\n',stderr)
percent*100.,
hour,minu,sec)) self.logger.info('%s %5.1f %% remain : %02d:%02d:%02d' % (
self.lastlog=tenth bytes2str(self._head),
percent*100.,
hour,minu,sec))
self.lastlog=tenth
else: else:
self.cycle+=1 self.cycle+=1
property head:
property head:
def __get__(self): def __get__(self):
return self._head return self._head
def __set__(self,str value): def __set__(self,str value):
self._head=str2bytes(value) self._head=str2bytes(value)
self.chead=self._head self.chead=self._head