Improve ListModel, ListItem, SortFilterProxy repr

Newlines and indentation
This commit is contained in:
miruka 2019-05-10 20:12:58 -04:00
parent 76fc03e8a1
commit 7ff3448fed
3 changed files with 36 additions and 4 deletions

View File

@ -1,3 +1,7 @@
# Copyright 2019 miruka
# This file is part of harmonyqml, licensed under GPLv3.
import textwrap
from typing import Any, Dict, List, Mapping, Set, Tuple, Union
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal, pyqtSlot
@ -150,13 +154,26 @@ class ListItem(QObject, metaclass=_ListItemMeta):
def __repr__(self) -> str:
from .list_model import ListModel
multiline = any((
isinstance(v, ListModel) for _, v in self._props.values()
))
prop_strings = (
"\033[%dm%s\033[0m=%r" % (
"\033[{0}m{1}{2}={2}{3}\033[0m".format(
1 if p == self.mainKey else 0, # 1 = term bold
p,
" " if multiline else "",
getattr(self, p)
) for p in list(self._props.keys()) + self._direct_props
)
if any((isinstance(v, ListModel) for _, v in self._props.values())):
return "%s(\n%s\n)" % (
type(self).__name__,
textwrap.indent(",\n".join(prop_strings), prefix=" " * 4)
)
return "%s(%s)" % (type(self).__name__, ", ".join(prop_strings))

View File

@ -1,4 +1,5 @@
import logging
import textwrap
from typing import (
Any, Callable, Dict, Iterable, List, Mapping, MutableSequence, Optional,
Sequence, Set, Tuple, Union
@ -40,7 +41,16 @@ class ListModel(QAbstractListModel):
def __repr__(self) -> str:
return "%s(%r)" % (type(self).__name__, self._data)
if not self._data:
return "%s()" % type(self).__name__
return "%s(\n%s\n)" % (
type(self).__name__,
textwrap.indent(
",\n".join((repr(item) for item in self._data)),
prefix = " " * 4,
)
)
def __contains__(self, index: Index) -> bool:

View File

@ -52,8 +52,13 @@ class SortFilterProxy(QSortFilterProxyModel):
def __repr__(self) -> str:
return "%s(sortByRole=%r, sourceModel=%r)" % (
type(self).__name__, self.sortByRole, self.sourceModel(),
return "%s(sortByRole=%r, sourceModel=%s)" % (
type(self).__name__,
self.sortByRole,
"<%s at %s>" % (
type(self.sourceModel()).__name__,
hex(id(self.sourceModel())),
)
)