remove Tqdm.truncate => add {ncols_desc}

This commit is contained in:
Casper da Costa-Luis
2019-08-27 20:23:55 +01:00
parent 03a5c6df5e
commit f336afa861
7 changed files with 28 additions and 54 deletions
+17 -26
View File
@@ -36,11 +36,13 @@ class Tqdm(tqdm):
"""
BAR_FMT_DEFAULT = (
"{percentage:3.0f}%|{bar:10}|{desc} {bar:-10b}{n}/{total}"
"{percentage:3.0f}%|{bar:10}|"
"{desc:{ncols_desc}.{ncols_desc}}{n}/{total}"
" [{elapsed}<{remaining}, {rate_fmt:>11}{postfix}]"
)
BAR_FMT_NOTOTAL = (
"{desc} {bar:b}{n} [{elapsed}<??:??, {rate_fmt:>11}{postfix}]"
"{desc:{ncols_desc}.{ncols_desc}}{n}"
" [{elapsed}<??:??, {rate_fmt:>11}{postfix}]"
)
def __init__(
@@ -49,7 +51,6 @@ class Tqdm(tqdm):
disable=None,
level=logging.ERROR,
desc=None,
desc_truncate=None,
leave=False,
bar_format=None,
bytes=False, # pylint: disable=W0622
@@ -59,7 +60,6 @@ class Tqdm(tqdm):
bytes : shortcut for
`unit='B', unit_scale=True, unit_divisor=1024, miniters=1`
desc : persists after `close()`
desc_truncate : like `desc` but will `truncate()` and not persist
level : effective logging level for determining `disable`;
used only if `disable` is unspecified
kwargs : anything accepted by `tqdm.tqdm()`
@@ -72,8 +72,6 @@ class Tqdm(tqdm):
kwargs,
)
self.desc_persist = desc
if desc_truncate is not None:
desc = self.truncate(desc_truncate)
if disable is None:
disable = logger.getEffectiveLevel() > level
super(Tqdm, self).__init__(
@@ -81,7 +79,7 @@ class Tqdm(tqdm):
disable=disable,
leave=leave,
desc=desc,
bar_format="",
bar_format="!",
**kwargs
)
if bar_format is None:
@@ -92,13 +90,11 @@ class Tqdm(tqdm):
self.bar_format = bar_format
self.refresh()
def update_desc(self, desc, n=1, truncate=True):
def update_desc(self, desc, n=1):
"""
Calls `set_description_str(truncate(desc))` and `update(n)`
Calls `set_description_str(desc)` and `update(n)`
"""
self.set_description_str(
self.truncate(desc) if truncate else desc, refresh=False
)
self.set_description_str(desc, refresh=False)
self.update(n)
def update_to(self, current, total=None):
@@ -107,20 +103,15 @@ class Tqdm(tqdm):
self.update(current - self.n)
def close(self):
if self.desc_persist:
if self.desc_persist is not None:
self.set_description_str(self.desc_persist, refresh=False)
super(Tqdm, self).close()
@classmethod
def truncate(cls, s, max_len=25, 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)
@property
def format_dict(self):
"""inject `ncols_desc` to fill the display width (`ncols`)"""
d = super(Tqdm, self).format_dict
ncols = d.get("ncols", 80)
ncols_desc = ncols - len(self.format_meter(ncols_desc=1, **d)) + 1
d["ncols_desc"] = max(ncols_desc, 0)
return d
+2 -6
View File
@@ -115,9 +115,7 @@ class RemoteAZURE(RemoteBASE):
def _upload(
self, from_file, to_info, name=None, no_progress_bar=False, **_kwargs
):
with Tqdm(
desc_truncate=name, disable=no_progress_bar, bytes=True
) as pbar:
with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar:
self.blob_service.create_blob_from_path(
to_info.bucket,
to_info.path,
@@ -128,9 +126,7 @@ class RemoteAZURE(RemoteBASE):
def _download(
self, from_info, to_file, name=None, no_progress_bar=False, **_kwargs
):
with Tqdm(
desc_truncate=name, disable=no_progress_bar, bytes=True
) as pbar:
with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar:
self.blob_service.get_blob_to_path(
from_info.bucket,
from_info.path,
+1 -1
View File
@@ -38,7 +38,7 @@ class RemoteHTTP(RemoteBASE):
total=None if no_progress_bar else self._content_length(from_info),
leave=False,
bytes=True,
desc_truncate=from_info.url if name is None else name,
desc=from_info.url if name is None else name,
disable=no_progress_bar,
) as pbar:
with open(to_file, "wb") as fd:
+2 -6
View File
@@ -107,9 +107,7 @@ class RemoteOSS(RemoteBASE):
def _upload(
self, from_file, to_info, name=None, no_progress_bar=False, **_kwargs
):
with Tqdm(
desc_truncate=name, disable=no_progress_bar, bytes=True
) as pbar:
with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar:
self.oss_service.put_object_from_file(
to_info.path, from_file, progress_callback=pbar.update_to
)
@@ -117,9 +115,7 @@ class RemoteOSS(RemoteBASE):
def _download(
self, from_info, to_file, name=None, no_progress_bar=False, **_kwargs
):
with Tqdm(
desc_truncate=name, disable=no_progress_bar, bytes=True
) as pbar:
with Tqdm(desc=name, disable=no_progress_bar, bytes=True) as pbar:
self.oss_service.get_object_to_file(
from_info.path, to_file, progress_callback=pbar.update_to
)
+2 -8
View File
@@ -208,10 +208,7 @@ class RemoteS3(RemoteBASE):
def _upload(self, from_file, to_info, name=None, no_progress_bar=False):
total = os.path.getsize(from_file)
with Tqdm(
disable=no_progress_bar,
total=total,
bytes=True,
desc_truncate=name,
disable=no_progress_bar, total=total, bytes=True, desc=name
) as pbar:
self.s3.upload_file(
from_file,
@@ -229,10 +226,7 @@ class RemoteS3(RemoteBASE):
Bucket=from_info.bucket, Key=from_info.path
)["ContentLength"]
with Tqdm(
disable=no_progress_bar,
total=total,
bytes=True,
desc_truncate=name,
disable=no_progress_bar, total=total, bytes=True, desc=name
) as pbar:
self.s3.download_file(
from_info.bucket, from_info.path, to_file, Callback=pbar.update
+2 -2
View File
@@ -169,7 +169,7 @@ class SSHConnection:
def download(self, src, dest, no_progress_bar=False, progress_title=None):
with Tqdm(
desc_truncate=progress_title or os.path.basename(src),
desc=progress_title or os.path.basename(src),
disable=no_progress_bar,
bytes=True,
) as pbar:
@@ -186,7 +186,7 @@ class SSHConnection:
progress_title = posixpath.basename(dest)
with Tqdm(
desc_truncate=progress_title, disable=no_progress_bar, bytes=True
desc=progress_title, disable=no_progress_bar, bytes=True
) as pbar:
self.sftp.put(src, tmp_file, callback=pbar.update_to)
+2 -5
View File
@@ -59,7 +59,7 @@ def file_md5(fname):
name = relpath(fname)
with Tqdm(
desc_truncate=name,
desc=name,
disable=no_progress_bar,
total=size,
bytes=True,
@@ -132,10 +132,7 @@ def copyfile(src, dest, no_progress_bar=False, name=None):
System.reflink(src, dest)
except DvcException:
with Tqdm(
desc_truncate=name,
disable=no_progress_bar,
total=total,
bytes=True,
desc=name, disable=no_progress_bar, total=total, bytes=True
) as pbar:
with open(src, "rb") as fsrc, open(dest, "wb+") as fdest:
while True: