我在一本书中看到了下面的一段代码,并想知道它是如何工作的..我试图了解自己,但在几个地方陷入困境..我知道它更像是一个教学问题,但希望在论坛中能对我有所帮助。 .
代码:
import os
import re
class Wiki:
"A class representing a wiki as a whole."
HOME_PAGE_NAME = "HomePage"
def __init__(self, base):
"Initializes a wiki that uses the provided base directory."
self.base = base
if not os.path.exists(self.base):
os.makedirs(self.base)
elif not os.path.isdir(self.base):
raise IOError('Wiki base "%s" is not a directory!' % self.base)
def getPage(self, name=None):
"""Retrieves the given page for this wiki, which may or may not
currently exist."""
if not name:
name = self.HOME_PAGE_NAME
return Page(self, name)
class Page:
"""A class representing one page of a wiki, containing all the
logic necessary to manipulate that page and to determine which other
pages it references."""
#We consider a WikiWord any word beginning with a capital letter,
#containing at least one other capital letter, and containing only
#alphanumerics.
WIKI_WORD_MATCH = "(([A-Z][a-z0-9]*){2,})"
WIKI_WORD = re.compile(WIKI_WORD_MATCH)
WIKI_WORD_ALONE = re.compile('^%s$' % WIKI_WORD_MATCH)
def __init__(self, wiki, name):
"""Initializes the page for the given wiki with the given
name, making sure the name is valid. The page may or may not
actually exist right now in the wiki."""
#WIKI_WORD matches a WikiWord anywhere in the string. We want to make
#sure the page is a WikiWord and nothing else.
if not self.WIKI_WORD_ALONE.match(name):
raise(NotWikiWord, name)
self.wiki = wiki
self.name = name
self.path = os.path.join(self.wiki.base, name)
def exists(self):
"Returns true if there's a page for the wiki with this name."
return os.path.isfile(self.path)
def load(self):
"Loads this page from disk, if it exists."
if not hasattr(self, 'text'):
self.text = ''
if self.exists():
self.text = open(self.path, 'r').read()
def save(self):
"Saves this page. If it didn't exist before, it does now."
if not hasattr(self, 'text'):
self.text = ''
out = open(self.path, 'w')
out.write(self.text)
out.close()
def delete(self):
"Deletes this page, assuming it currently exists."
if self.exists():
os.remove(self.path)
def getText(self):
"Returns the raw text of this page."
self.load()
return self.text
class NotWikiWord(Exception):
"""Exception thrown when someone tries to pass off a non-WikiWord
as a WikiWord."""
pass
在 Python 外壳中:
> > > from BittyWiki import Wiki
> > > wiki = Wiki(“localwiki”)
> > > homePage = wiki.getPage()
> > > homePage.text = “Here ’ s the home page.\n\nIt links to PageTwo and
PageThree.”
> > > homePage.save()
这是我的理解。
- 创建了 2 个类 a) Wiki 和 b)Page
- 页面类存储与页面相关的所有信息。
问题:
- 我们创建了一个方法对象 -homepage 但 homepage.save() 是如何工作的。homepage 是 Wiki 的对象, save() 是页面对象的方法。它是怎么工作的?
任何澄清都会非常有帮助