1
0
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:
Mike Fährmann 2022-04-08 14:40:26 +02:00
parent 4527a35aba
commit 7fe54bab2a
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 14 additions and 13 deletions

View File

@ -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

View File

@ -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):