0

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.

  1. 2 classes are created a) Wiki and b)Page
  2. Page classes stores all the information related to a page.

Questions:

  1. We create a method object -homepage but how does the homepage.save() works..homepage is a object of Wiki and save() is a method of page object. how come it works?

Any clarification would be really helpful

mechanical_meat
170k25 gold badges238 silver badges231 bronze badges
asked Jun 29, 2012 at 18:11

2 Answers 2

2

homepage is a object of Wiki

Incorrect. Wiki.getPage() is a factory method that returns an instance of Page.

answered Jun 29, 2012 at 18:17
Sign up to request clarification or add additional context in comments.

Comments

0

wiki.getPage() returns a Page object, not a wiki. Therefore, homepage does indeed have the necessary method save.

answered Jun 29, 2012 at 18:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.