add progress.Tqdm(truncate)

This commit is contained in:
Casper da Costa-Luis
2019-08-10 14:15:16 +01:00
parent 6aee6b6b16
commit 3f0b85cf15
2 changed files with 14 additions and 0 deletions
+13
View File
@@ -40,6 +40,19 @@ class Tqdm(tqdm):
self.set_description(desc, refresh=False)
self.update(n)
@classmethod
def truncate(cls, s, max_len=10, end=True, fill="..."):
"""
Guarantee len(output) < max_lenself.
>>> truncate("hello", 4)
'...o'
"""
if len(s) <= max_len:
return s
if len(fill) > max_len:
return fill[-max_len:] if end else fill[:max_len]
i = max_len - len(fill)
return (fill + s[-i:]) if end else (s[:i] + fill)
class Progress(tqdm):
"""
+1
View File
@@ -36,6 +36,7 @@ class RemoteHTTP(RemoteBASE):
# leave = total > self.CHUNK_SIZE * 100
with Tqdm(total=total, leave=leave, bytes=True,
desc=Tqdm.truncate(to_file, 10),
disable=no_progress_bar) as pbar:
with open(to_file, "wb") as fd:
for chunk in request.iter_content(chunk_size=self.CHUNK_SIZE):