Skip to content

Commit

Permalink
code review for Element.__str__
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Jul 22, 2024
1 parent f87b67c commit 0f4e1f0
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions nicegui/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,32 +570,30 @@ def is_deleted(self) -> bool:
return self._deleted

def __str__(self) -> str:
additions = self._additions_str()
result = f'{self._element_str()}' + (f' [{", ".join(additions)}]' if additions else '')
for child in self.default_slot.children:
for line in child.__str__().split('\n'):
result += f'\n {line}'
return result
result = self.tag if type(self) is Element else self.__class__.__name__ # pylint: disable=unidiomatic-typecheck

def _element_str(self) -> str:
return self.__class__.__name__ if type(self) != Element else self.tag # pylint: disable=unidiomatic-typecheck

def _additions_str(self) -> List[str]:
def shorten(content: Any, length: int = 20) -> str:
text = str(content).replace('\n', ' ').replace('\r', ' ')
return text[:length].strip() + '...' if len(text) > length else text

additions = []
if self._markers:
additions.append(f'markers={", ".join(self._markers)}')
if hasattr(self, 'text') and self._text:
if self._text:
additions.append(f'text={shorten(self._text)}')
if hasattr(self, 'content') and self.content: # pylint: disable=no-member
additions.append(f'content={shorten(self.content)}') # pylint: disable=no-member
IGNORED_PROPS = {'loopback', 'color', 'view', 'innerHTML', 'codehilite_css'}
additions += [
f'{key}={shorten(value)}' for key, value in self._props.items()
if not key.startswith('_') and
key not in ['loopback', 'color', 'view', 'innerHTML', 'codehilite_css'] and
value not in [None, False, '']
f'{key}={shorten(value)}'
for key, value in self._props.items()
if not key.startswith('_') and key not in IGNORED_PROPS and value
]
return additions
if additions:
result += f' [{", ".join(additions)}]'

for child in self.default_slot.children:
for line in str(child).split('\n'):
result += f'\n {line}'

return result

0 comments on commit 0f4e1f0

Please sign in to comment.