Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

edited tags
Link
mechanical_meat
  • 170.5k
  • 25
  • 238
  • 231
Source Link
user1050619
  • 21k
  • 89
  • 255
  • 432

Python classes clarification

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

lang-py

AltStyle によって変換されたページ (->オリジナル) /