From cf0848cb22e2dcf42846ce2d92b182b166e5fc59 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Fri, 11 Apr 2014 16:04:46 +0200 Subject: [PATCH] =?utf8?q?Recherche=20de=20regexp/auteur=20(=C3=A9ventuell?= =?utf8?q?ement=20par=20regexp)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- quotes.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/quotes.py b/quotes.py index 1365b12..c1aca76 100644 --- a/quotes.py +++ b/quotes.py @@ -99,6 +99,37 @@ class QuoteDB(object): def randomfrom(self, author): """ Sort une quote aléatoire de ``author`` """ return random.choice([q for q in self.quotelist if q.author == 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] + return l + + def search_authors(self, author=None, regexp=False): + """Renvoie la liste des auteur 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])) + return l def dump(quotedb, dump_file=None): """Pour exporter les quotes dans un format readable vers un fichier.""" -- 2.39.2