Skip to content

Commit

Permalink
attempt to fix some issues with 'contains()' (#2446)
Browse files Browse the repository at this point in the history
add a third argument that gets used
when the values o search are given as a string
  • Loading branch information
mikf committed Apr 8, 2022
1 parent 4527a35 commit 7fe54ba
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
20 changes: 8 additions & 12 deletions gallery_dl/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
7 changes: 6 additions & 1 deletion test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,18 @@ def test_contains(self):
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):
Expand Down

0 comments on commit 7fe54ba

Please sign in to comment.