1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-10-18 05:49:11 +00:00

feat(python): add pretty-printing of protobuf messages in IPython

This commit is contained in:
Darsey Litzenberger 2024-08-02 01:03:02 -06:00 committed by matejcik
parent 774f9de745
commit 9416caade4
2 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1 @@
Added pretty-printing of protobuf messages in IPython (`_repr_pretty_`)

View File

@ -35,6 +35,9 @@ from itertools import zip_longest
import typing_extensions as tx
if t.TYPE_CHECKING:
from IPython.lib.pretty import RepresentationPrinter # noqa: I900
T = t.TypeVar("T", bound=type)
MT = t.TypeVar("MT", bound="MessageType")
@ -271,6 +274,39 @@ class MessageType:
d[key] = value
return f"<{self.__class__.__name__}: {d}>"
def _repr_pretty_(self, p: RepresentationPrinter, cycle: bool) -> None:
"""prettier version of __repr__ for IPython
This pretty-prints/indents the object when displayed in IPython,
for example:
<PrevInput: {'prev_hash': b'xyzasdf',
'prev_index': 1,
'script_sig': b'abcdef',
'sequence': 42,
'decred_tree': 21}>
The API is for this method is described in the IPython docs:
https://ipython.readthedocs.io/en/8.26.0/api/generated/IPython.lib.pretty.html
"""
prefix = f"<{self.__class__.__name__}: {{"
if cycle:
p.text(f"{prefix} ...>")
return
with p.group(len(prefix), prefix, f"}}>"): # noqa: F541
itemsiter = (
(key, value)
for key, value in self.__dict__.items()
if not (value is None or value == [])
)
for i, (key, value) in enumerate(itemsiter):
if i:
p.text(",")
p.breakable()
subprefix = f"{key!r}: "
with p.group(len(subprefix), subprefix, ""):
p.pretty(value)
def ByteSize(self) -> int:
data = BytesIO()
dump_message(data, self)