From ef4f3b6514727767ca00aa374ee7a00230419076 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Sat, 12 Mar 2016 14:07:09 +0100 Subject: [PATCH] =?utf8?q?Refonte=20du=20syst=C3=A8me=20de=20recherche=20:?= =?utf8?q?=20moins=20de=20code,=20plus=20propre,=20et=20case=20insensitive?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- quotes.py | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/quotes.py b/quotes.py index e8e4061..085423f 100644 --- a/quotes.py +++ b/quotes.py @@ -185,34 +185,31 @@ class QuoteDB(object): return random.choice(self.quotesfrom(author)) def search(self, inquote=None, author=None, regexp=False): - """Fait une recherche dans les quotes.""" - if regexp: - if inquote is None: - inquote = ".*" - if author is None: - author = ".*" - qreg = re.compile(inquote, flags=re.UNICODE) - areg = re.compile(author, flags=re.UNICODE) - l = [q for q in self.quotelist if qreg.match(q.content) and areg.match(q.author)] - else: - if inquote is None: - inquote = "" - if author is None: - author = "" - l = [q for q in self.quotelist if inquote in q.content and author in q.author] + """ + Fait une recherche dans les quotes. + C'est une disjonction de cas : on garde la quote si + ``inquote`` matche dans le contenu + *ou* si ``author`` matche l'auteur + """ + params = [inquote, author] + regexps = [] + for param in params: + if param is None: + param = u".*" + elif not regexp: + param = u".*%s.*" % param + regexps.append(re.compile(param, flags=re.UNICODE + re.IGNORECASE)) + l = [q for q in self.quotelist if all([reg.match(truc) for (reg, truc) in zip(regexps, [q.content, q.author])])] return l def search_authors(self, author=None, regexp=False): """Renvoie la liste des auteurs contenant ``author`` ou qui matchent la regexp.""" - if regexp: - if author is None: - author = ".*" - areg = re.compile(author, flags=re.UNICODE) - l = list(set([q.author for q in self.quotelist if areg.match(q.author)])) - else: - if author is None: - author = "" - l = list(set([q.author for q in self.quotelist if author in q.author])) + if author is None: + author = u".*" + elif not regexp: + author = u".*%s.*" % author + areg = re.compile(author, flags=re.UNICODE + re.IGNORECASE) + l = list(set([q.author for q in self.quotelist if areg.match(q.author)])) return l def dump(quotedb, dump_file=None): -- 2.39.2