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 bint ontty
cdef int fd
cdef bint cut
cdef bytes _head
cdef char *chead

View File

@ -6,11 +6,14 @@ Created on 27 mars 2016
@author: coissac
'''
from ..utils cimport str2bytes, bytes2str
from .config cimport getConfiguration
import sys
cdef class ProgressBar:
cdef clock_t clock(self):
cdef clock_t t
cdef timeval tp
@ -22,11 +25,14 @@ cdef class ProgressBar:
return t
def __init__(self,
off_t maxi,
dict config={},
str head="",
double seconde=0.1):
double seconde=0.1,
cut=False):
self.starttime = self.clock()
self.lasttime = self.starttime
self.tickcount = <clock_t> (seconde * CLOCKS_PER_SEC)
@ -38,7 +44,6 @@ cdef class ProgressBar:
if not config:
config=getConfiguration()
self.ontty = sys.stderr.isatty()
if (maxi<=0):
@ -46,8 +51,8 @@ cdef class ProgressBar:
self.maxi = maxi
self.head = head
self.chead= self._head
self.chead = self._head
self.cut = cut
self.logger=config[config["__root_config__"]]["logger"]
self.wheel = '|/-\\'
@ -62,7 +67,8 @@ cdef class ProgressBar:
'##########' \
'##########'
def __call__(self,object pos):
def __call__(self, object pos):
cdef off_t ipos
cdef clock_t elapsed
cdef clock_t newtime
@ -111,7 +117,7 @@ cdef class ProgressBar:
self.arrow=(self.arrow+1) % 4
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,
percent*100,
fraction,self.diese,
@ -119,7 +125,7 @@ cdef class ProgressBar:
50-fraction,self.spaces,
days,hour,minu,sec)
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,
percent*100.,
fraction,self.diese,
@ -127,6 +133,7 @@ cdef class ProgressBar:
50-fraction,self.spaces,
hour,minu,sec)
if self.cut:
tenth = int(percent * 10)
if tenth != self.lastlog:
@ -141,11 +148,10 @@ cdef class ProgressBar:
else:
self.cycle+=1
property head:
property head:
def __get__(self):
return self._head
def __set__(self,str value):
self._head=str2bytes(value)
self.chead=self._head