Commit c0d284d5 authored by Philipp Gast's avatar Philipp Gast
Browse files

added a lightweight progress bar in mplClient waitForData()

parent 52671e3c
......@@ -37,7 +37,7 @@ rcParams['font.size'] = '22'
try:
rcParams['text.usetex'] = True # need dvipng, ghostscript, and Agg
except:
print(WARN + "I couls not set the LaTeX font sorry...")
print(WARN + "I could not set the LaTeX font sorry...")
# LINES
rcParams['lines.linewidth'] = 1.5
......
......@@ -2,7 +2,7 @@ from . import socket, AF_INET, SOCK_STREAM
from . import INFO, DBUG, WARN, SEVR, SPCE
SIZE_STATUS_MAX = 1024
from . import progressbar
from . import D_DEBUG
DEBUG = D_DEBUG
......@@ -298,9 +298,12 @@ class MplClient(object):
'''Wait for data of size dataSize.'''
receivedSize = 0
unpackedData = b''
progress = ProgressBar(dataSize, fmt=ProgressBar.FULL)
while receivedSize < dataSize:
unpackedData += sock.recv(4096)
progress(4096)
receivedSize = len(unpackedData)
progress.done()
return(unpackedData)
# UNPACKING --------------------------------------------------------
......
class ProgressBar(object):
DEFAULT = 'Progress: %(bar)s %(percent)3d%%'
FULL = '%(bar)s %(current)d/%(total)d (%(percent)3d%%) %(remaining)d to go'
def __init__(self, total, width=40, fmt=DEFAULT, symbol='=',
output=sys.stderr):
assert len(symbol) == 1
self.total = total
self.width = width
self.symbol = symbol
self.output = output
self.fmt = re.sub(r'(?P<name>%\(.+?\))d',
r'\g<name>%dd' % len(str(total)), fmt)
self.current = 0
def __call__(self,inc):
percent = self.current / float(self.total)
size = int(self.width * percent)
remaining = self.total - self.current
bar = '[' + self.symbol * size + ' ' * (self.width - size) + ']'
args = {
'total': self.total,
'bar': bar,
'current': self.current,
'percent': percent * 100,
'remaining': remaining
}
print('\r' + self.fmt % args, file=self.output, end='')
self.current += inc
def done(self):
self.current = self.total
self(0)
print('', file=self.output)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment