我在一本书的下面看到了这段代码并且想知道它是如何工作的......我试图了解自己,但却陷入了困境......我知道它更像是一个教学问题,但希望在论坛中能够帮帮我......
代码:
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 shell中:
> > > 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()
这是我的理解。
问题:
任何澄清都会非常有用
答案 0 :(得分:2)
主页是Wiki
的对象
不正确的。 Wiki.getPage()
是一个返回Page
。
答案 1 :(得分:0)
wiki.getPage()
返回一个Page对象,而不是一个wiki。因此,homepage
确实具有必要的方法save
。