Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better display of estimated line numbers and add number of columns for tabular #17492

Merged
merged 8 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/galaxy/datatypes/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
90 changes: 65 additions & 25 deletions lib/galaxy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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

Expand All @@ -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):
Expand Down
Loading