Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Turned "strip_list" adaptor into "strip"

elpolilla011adf4dd753
diff --git a/scrapy/trunk/scrapy/contrib/adaptors/__init__.py b/scrapy/trunk/scrapy/contrib/adaptors/__init__.py
index b3951c0fc..665d72014 100644
--- a/scrapy/trunk/scrapy/contrib/adaptors/__init__.py
+++ b/scrapy/trunk/scrapy/contrib/adaptors/__init__.py
@@ -1,4 +1,4 @@
from scrapy.contrib.adaptors.extraction import extract, extract_unquoted, ExtractImages
from scrapy.contrib.adaptors.markup import remove_tags, remove_root, Unquote
-from scrapy.contrib.adaptors.misc import to_unicode, clean_spaces, strip_list, drop_empty, canonicalize_urls, Delist, Regex
+from scrapy.contrib.adaptors.misc import to_unicode, clean_spaces, strip, drop_empty, canonicalize_urls, Delist, Regex
from scrapy.utils.python import unique, flatten
diff --git a/scrapy/trunk/scrapy/contrib/adaptors/misc.py b/scrapy/trunk/scrapy/contrib/adaptors/misc.py
index a656eef3e..02a432fd1 100644
--- a/scrapy/trunk/scrapy/contrib/adaptors/misc.py
+++ b/scrapy/trunk/scrapy/contrib/adaptors/misc.py
@@ -37,18 +37,23 @@ def clean_spaces(value):
_clean_spaces_re = re.compile("\s+", re.U)
return [ _clean_spaces_re.sub(' ', v.decode('utf-8')) for v in value ]
-def strip_list(value):
+def strip(value):
"""
Removes any spaces at both the start and the ending
- of each string in the provided list.
+ of the provided string or list.
E.g:
- >> strip_list([' hi ', 'buddies '])
+ >> strip([' hi ', 'buddies '])
[u'hi', u'buddies']
+ >> strip(' hi buddies ')
+ u'hi buddies'
- Input: iterable of unicodes
- Output: list of unicodes
+ Input: unicode or iterable of unicodes
+ Output: unicode or list of unicodes
"""
- return [ unicode(v.strip()) for v in value ]
+ if isinstance(value, basestring):
+ return unicode(value.strip())
+ elif hasattr(value, '__iter__'):
+ return [ unicode(v.strip()) for v in value ]
def drop_empty(value):
"""
diff --git a/scrapy/trunk/scrapy/tests/test_adaptors.py b/scrapy/trunk/scrapy/tests/test_adaptors.py
index 881c40a06..d1f66b9fe 100644
--- a/scrapy/trunk/scrapy/tests/test_adaptors.py
+++ b/scrapy/trunk/scrapy/tests/test_adaptors.py
@@ -69,9 +69,11 @@ class AdaptorsTestCase(unittest.TestCase):
self.assertEqual(adaptors.clean_spaces([' hello, whats up?', 'testing testingtesting testing']),
[' hello, whats up?', 'testing testingtesting testing'])
- def test_strip_list(self):
- self.assertEqual(adaptors.strip_list([' hi there, sweety ;D ', ' I CAN HAZ TEST?? ']),
+ def test_strip(self):
+ self.assertEqual(adaptors.strip([' hi there, sweety ;D ', ' I CAN HAZ TEST?? ']),
['hi there, sweety ;D', 'I CAN HAZ TEST??'])
+ self.assertEqual(adaptors.strip(' hello there, this is my test '),
+ 'hello there, this is my test')
def test_drop_empty_elements(self):
self.assertEqual(adaptors.drop_empty([1, 2, None, 5, 0, 6, False, 'hi']),