[enh] add re-usable func to filter text

This commit is contained in:
Allen 2024-05-23 23:21:58 +00:00 committed by Markus Heiser
parent 0fb3f0e4ae
commit 0fa81fc782
6 changed files with 53 additions and 25 deletions

View file

@ -2,6 +2,9 @@
"""Utility functions for the engines
"""
from __future__ import annotations
import re
import importlib
import importlib.util
@ -371,6 +374,35 @@ def convert_str_to_int(number_str: str) -> int:
return 0
def extr(txt: str, begin: str, end: str, default: str = ""):
"""Extract the string between ``begin`` and ``end`` from ``txt``
:param txt: String to search in
:param begin: First string to be searched for
:param end: Second string to be searched for after ``begin``
:param default: Default value if one of ``begin`` or ``end`` is not
found. Defaults to an empty string.
:return: The string between the two search-strings ``begin`` and ``end``.
If at least one of ``begin`` or ``end`` is not found, the value of
``default`` is returned.
Examples:
>>> extr("abcde", "a", "e")
"bcd"
>>> extr("abcde", "a", "z", deafult="nothing")
"nothing"
"""
# From https://github.com/mikf/gallery-dl/blob/master/gallery_dl/text.py#L129
try:
first = txt.index(begin) + len(begin)
return txt[first : txt.index(end, first)]
except ValueError:
return default
def int_or_zero(num: Union[List[str], str]) -> int:
"""Convert num to int or 0. num can be either a str or a list.
If num is a list, the first element is converted to int (or return 0 if the list is empty).