Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bernt-matthias committed Feb 21, 2024
1 parent 0790854 commit 3a8dbf5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
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.trailing_zeros_to_powerof10(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
62 changes: 29 additions & 33 deletions lib/galaxy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,32 +1135,6 @@ def commaify(amount):
return commaify(new)


def trailing_zeros_to_powerof10(amount: int) -> str:
"""
>>> trailing_zeros_to_powerof10(23000)
'23K'
>>> trailing_zeros_to_powerof10(2300000)
'2.3M'
>>> trailing_zeros_to_powerof10(23000000)
'23M'
>>> trailing_zeros_to_powerof10(1)
'1'
>>> trailing_zeros_to_powerof10(0)
'0'
>>> trailing_zeros_to_powerof10(100)
'100'
>>> trailing_zeros_to_powerof10(-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)


@overload
def unicodify( # type: ignore[misc]
value: Literal[None],
Expand Down Expand Up @@ -1486,18 +1460,14 @@ def docstring_trim(docstring):
return "\n".join(trimmed)


def metric_prefix(number: Union[int, float], base: int, text: bool = True) -> Tuple[float, str]:
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(999, 1000, False)
(999.0, '0')
>>> metric_prefix(1000, 1000, False)
(1.0, '3')
>>> metric_prefix(1001, 1000)
(1.001, 'K')
>>> metric_prefix(1000000, 1000)
Expand All @@ -1516,10 +1486,36 @@ def metric_prefix(number: Union[int, float], base: int, text: bool = True) -> Tu

for i, prefix in enumerate(prefixes):
if number < base:
return sign * float(number), prefix if text else str(i * 3)
return sign * float(number), prefix
number /= base
else:
return sign * float(number) * base, prefix if text else str(i * 3)
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:
Expand Down

0 comments on commit 3a8dbf5

Please sign in to comment.