From 05255f5be073e893b6b2c109ef5e279958fdd221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20F=C3=A4hrmann?= Date: Wed, 9 Nov 2022 11:00:32 +0100 Subject: [PATCH] add 'default' argument to 'text.extr()' --- gallery_dl/text.py | 4 ++-- test/test_text.py | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/gallery_dl/text.py b/gallery_dl/text.py index cbba063d..1fb1851a 100644 --- a/gallery_dl/text.py +++ b/gallery_dl/text.py @@ -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): diff --git a/test/test_text.py b/test/test_text.py index a1b30887..2c0be3bf 100644 --- a/test/test_text.py +++ b/test/test_text.py @@ -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, "<" , ">") , "")