From d0eaa8ebe093c8db519f915a8970aa9d1ced7433 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Wed, 24 Aug 2011 14:50:04 -0400 Subject: [PATCH] adding __repr__() and __str__() methods to Site, Page, Category, and User --- bot/wiki/category.py | 10 ++++++++++ bot/wiki/page.py | 10 ++++++++++ bot/wiki/site.py | 25 +++++++++++++++++++++++++ bot/wiki/user.py | 8 ++++++++ 4 files changed, 53 insertions(+) diff --git a/bot/wiki/category.py b/bot/wiki/category.py index 745c90c..578ae01 100644 --- a/bot/wiki/category.py +++ b/bot/wiki/category.py @@ -16,6 +16,16 @@ class Category(Page): members -- returns a list of titles in the category """ + def __repr__(self): + """Returns the canonical string representation of the Category.""" + res = ", ".join(("Category(title={0!r}", "follow_redirects={1!r}", + "site={2!r})")) + return res.format(self._title, self._follow_redirects, self._site) + + def __str__(self): + """Returns a nice string representation of the Category.""" + return ''.format(self.title(), str(self._site)) + def members(self, limit=50): """Returns a list of titles in the category. diff --git a/bot/wiki/page.py b/bot/wiki/page.py index 19cfac3..a917747 100644 --- a/bot/wiki/page.py +++ b/bot/wiki/page.py @@ -81,6 +81,16 @@ class Page(object): else: self._is_talkpage = self._namespace % 2 == 1 + def __repr__(self): + """Returns the canonical string representation of the Page.""" + res = ", ".join(("Page(title={0!r}", "follow_redirects={1!r}", + "site={2!r})")) + return res.format(self._title, self._follow_redirects, self._site) + + def __str__(self): + """Returns a nice string representation of the Page.""" + return ''.format(self.title(), str(self._site)) + def _force_validity(self): """Used to ensure that our page's title is valid. diff --git a/bot/wiki/site.py b/bot/wiki/site.py index bca2807..476159e 100644 --- a/bot/wiki/site.py +++ b/bot/wiki/site.py @@ -96,6 +96,31 @@ class Site(object): if logged_in_as is None or name != logged_in_as: self._login(login) + def __repr__(self): + """Returns the canonical string representation of the Site.""" + res = ", ".join(( + "Site(name={_name!r}", "project={_project!r}", "lang={_lang!r}", + "base_url={_base_url!r}", "article_path={_article_path!r}", + "script_path={_script_path!r}", "assert_edit={_assert_edit!r}", + "maxlag={_maxlag!r}", "sql={_sql!r}", "login={0}", + "user_agent={2!r}", "cookiejar={1})" + )) + name, password = self._login_info + login = "({0}, {1})".format(repr(name), "hidden" if password else None) + cookies = self._cookiejar.__class__.__name__ + try: + cookies += "({0!r})".format(self._cookiejar.filename) + except AttributeError: + cookies += "()" + agent = self._opener.addheaders[0][1] + return res.format(login, cookies, agent, **self.__dict__) + + def __str__(self): + """Returns a nice string representation of the Site.""" + res = "" + return res.format(self.name(), self.project(), self.lang(), + self.domain()) + def _api_query(self, params, tries=0, wait=5): """Do an API query with `params` as a dict of parameters. diff --git a/bot/wiki/user.py b/bot/wiki/user.py index da14255..f65b9fc 100644 --- a/bot/wiki/user.py +++ b/bot/wiki/user.py @@ -45,6 +45,14 @@ class User(object): self._site = site self._name = name + def __repr__(self): + """Returns the canonical string representation of the User.""" + return "User(name={0!r}, site={1!r})".format(self._name, self._site) + + def __str__(self): + """Returns a nice string representation of the User.""" + return ''.format(self.name(), str(self._site)) + def _get_attribute(self, attr, force): """Internally used to get an attribute by name.