1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-23 03:02:50 +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()"""
index = txt.index
lbeg = len(begin)
lend = len(end)
try:
while True: while True:
value, pos = extract(txt, begin, end, pos) first = index(begin, pos) + lbeg
if value is None: last = index(end, first)
pos = last + lend
yield txt[first:last]
except (ValueError, TypeError, AttributeError):
return return
yield value
def parse_bytes(value, default=0, suffixes="bkmgtp"): def parse_bytes(value, default=0, suffixes="bkmgtp"):