Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hivemind
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
hive
hivemind
Commits
2fae7da3
Commit
2fae7da3
authored
6 years ago
by
roadscape
Browse files
Options
Downloads
Patches
Plain Diff
move secs_to_str to utils, add test
parent
178a29ac
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
hive/utils/normalize.py
+13
-0
13 additions, 0 deletions
hive/utils/normalize.py
hive/utils/timer.py
+3
-17
3 additions, 17 deletions
hive/utils/timer.py
tests/test_utils_normalize.py
+6
-0
6 additions, 0 deletions
tests/test_utils_normalize.py
with
22 additions
and
17 deletions
hive/utils/normalize.py
+
13
−
0
View file @
2fae7da3
...
...
@@ -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.
"""
...
...
This diff is collapsed.
Click to expand it.
hive/utils/timer.py
+
3
−
17
View file @
2fae7da3
"""
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
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_utils_normalize.py
+
6
−
0
View file @
2fae7da3
...
...
@@ -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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment