mirror of https://github.com/scrapy/scrapy.git
utils: add any value to iterable function
--HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40952
This commit is contained in:
parent
315934bf6b
commit
2acdd9b409
|
|
@ -1,7 +1,7 @@
|
|||
import unittest
|
||||
from cStringIO import StringIO
|
||||
|
||||
from scrapy.utils.misc import hash_values, items_to_csv, load_object, arg_to_list
|
||||
from scrapy.utils.misc import hash_values, items_to_csv, load_object, arg_to_list, arg_to_iter
|
||||
from scrapy.item import ScrapedItem
|
||||
|
||||
class UtilsMiscTestCase(unittest.TestCase):
|
||||
|
|
@ -67,5 +67,25 @@ class UtilsMiscTestCase(unittest.TestCase):
|
|||
self.assertEqual(arg_to_list(100), [100])
|
||||
self.assertEqual(arg_to_list(('lala', 'poo')), ['lala', 'poo'])
|
||||
|
||||
def test_arg_to_list(self):
|
||||
self.assertEqual(arg_to_list(None), [])
|
||||
self.assertEqual(arg_to_list('lala'), ['lala'])
|
||||
self.assertEqual(arg_to_list(100), [100])
|
||||
self.assertEqual(arg_to_list(('lala', 'poo')), ['lala', 'poo'])
|
||||
self.assertEqual(arg_to_list(s for s in ['lala', 'poo']), ['lala', 'poo'])
|
||||
|
||||
def test_arg_to_iter(self):
|
||||
assert hasattr(arg_to_iter(None), '__iter__')
|
||||
assert hasattr(arg_to_iter(100), '__iter__')
|
||||
assert hasattr(arg_to_iter('lala'), '__iter__')
|
||||
assert hasattr(arg_to_iter([1,2,3]), '__iter__')
|
||||
assert hasattr(arg_to_iter(l for l in 'abcd'), '__iter__')
|
||||
|
||||
self.assertEqual(list(arg_to_iter(None)), [])
|
||||
self.assertEqual(list(arg_to_iter('lala')), ['lala'])
|
||||
self.assertEqual(list(arg_to_iter(100)), [100])
|
||||
self.assertEqual(list(arg_to_iter(l for l in 'abc')), ['a', 'b', 'c'])
|
||||
self.assertEqual(list(arg_to_iter([1,2,3])), [1,2,3])
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ from scrapy.utils.defer import defer_succeed
|
|||
|
||||
def arg_to_list(arg):
|
||||
"""Convert an argument to list, so that it can be iterated. The argument
|
||||
can be a None, single value, or an iterable.
|
||||
|
||||
can be a None, single value, or an iterable.
|
||||
|
||||
This is meant to be used in functions for arguments which can be either
|
||||
None, single valued or multi-valued.
|
||||
"""
|
||||
|
|
@ -28,6 +28,17 @@ def arg_to_list(arg):
|
|||
else:
|
||||
return [arg]
|
||||
|
||||
def arg_to_iter(arg):
|
||||
"""Convert an argument to an iterable. The argument can be a None, single
|
||||
value, or an iterable.
|
||||
"""
|
||||
if arg is None:
|
||||
return []
|
||||
elif hasattr(arg, '__iter__'):
|
||||
return arg
|
||||
else:
|
||||
return [arg]
|
||||
|
||||
def dict_updatedefault(D, E, **F):
|
||||
"""
|
||||
updatedefault(D, E, **F) -> None.
|
||||
|
|
@ -101,7 +112,7 @@ load_class = load_object # backwards compatibility, but isnt going to be availab
|
|||
|
||||
def extract_regex(regex, text, encoding):
|
||||
"""Extract a list of unicode strings from the given text/encoding using the following policies:
|
||||
|
||||
|
||||
* if the regex contains a named group called "extract" that will be returned
|
||||
* if the regex contains multiple numbered groups, all those will be returned (flattened)
|
||||
* if the regex doesn't contain any group the entire regex matching is returned
|
||||
|
|
@ -122,8 +133,8 @@ def extract_regex(regex, text, encoding):
|
|||
return [remove_entities(unicode(s, encoding), keep=['lt', 'amp']) for s in strings]
|
||||
|
||||
def hash_values(*values):
|
||||
"""Hash a series of non-None values.
|
||||
|
||||
"""Hash a series of non-None values.
|
||||
|
||||
For example:
|
||||
>>> hash_values('some', 'values', 'to', 'hash')
|
||||
'f37f5dc65beaaea35af05e16e26d439fd150c576'
|
||||
|
|
|
|||
Loading…
Reference in New Issue