mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 02:32:33 +01:00
attempt to fix some issues with 'contains()' (#2446)
add a third argument that gets used when the values o search are given as a string
This commit is contained in:
parent
4527a35aba
commit
7fe54bab2a
@ -71,21 +71,17 @@ def unique_sequence(iterable):
|
||||
yield element
|
||||
|
||||
|
||||
def contains(values, elements):
|
||||
def contains(values, elements, separator=" "):
|
||||
"""Returns True if at least one of 'elements' is contained in 'values'"""
|
||||
if not isinstance(elements, (tuple, list)):
|
||||
elements = (elements,)
|
||||
|
||||
if isinstance(values, str):
|
||||
fmt = r"\b{}\b".format
|
||||
for e in elements:
|
||||
if re.compile(fmt(re.escape(e))).search(values):
|
||||
return True
|
||||
else:
|
||||
for e in elements:
|
||||
if e in values:
|
||||
return True
|
||||
values = values.split(separator)
|
||||
|
||||
if not isinstance(elements, (tuple, list)):
|
||||
return elements in values
|
||||
|
||||
for e in elements:
|
||||
if e in values:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
@ -328,13 +328,18 @@ class TestOther(unittest.TestCase):
|
||||
self.assertFalse(util.contains(c, "bar"))
|
||||
self.assertFalse(util.contains(c, [2, 5, "bar"]))
|
||||
|
||||
s = "1, 2, 3, asd, qwe, y(+)c, f(+)(-), bar"
|
||||
s = "1 2 3 asd qwe y(+)c f(+)(-) bar"
|
||||
self.assertTrue(util.contains(s, "y(+)c"))
|
||||
self.assertTrue(util.contains(s, ["asd", "qwe", "yxc"]))
|
||||
self.assertTrue(util.contains(s, ["sdf", "dfg", "qwe"]))
|
||||
self.assertFalse(util.contains(s, "tag1"))
|
||||
self.assertFalse(util.contains(s, ["tag1", "tag2", "tag3"]))
|
||||
|
||||
s = "1, 2, 3, asd, qwe, y(+)c, f(+)(-), bar"
|
||||
self.assertTrue(util.contains(s, "y(+)c", ", "))
|
||||
self.assertTrue(util.contains(s, ["sdf", "dfg", "qwe"], ", "))
|
||||
self.assertFalse(util.contains(s, "tag1", ", "))
|
||||
|
||||
def test_raises(self):
|
||||
func = util.raises(Exception)
|
||||
with self.assertRaises(Exception):
|
||||
|
Loading…
Reference in New Issue
Block a user