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:
parent
e326029355
commit
05255f5be0
@ -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):
|
||||
|
@ -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, "<" , ">") , "")
|
||||
|
Loading…
Reference in New Issue
Block a user