Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions hydration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,26 @@ def __init__(self, *args, **kwargs):
self.__frozen = True

def __str__(self):
from .vectors import Array

x = [f'{self.__class__.__qualname__}:']
for name, field in self:
if isinstance(field, Struct):
x.append('\t{} ({}):'.format(name, field.__class__.__qualname__))
x.extend('\t{}'.format(field_str) for field_str in str(field).splitlines()[1:])
if isinstance(field, (Struct, Array)):
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Vector? This probably should be _Sequence instead of Array

# Get string representation of inner field
field_str_lines = str(field).splitlines()

# Check if inner field's representation is single-line
if len(field_str_lines) == 1:
x.append('\t{}:\t{}'.format(name, field_str_lines[0]))
else:
# Ignore first line of inner representation (the struct's name)
field_str_lines = field_str_lines[1:]

# Start multiline print
x.append('\t{} ({}):'.format(name, field.__class__.__qualname__))

# Add all inner fields indented
x.extend('\t{}'.format(field_str) for field_str in field_str_lines)
else:
x.append('\t{}:\t{}'.format(name, field))
return '\n'.join(x)
Expand Down
6 changes: 6 additions & 0 deletions hydration/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def from_bytes(self, data: bytes):
return self

def __str__(self):
if isinstance(self.type, Struct):
x = [f'{self.__class__.__qualname__}:', f'\ttype: {self.type.__class__.__qualname__}']
for index, field in enumerate(self.value):
x.append('\t{}:'.format(index))
x.extend('\t{}'.format(field_str) for field_str in str(field).splitlines()[1:])
return '\n'.join(x)
return '{}{}'.format(self.__class__.__qualname__, self.value)

def __repr__(self) -> str:
Expand Down