Skip to content

Commit 5147609

Browse files
committed
Pretty hash display utility
1 parent ac7d865 commit 5147609

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

tests/trinity/core/humanize-utils/test_humanize_elapsed.py renamed to tests/trinity/core/utils/test_humanize.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import pytest
22

3-
from trinity.utils.humanize import humanize_elapsed
3+
from trinity.utils.humanize import humanize_elapsed, humanize_hash
44

55

66
SECOND = 1
@@ -39,3 +39,13 @@
3939
def test_humanize_elapsed(seconds, expected):
4040
actual = humanize_elapsed(seconds)
4141
assert actual == expected
42+
43+
44+
@pytest.mark.parametrize(
45+
'hash32,expected',
46+
(
47+
(bytes(range(32)), '000102..1d1e1f'),
48+
)
49+
)
50+
def test_humanize_hash(hash32, expected):
51+
assert humanize_hash(hash32) == expected

trinity/utils/humanize.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Iterator
22

3+
from eth_typing import Hash32
4+
35

46
def humanize_elapsed(seconds: int) -> str:
57
return ''.join(_humanize_elapsed(seconds))
@@ -44,3 +46,12 @@ def _humanize_elapsed(seconds: int) -> Iterator[str]:
4446
return
4547

4648
remainder %= duration
49+
50+
51+
DISPLAY_HASH_BYTES = 3
52+
53+
54+
def humanize_hash(value: Hash32) -> str:
55+
head = value[:DISPLAY_HASH_BYTES]
56+
tail = value[-1 * DISPLAY_HASH_BYTES:]
57+
return f"{head.hex()}..{tail.hex()}"

0 commit comments

Comments
 (0)