From d87c226417f08716713e6f190f72d8b59d2eef35 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 6 Jul 2012 21:50:57 -0400 Subject: [PATCH] __repr__ and __str__ for everything per #5 and #22. --- earwigbot/wiki/copyvios/__init__.py | 11 +++++++++-- earwigbot/wiki/copyvios/markov.py | 19 +++++++++++++++++++ earwigbot/wiki/copyvios/parsers.py | 9 +++++++++ earwigbot/wiki/copyvios/search.py | 8 ++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/earwigbot/wiki/copyvios/__init__.py b/earwigbot/wiki/copyvios/__init__.py index 30d4681..f85ab22 100644 --- a/earwigbot/wiki/copyvios/__init__.py +++ b/earwigbot/wiki/copyvios/__init__.py @@ -48,8 +48,15 @@ class CopyvioCheckResult(object): self.delta_chain = chains[1] def __repr__(self): - r = "CopyvioCheckResult(violation={0!r}, confidence={1!r}, url={2!r}, queries={3|r})" - return r.format(self.violation, self.confidence, self.url, self.queries) + """Return the canonical string representation of the result.""" + res = "CopyvioCheckResult(violation={0!r}, confidence={1!r}, url={2!r}, queries={3|r})" + return res.format(self.violation, self.confidence, self.url, + self.queries) + + def __str__(self): + """Return a nice string representation of the result.""" + res = "" + return res.format(self.violation, self.confidence) class CopyvioMixIn(object): diff --git a/earwigbot/wiki/copyvios/markov.py b/earwigbot/wiki/copyvios/markov.py index 74783d0..081469f 100644 --- a/earwigbot/wiki/copyvios/markov.py +++ b/earwigbot/wiki/copyvios/markov.py @@ -42,6 +42,14 @@ class MarkovChain(object): except KeyError: pass + def __repr__(self): + """Return the canonical string representation of the MarkovChain.""" + return "MarkovChain(text={0!r})".format(self.text) + + def __str__(self): + """Return a nice string representation of the MarkovChain.""" + return "".format(self.size()) + def size(self): count = 0 for node in self.chain.itervalues(): @@ -53,6 +61,7 @@ class MarkovChain(object): class MarkovChainIntersection(MarkovChain): def __init__(self, mc1, mc2): self.chain = defaultdict(lambda: defaultdict(lambda: 0)) + self.mc1, self.mc2 = mc1, mc2 c1 = mc1.chain c2 = mc2.chain @@ -63,3 +72,13 @@ class MarkovChainIntersection(MarkovChain): if node in nodes2: count2 = nodes2[node] self.chain[word][node] = min(count1, count2) + + def __repr__(self): + """Return the canonical string representation of the intersection.""" + res = "MarkovChainIntersection(mc1={0!r}, mc2={1!r})" + return res.format(self.mc1, self.mc2) + + def __str__(self): + """Return a nice string representation of the intersection.""" + res = "" + return res.format(self.size(), self.mc1, self.mc2) diff --git a/earwigbot/wiki/copyvios/parsers.py b/earwigbot/wiki/copyvios/parsers.py index 9e97267..0c3c17b 100644 --- a/earwigbot/wiki/copyvios/parsers.py +++ b/earwigbot/wiki/copyvios/parsers.py @@ -23,6 +23,15 @@ __all__ = ["BaseTextParser", "ArticleTextParser", "HTMLTextParser"] class BaseTextParser(object): + def __repr__(self): + """Return the canonical string representation of the text parser.""" + return "{0}(text={1!r})".format(self.__class__.__name__, self.text) + + def __str__(self): + """Return a nice string representation of the text parser.""" + name = self.__class__.__name__ + return "<{0} of text with size {1}>".format(name, len(text)) + def __init__(self, text): self.text = text diff --git a/earwigbot/wiki/copyvios/search.py b/earwigbot/wiki/copyvios/search.py index d8091ee..4345b29 100644 --- a/earwigbot/wiki/copyvios/search.py +++ b/earwigbot/wiki/copyvios/search.py @@ -37,6 +37,14 @@ class BaseSearchEngine(object): """Store credentials 'cred' for searching later on.""" self.cred = cred + def __repr__(self): + """Return the canonical string representation of the search engine.""" + return "{0}()".format(self.__class__.__name__) + + def __str__(self): + """Return a nice string representation of the search engine.""" + return "<{0}>".format(self.__class__.__name__) + def search(self, query): """Use this engine to search for 'query'.