From 8e3f5410b7d45ceb50142c2e750da27341915be4 Mon Sep 17 00:00:00 2001 From: Dpeta <69427753+Dpeta@users.noreply.github.com> Date: Thu, 23 Feb 2023 01:25:13 +0100 Subject: [PATCH] Chain iterators and extend when excluding links from quirks --- dataobjs.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/dataobjs.py b/dataobjs.py index 721ee5e..bfd65b4 100644 --- a/dataobjs.py +++ b/dataobjs.py @@ -1,4 +1,8 @@ +import re +import random +import itertools import logging +from datetime import datetime PchumLog = logging.getLogger("pchumLogger") try: @@ -6,9 +10,6 @@ try: except ImportError: print("PyQt5 fallback (dataobjs.py)") from PyQt5 import QtGui -from datetime import datetime -import re -import random from mood import Mood from parsetools import ( @@ -167,20 +168,17 @@ class pesterQuirks: if checkstate == 2: # Check for substring that should be excluded. excludes = [] - # Check for links, store in list. - for match in re.finditer(_urlre, string): - excludes.append(match) - # Check for smilies, store in list. - for match in re.finditer(_smilere, string): - excludes.append(match) - # Check for @handles, store in list. - for match in re.finditer(_handlere, string): - excludes.append(match) - # Check for #memos, store in list. - for match in re.finditer(_memore, string): - excludes.append(match) + # Return matches for links, smilies, handles, memos. + # Chain the iterators and add to excludes list. + matches = itertools.chain( + re.finditer(_urlre, string), + re.finditer(_smilere, string), + re.finditer(_handlere, string), + re.finditer(_memore, string), + ) + excludes.extend(matches) - if len(excludes) >= 1: + if excludes: # SORT !!! excludes.sort(key=lambda exclude: exclude.start()) # Recursion check.