Reputation: 20856
I saw this below piece of code in a book and would like to know how it works..I tried to understand myself but get stuck in few places..I know its more like a teaching question but hopefully in the forum will help me..
Code:
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
In 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()
This is my understanding.
Questions:
Any clarification would be really helpful
Upvotes: 0
Views: 87
Reputation: 995
wiki.getPage()
returns a Page object, not a wiki. Therefore, homepage
does indeed have the necessary method save
.
Upvotes: 0
Reputation: 798526
homepage is a object of Wiki
Incorrect. Wiki.getPage()
is a factory method that returns an instance of Page
.
Upvotes: 2