Skip to content

Commit

Permalink
Rename shorten_selector_data to shorten and improve its implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gallaecio committed Jul 11, 2019
1 parent ac5ec5e commit 63fe189
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
4 changes: 2 additions & 2 deletions parsel/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import six
from lxml import etree, html

from .utils import flatten, iflatten, extract_regex, shorten_selector_data
from .utils import flatten, iflatten, extract_regex, shorten
from .csstranslator import HTMLTranslator, GenericTranslator


Expand Down Expand Up @@ -358,6 +358,6 @@ def __bool__(self):
__nonzero__ = __bool__

def __str__(self):
data = repr(shorten_selector_data(self.get(), width=40))
data = repr(shorten(self.get(), width=40))
return "<%s xpath=%r data=%s>" % (type(self).__name__, self._expr, data)
__repr__ = __str__
14 changes: 10 additions & 4 deletions parsel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ def extract_regex(regex, text, replace_entities=True):
return strings
return [w3lib_replace_entities(s, keep=['lt', 'amp']) for s in strings]

def shorten_selector_data(data, width):
"""Shortens the preview of extracted data by adding placeholder '...'
"""
return data[:width - 3] + "..." if (len(data) > width - 3) else data

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')
26 changes: 26 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from parsel.utils import shorten

from pytest import mark, raises
import six


@mark.parametrize(
'width,expected',
(
(-1, ValueError),
(0, u''),
(1, u'.'),
(2, u'..'),
(3, u'...'),
(4, u'f...'),
(5, u'fo...'),
(6, u'foobar'),
(7, u'foobar'),
)
)
def test_shorten(width, expected):
if isinstance(expected, six.string_types):
assert shorten(u'foobar', width) == expected
else:
with raises(expected):
shorten(u'foobar', width)

0 comments on commit 63fe189

Please sign in to comment.