Skip to content

Commit

Permalink
improve display of estimated number of lines
Browse files Browse the repository at this point in the history
fixes #6506

for large files the number of lines is estimated and shown
as a rounded number (using two significant digits), e.g
`~8,700,000 lines`.

with this change it will be: `~87 10^5 lines`

this commit also makes roundify really round numbers (as the name
suggests) and not simply cut at two digits, but this could be
reverted if there are concerns wrt speed due to using more math
  • Loading branch information
bernt-matthias committed May 14, 2022
1 parent 62ce926 commit 943836c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/galaxy/datatypes/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def set_peek(self, dataset, line_count=None, WIDTH=256, skipchars=None, line_wra
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.trailing_zeros_to_powerof10(util.roundify(str(est_lines)))} {inflector.cond_plural(est_lines, self.line_class)}"
else:
dataset.blurb = "Error: Cannot estimate lines in dataset"
else:
Expand Down
49 changes: 44 additions & 5 deletions lib/galaxy/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from hashlib import md5
from math import (
floor,
log10,
)
from os.path import relpath
from urllib.parse import (
urlencode,
Expand Down Expand Up @@ -1065,14 +1069,49 @@ def commaify(amount):
return commaify(new)


def trailing_zeros_to_powerof10(amount):
"""
>>> trailing_zeros_to_powerof10(23000)
'23000'
>>> trailing_zeros_to_powerof10(2300000)
'23 10^5'
>>> trailing_zeros_to_powerof10(23000000)
'23 10^6'
>>> trailing_zeros_to_powerof10(1)
'1'
>>> trailing_zeros_to_powerof10(0)
'0'
>>> trailing_zeros_to_powerof10(100)
'100'
>>> trailing_zeros_to_powerof10(-100)
'-100'
"""
amount = str(amount)
zeros = 0
i = len(amount) - 1
while i >= 0 and amount[i] == "0":
zeros += 1
i -= 1
if len(amount) < len(f"{amount[:i+1]} 10^{zeros}"):
return amount
else:
return f"{amount[:i+1]} 10^{zeros}"


def roundify(amount, sfs=2):
"""
Take a number in string form and truncate to 'sfs' significant figures.
Take a number and round it to 'sfs' significant figures.
>>> roundify(99)
99
>>> roundify(-99)
-99
>>> roundify(1111)
1100
>>> roundify(1999)
2000
"""
if len(amount) <= sfs:
return amount
else:
return amount[0:sfs] + "0" * (len(amount) - sfs)
return round(amount, -int(floor(log10(abs(amount)))) + sfs - 1)


def unicodify(value, encoding=DEFAULT_ENCODING, error="replace", strip_null=False, log_exception=True):
Expand Down

0 comments on commit 943836c

Please sign in to comment.