util: cut off long data by default

This commit is contained in:
Shiz 2022-01-04 01:35:39 +01:00
parent 6b972ac877
commit ac8d019ad7
1 changed files with 7 additions and 2 deletions

View File

@ -38,8 +38,13 @@ def indent(s: str, count: int, start: bool = False) -> str:
lines[i] = ' ' * count + lines[i]
return '\n'.join(lines)
def format_bytes(bs: bytes) -> str:
return '[' + ' '.join(hex(b)[2:].zfill(2) for b in bs) + ']'
def format_bytes(bs: bytes, cutoff=256) -> str:
if len(bs) > 256:
trailer = ' ...'
bs = bs[:cutoff]
else:
trailer = ''
return '[' + ' '.join(hex(b)[2:].zfill(2) for b in bs) + trailer + ']'
def format_value(value: Any, formatter: Callable[[Any], str], indentation: int = 0) -> str:
""" Format containers to use the given formatter function instead of always repr(). """