Skip to content
Permalink
Browse files
fix: improved display of percentage of done jobs
  • Loading branch information
johanneskoester committed Jul 22, 2021
1 parent 83b1f5b commit 1fee8c06d6ed229d7e3757de3c693e755d01d1bb
Showing 1 changed file with 18 additions and 3 deletions.
@@ -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:
@@ -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()


0 comments on commit 1fee8c0

Please sign in to comment.