Skip to content
Snippets Groups Projects
Commit 2fae7da3 authored by roadscape's avatar roadscape
Browse files

move secs_to_str to utils, add test

parent 178a29ac
No related branches found
No related tags found
No related merge requests found
......@@ -92,6 +92,19 @@ def trunc(string, maxlen):
string = string[0:(maxlen-3)] + '...'
return string
def secs_to_str(secs):
"""Given number of seconds returns, e.g., `02h 29m 39s`"""
units = (('s', 60), ('m', 60), ('h', 24), ('d', 7))
out = []
rem = secs
for (unit, cycle) in units:
out.append((rem % cycle, unit))
rem = int(rem / cycle)
if not rem:
break
if rem: # leftover = weeks
out.append((rem, 'w'))
return ' '.join(["%02d%s" % tup for tup in out[::-1]])
def rep_log10(rep):
"""Convert raw steemd rep into a UI-ready value centered at 25."""
......
"""Timer for reporting progress on long batch operations."""
import time
from hive.utils.normalize import secs_to_str
class Timer:
"""Times long routines, printing status and ETA.
......@@ -73,7 +74,7 @@ class Timer:
else:
total_time = self._end_time - self._start_time
out += "done in %s, avg rate: %.1f/s" % (
Timer.secs_to_str(total_time),
secs_to_str(total_time),
self._total / total_time)
return out
......@@ -87,22 +88,7 @@ class Timer:
"""Time to finish, based on most recent batch."""
left = self._full_total - self._processed
secs = (left / self._rate())
return Timer.secs_to_str(secs)
@staticmethod
def secs_to_str(secs):
"""Given number of seconds returns, e.g., `02h 29m 39s`"""
units = (('s', 60), ('m', 60), ('h', 24), ('d', 7))
out = []
rem = secs
for (unit, cycle) in units:
out.append((rem % cycle, unit))
rem = int(rem / cycle)
if not rem:
break
if rem: # leftover = weeks
out.append((rem, 'w'))
return ' '.join(["%02d%s" % tup for tup in out[::-1]])
return secs_to_str(secs)
def _elapsed(self, lap_idx=None):
if not lap_idx:
......
......@@ -15,8 +15,14 @@ from hive.utils.normalize import (
trunc,
rep_log10,
safe_img_url,
secs_to_str,
)
def test_secs_to_str():
assert secs_to_str(0) == '00s'
assert secs_to_str(8979) == '02h 29m 39s'
assert secs_to_str(12345678) == '20w 02d 21h 21m 18s'
def test_block_num():
block = dict(block_id='013c33f88c643c92a7352b52efde7237f4d4ee0b')
assert block_num(block) == 20722680
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment