implement convenience wrapper for text.extract functionality

This commit is contained in:
Mike Fährmann
2019-04-19 22:30:11 +02:00
parent 8f249f1d54
commit 21a7e395a7
2 changed files with 31 additions and 0 deletions

View File

@@ -149,6 +149,20 @@ def extract_iter(txt, begin, end, pos=0):
return
def extract_from(txt, pos=0, default=""):
"""Returns a function object that extracts from 'txt'"""
def extr(begin, end, index=txt.index, txt=txt):
nonlocal pos
try:
first = index(begin, pos) + len(begin)
last = index(end, first)
pos = last + len(end)
return txt[first:last]
except (ValueError, TypeError, AttributeError):
return default
return extr
def parse_bytes(value, default=0, suffixes="bkmgtp"):
"""Convert a bytes-amount ("500k", "2.5M", ...) to int"""
try: