Просмотр исходного кода

adding __repr__() and __str__() methods to Site, Page, Category, and User

tags/v0.1^2
Ben Kurtovic 13 лет назад
Родитель
Сommit
d0eaa8ebe0
4 измененных файлов: 53 добавлений и 0 удалений
  1. +10
    -0
      bot/wiki/category.py
  2. +10
    -0
      bot/wiki/page.py
  3. +25
    -0
      bot/wiki/site.py
  4. +8
    -0
      bot/wiki/user.py

+ 10
- 0
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 '<Category "{0}" of {1}>'.format(self.title(), str(self._site))

def members(self, limit=50):
"""Returns a list of titles in the category.



+ 10
- 0
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 '<Page "{0}" of {1}>'.format(self.title(), str(self._site))

def _force_validity(self):
"""Used to ensure that our page's title is valid.



+ 25
- 0
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 = "<Site {0} ({1}:{2}) at {3}>"
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.



+ 8
- 0
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 '<User "{0}" of {1}>'.format(self.name(), str(self._site))

def _get_attribute(self, attr, force):
"""Internally used to get an attribute by name.



Загрузка…
Отмена
Сохранить