Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 22 ■ PROJECT 3: XML FOR ALL OCCASIONS 435 def characters(self, chars): if self.passthrough: self.out.write(chars) def defaultStart(self, name, attrs): if self.passthrough: self.out.write('') def defaultEnd(self, name): if self.passthrough: self.out.write('' % name) def startDirectory(self, attrs): self.directory.append(attrs['name']) self.ensureDirectory() def endDirectory(self): self.directory.pop() def startPage(self, attrs): filename = os.path.join(*self.directory+[attrs['name']+'.html']) self.out = open(filename, 'w') self.writeHeader(attrs['title']) self.passthrough = True def endPage(self): self.passthrough = False self.writeFooter() self.out.close() def writeHeader(self, title): self.out.write('\n \n ') self.out.write(title) self.out.write('\n \n \n') def writeFooter(self): self.out.write('\n \n\n') parse('website.xml', WebsiteConstructor('public_html'))
436 CHAPTER 22 ■ PROJECT 3: XML FOR ALL OCCASIONS Listing 22-4. The Files and Directories Created public_html/ public_html/index.html public_html/interests public_html/interests/shouting.html public_html/interests/sleeping.html public_html/interests/eating.html ENCODING BLUES If your XML file contains special characters (those with ordinal numbers above 127), you may be in trouble. The XML parser uses Unicode strings during its processing, and returns those to you (for example, in the characters event handler). Unicode handles the special characters just fine. However, if you want to convert this Unicode string to an ordinary string (which is what happens when you print it, for example), an exception is raised (assuming that your default encoding is ASCII): >>> some_string = u'Möööse' >>> some_string u'M\xf6\xf6\xf6se' >>> print some_string Traceback (most recent call last): File "", line 1, in ? UnicodeError: ASCII encoding error: ordinal not in range(128) As you can see, the error message is “ASCII encoding error,” which actually means that Python has tried to encode the Unicode string with the ASCII encoding, which isn’t possible when it contains special characters like this. (You can find the default encoding of your installation using the sys.getdefaultencoding function. You can also change it with the sys.setdefaultencoding, but only in the site-wide customization file called site.py.) Encoding is done with the encode method: >>> some_string.encode('ascii') Traceback (most recent call last): File "", line 1, in ? UnicodeError: ASCII encoding error: ordinal not in range(128) To solve this problem, you have to use another encoding—for example, ISO8859-1 (which is fine for most European languages): >>> print some_string.encode('iso8859-1') Möööse (The actual appearance of the output will depend on your terminal emulator.) You can find more information about such encodings at the World Wide Web Consortium’s Web site (http://www.w3.org/International/O-charset.html).
- Page 416 and 417: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 442 and 443: CHAPTER 21 ■ ■ ■ Project 2: P
- Page 444 and 445: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 446 and 447: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 448 and 449: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 450 and 451: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 452 and 453: CHAPTER 22 ■ ■ ■ Project 3: X
- Page 454 and 455: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 456 and 457: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 458 and 459: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 460 and 461: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 462 and 463: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 464 and 465: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 468: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 471 and 472: 440 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 473 and 474: 442 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 475 and 476: 444 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 477 and 478: 446 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 479 and 480: 448 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 481 and 482: 450 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 483 and 484: 452 CHAPTER 23 ■ PROJECT 4: IN TH
- Page 486 and 487: CHAPTER 24 ■ ■ ■ Project 5: A
- Page 488 and 489: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 490 and 491: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 492 and 493: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 494 and 495: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 496 and 497: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 498 and 499: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 500 and 501: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 502 and 503: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 504 and 505: CHAPTER 25 ■ ■ ■ Project 6: R
- Page 506 and 507: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 508 and 509: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 510 and 511: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 512: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 515 and 516: 484 CHAPTER 26 ■ PROJECT 7: YOUR
CHAPTER 22 ■ PROJECT 3: XML FOR ALL OCCASIONS 435<br />
def characters(self, chars):<br />
if self.passthrough: self.out.write(chars)<br />
def defaultStart(self, name, attrs):<br />
if self.passthrough:<br />
self.out.write('')<br />
def defaultEnd(self, name):<br />
if self.passthrough:<br />
self.out.write('' % name)<br />
def startDirec<strong>to</strong>ry(self, attrs):<br />
self.direc<strong>to</strong>ry.append(attrs['name'])<br />
self.ensureDirec<strong>to</strong>ry()<br />
def endDirec<strong>to</strong>ry(self):<br />
self.direc<strong>to</strong>ry.pop()<br />
def startPage(self, attrs):<br />
filename = os.path.join(*self.direc<strong>to</strong>ry+[attrs['name']+'.html'])<br />
self.out = open(filename, 'w')<br />
self.writeHeader(attrs['title'])<br />
self.passthrough = True<br />
def endPage(self):<br />
self.passthrough = False<br />
self.writeFooter()<br />
self.out.close()<br />
def writeHeader(self, title):<br />
self.out.write('\n \n ')<br />
self.out.write(title)<br />
self.out.write('\n \n \n')<br />
def writeFooter(self):<br />
self.out.write('\n \n\n')<br />
parse('website.xml', WebsiteConstruc<strong>to</strong>r('public_html'))