diff --git a/parsel/selector.py b/parsel/selector.py index bbd4289a..666a3036 100644 --- a/parsel/selector.py +++ b/parsel/selector.py @@ -7,7 +7,7 @@ import six from lxml import etree, html -from .utils import flatten, iflatten, extract_regex +from .utils import flatten, iflatten, extract_regex, shorten from .csstranslator import HTMLTranslator, GenericTranslator @@ -358,6 +358,6 @@ def __bool__(self): __nonzero__ = __bool__ def __str__(self): - data = repr(self.get()[:40]) + data = repr(shorten(self.get(), width=40)) return "<%s xpath=%r data=%s>" % (type(self).__name__, self._expr, data) __repr__ = __str__ diff --git a/parsel/utils.py b/parsel/utils.py index 56bb105d..458bc6cc 100644 --- a/parsel/utils.py +++ b/parsel/utils.py @@ -80,4 +80,15 @@ def extract_regex(regex, text, replace_entities=True): strings = flatten(strings) if not replace_entities: return strings - return [w3lib_replace_entities(s, keep=['lt', 'amp']) for s in strings] \ No newline at end of file + return [w3lib_replace_entities(s, keep=['lt', 'amp']) for s in strings] + + +def shorten(text, width, suffix='...'): + """Truncate the given text to fit in the given width.""" + if len(text) <= width: + return text + if width > len(suffix): + return text[:width-len(suffix)] + suffix + if width >= 0: + return suffix[len(suffix)-width:] + raise ValueError('width must be equal or greater than 0') diff --git a/tests/test_selector.py b/tests/test_selector.py index e504166a..c8845a5f 100644 --- a/tests/test_selector.py +++ b/tests/test_selector.py @@ -133,9 +133,9 @@ def test_representation_slice(self): body = u"
".format(50 * 'b') sel = self.sscls(text=body) - representation = "