Skip to content

Commit

Permalink
fix: improved display of percentage of done jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskoester committed Jul 22, 2021
1 parent 83b1f5b commit 1fee8c0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions snakemake/logging.py
Expand Up @@ -558,10 +558,10 @@ def timestamp():
elif level == "progress" and not self.quiet:
done = msg["done"]
total = msg["total"]
p = done / total
percent_fmt = ("{:.2%}" if p < 0.01 else "{:.0%}").format(p)
self.logger.info(
"{} of {} steps ({}) done".format(done, total, percent_fmt)
"{} of {} steps ({}) done".format(
done, total, format_percentage(done, total)
)
)
elif level == "shellcmd":
if self.printshellcmds:
Expand Down Expand Up @@ -627,6 +627,21 @@ def format_resource_names(resources, omit_resources="_cores _nodes".split()):
return ", ".join(name for name in resources if name not in omit_resources)


def format_percentage(done, total):
"""Format percentage from given fraction while avoiding superflous precision."""
if done == total:
return "100%"
if done == 0:
return "0%"
precision = 0
fraction = done / total
fmt_precision = "{{:.{}%}}".format
fmt = lambda fraction: fmt_precision(precision).format(fraction)
while fmt(fraction) == "100%" or fmt(fraction) == "0%":
precision += 1
return fmt(fraction)


logger = Logger()


Expand Down

0 comments on commit 1fee8c0

Please sign in to comment.