1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 18:53:21 +01:00

improve text.extract_iter() performance

by roughly 40% through
- inlining code
- pre-calculating reused values
- entering a try-except block only once
This commit is contained in:
Mike Fährmann 2019-04-18 23:37:17 +02:00
parent e25ebc4bff
commit 8f249f1d54
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

View File

@ -135,12 +135,18 @@ def extract_all(txt, rules, pos=0, values=None):
def extract_iter(txt, begin, end, pos=0): def extract_iter(txt, begin, end, pos=0):
"""Yield all values obtained by repeated calls to text.extract""" """Yield values that would be returned by repeated calls of extract()"""
while True: index = txt.index
value, pos = extract(txt, begin, end, pos) lbeg = len(begin)
if value is None: lend = len(end)
return try:
yield value while True:
first = index(begin, pos) + lbeg
last = index(end, first)
pos = last + lend
yield txt[first:last]
except (ValueError, TypeError, AttributeError):
return
def parse_bytes(value, default=0, suffixes="bkmgtp"): def parse_bytes(value, default=0, suffixes="bkmgtp"):