[Tutor] Re: Regex [example:HTMLParser, unittest, StringIO]

Andrei project5 at redrival.net
Tue Aug 26 01:14:38 EDT 2003


Danny Yoo wrote:
> Hmm... The example in:
> 
>     http://mail.python.org/pipermail/tutor/2003-August/024902.html
> 
> should be really close to what you're looking for.

I didn't (and still don't) see how to extend that to suit my purposes.

> Here's another example that shows how to use the handle_starttag() and
> handle_endtag() methods.  The example also shows how we can use "unit
> tests" to make sure our class is doing the right thing.
> 
> 
> ###
> class Parser(HTMLParser.HTMLParser):
>     """A small example for HTMLParser that pays attention to anchored
>        text."""
>     def __init__(self):
>         HTMLParser.HTMLParser.__init__(self)
>         self.in_anchor = False
> 
>     def handle_starttag(self, tag, attrs):
>         if tag == 'a':
>             self.in_anchor = True
> 
>     def handle_endtag(self, tag):
>         if tag == 'a':
>             self.in_anchor = False
> 
>     def handle_data(self, data):
>         if self.in_anchor:
>             print "Anchored text:", data

<snip>

Excellent example (that in_anchor bit did the trick, only I need to 
check for not in_anchor), now I might be able to solve my problem. 
Thanks, also for reminding me about unit tests. I should write some of 
those myself :).

Andrei


=====
Mail address in header catches spam. Real contact info (decode with rot13):
cebwrpg5 at bcrenznvy.pbz. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V 
ernq gur yvfg, fb gurer'f ab arrq gb PP.





More information about the Tutor mailing list