1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-25 04:02:32 +01:00

add 'default' argument to 'text.extr()'

This commit is contained in:
Mike Fährmann 2022-11-09 11:00:32 +01:00
parent e326029355
commit 05255f5be0
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 7 additions and 2 deletions

View File

@ -120,13 +120,13 @@ def extract(txt, begin, end, pos=0):
return None, pos
def extr(txt, begin, end):
def extr(txt, begin, end, default=""):
"""Stripped-down version of 'extract()'"""
try:
first = txt.index(begin) + len(begin)
return txt[first:txt.index(end, first)]
except (ValueError, TypeError, AttributeError):
return ""
return default
def rextract(txt, begin, end, pos=-1):

View File

@ -210,6 +210,11 @@ class TestText(unittest.TestCase):
self.assertEqual(f(txt, "<", ">"), "a")
self.assertEqual(f(txt, "><", ">"), "b")
# 'default' argument
self.assertEqual(f(txt, "<", "X", None), None)
self.assertEqual(f(txt, "<", "X", default=None), None)
self.assertEqual(f(txt, "<", "X", default=()), ())
# invalid arguments
for value in INVALID:
self.assertEqual(f(value, "<" , ">") , "")