diff --git a/lib/galaxy/datatypes/data.py b/lib/galaxy/datatypes/data.py index 2c12f640af76..5994682784cc 100644 --- a/lib/galaxy/datatypes/data.py +++ b/lib/galaxy/datatypes/data.py @@ -1100,7 +1100,7 @@ def set_peek(self, dataset: DatasetProtocol, **kwd) -> None: else: est_lines = self.estimate_file_lines(dataset) if est_lines is not None: - dataset.blurb = f"~{util.commaify(util.roundify(str(est_lines)))} {inflector.cond_plural(est_lines, self.line_class)}" + dataset.blurb = f"~{util.shorten_with_metric_prefix(est_lines)} {inflector.cond_plural(est_lines, self.line_class)}" else: dataset.blurb = "Error: Cannot estimate lines in dataset" else: diff --git a/lib/galaxy/datatypes/tabular.py b/lib/galaxy/datatypes/tabular.py index 87d709e75998..d5181188fda1 100644 --- a/lib/galaxy/datatypes/tabular.py +++ b/lib/galaxy/datatypes/tabular.py @@ -127,6 +127,7 @@ def set_meta(self, dataset: DatasetProtocol, *, overwrite: bool = True, **kwd) - def set_peek(self, dataset: DatasetProtocol, **kwd) -> None: kwd.setdefault("line_wrap", False) super().set_peek(dataset, **kwd) + dataset.blurb = f"{dataset.blurb} {dataset.metadata.columns} columns" if dataset.metadata.comment_lines: dataset.blurb = f"{dataset.blurb}, {util.commaify(str(dataset.metadata.comment_lines))} comments" diff --git a/lib/galaxy/util/__init__.py b/lib/galaxy/util/__init__.py index 6949dbafbb71..31e798a0495f 100644 --- a/lib/galaxy/util/__init__.py +++ b/lib/galaxy/util/__init__.py @@ -1135,16 +1135,6 @@ def commaify(amount): return commaify(new) -def roundify(amount, sfs=2): - """ - Take a number in string form and truncate to 'sfs' significant figures. - """ - if len(amount) <= sfs: - return amount - else: - return amount[0:sfs] + "0" * (len(amount) - sfs) - - @overload def unicodify( # type: ignore[misc] value: Literal[None], @@ -1470,7 +1460,65 @@ def docstring_trim(docstring): return "\n".join(trimmed) -def nice_size(size): +def metric_prefix(number: Union[int, float], base: int) -> Tuple[float, str]: + """ + >>> metric_prefix(100, 1000) + (100.0, '') + >>> metric_prefix(999, 1000) + (999.0, '') + >>> metric_prefix(1000, 1000) + (1.0, 'K') + >>> metric_prefix(1001, 1000) + (1.001, 'K') + >>> metric_prefix(1000000, 1000) + (1.0, 'M') + >>> metric_prefix(1000**10, 1000) + (1.0, 'Q') + >>> metric_prefix(1000**11, 1000) + (1000.0, 'Q') + """ + prefixes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"] + if number < 0: + number = abs(number) + sign = -1 + else: + sign = 1 + + for prefix in prefixes: + if number < base: + return sign * float(number), prefix + number /= base + else: + return sign * float(number) * base, prefix + + +def shorten_with_metric_prefix(amount: int) -> str: + """ + >>> shorten_with_metric_prefix(23000) + '23K' + >>> shorten_with_metric_prefix(2300000) + '2.3M' + >>> shorten_with_metric_prefix(23000000) + '23M' + >>> shorten_with_metric_prefix(1) + '1' + >>> shorten_with_metric_prefix(0) + '0' + >>> shorten_with_metric_prefix(100) + '100' + >>> shorten_with_metric_prefix(-100) + '-100' + """ + m, prefix = metric_prefix(amount, 1000) + m_str = str(int(m)) if m.is_integer() else f"{m:.1f}" + exp = f"{m_str}{prefix}" + if len(exp) <= len(str(amount)): + return exp + else: + return str(amount) + + +def nice_size(size: Union[float, int, str]) -> str: """ Returns a readably formatted string with the size @@ -1483,23 +1531,15 @@ def nice_size(size): >>> nice_size(100000000) '95.4 MB' """ - words = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB"] - prefix = "" try: size = float(size) - if size < 0: - size = abs(size) - prefix = "-" - except Exception: + except ValueError: return "??? bytes" - for ind, word in enumerate(words): - step = 1024 ** (ind + 1) - if step > size: - size = size / float(1024**ind) - if word == "bytes": # No decimals for bytes - return "%s%d bytes" % (prefix, size) - return f"{prefix}{size:.1f} {word}" - return "??? bytes" + size, prefix = metric_prefix(size, 1024) + if prefix == "": + return "%d bytes" % size + else: + return f"{size:.1f} {prefix}B" def size_to_bytes(size):