Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing data out of a Sax parser

Reply
Thread Tools

Passing data out of a Sax parser

 
 
Tim Rowe
Guest
Posts: n/a
 
      09-19-2003
Is there a preferred way of passing data out of a Sax parser? My Sax
content handler constructa an object for the rest of the program to
use, but I'm not sure how to pass it to the rest of the program. Ways
I can see:

a. Put it in a global (blech!)

b. Add a parameter to the handler's __init__ method, that takes some
sort of mutable object, and put the answer into that object.

Is it one of these that I should do? I suppose I could pass in the
owning parser, and put the result into a new member of the parser, but
I wonder if I'm missing something.

TIA.
 
Reply With Quote
 
 
 
 
Andrew Dalke
Guest
Posts: n/a
 
      09-20-2003
Tim Rowe:
> b. Add a parameter to the handler's __init__ method, that takes some
> sort of mutable object, and put the answer into that object.


Either that or have startDocument create the mutable object, as

class MyHandler(xml.sax.handlers.ContentHandler):
def startDocument(self):
self.count = 0
def startElement(self, name, attrs):
if name == "spam":
self.count += 1

parser = xml.sax.make_parser()
h = MyHandler()
parser.setContentHandler(h)
h.parse(input)
print h.count

Andrew



 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      09-21-2003
"Andrew Dalke" <> writes:

> Tim Rowe:
> > b. Add a parameter to the handler's __init__ method, that takes some
> > sort of mutable object, and put the answer into that object.

>
> Either that or have startDocument create the mutable object, as
>
> class MyHandler(xml.sax.handlers.ContentHandler):
> def startDocument(self):
> self.count = 0
> def startElement(self, name, attrs):
> if name == "spam":
> self.count += 1
>
> parser = xml.sax.make_parser()
> h = MyHandler()
> parser.setContentHandler(h)
> h.parse(input)
> print h.count


Works, but integers aren't mutable.


John
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      09-22-2003
Me:
> > if name == "spam":
> > self.count += 1


John J. Lee:
> Works, but integers aren't mutable.


I assume you refer to the snippet I posted above?

Since it works, I don't understand the need for
your comment.

True, integers aren't mutable, so += does nothing
to the integer. Since __iadd__ isn't defined, the
Python runtime turns it into the equivalent of

self.count = self.count + 1

and so does what is expected.

Andrew



 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      09-22-2003
"Andrew Dalke" <> writes:

> Me:
> > > if name == "spam":
> > > self.count += 1

>
> John J. Lee:
> > Works, but integers aren't mutable.

>
> I assume you refer to the snippet I posted above?
>
> Since it works, I don't understand the need for
> your comment.

[...]

It was just a nit: you said (indirectly) that integers are mutable:


| Either that or have startDocument create the mutable object, as
[...]
| self.count = 0
[...]
| self.count += 1
[...]


John
 
Reply With Quote
 
Tim Rowe
Guest
Posts: n/a
 
      09-22-2003
On Sat, 20 Sep 2003 00:53:06 GMT, "Andrew Dalke"
<> wrote:

>Tim Rowe:
>> b. Add a parameter to the handler's __init__ method, that takes some
>> sort of mutable object, and put the answer into that object.

>
>Either that or have startDocument create the mutable object, as
>
>class MyHandler(xml.sax.handlers.ContentHandler):
> def startDocument(self):
> self.count = 0
> def startElement(self, name, attrs):
> if name == "spam":
> self.count += 1
>
>parser = xml.sax.make_parser()
>h = MyHandler()
>parser.setContentHandler(h)
>h.parse(input)
>print h.count


Ah! Of course! Thanks, I should have thought of that. As others
have pointed out, startDocument can create any sort of an object
there, not just a mutable, of course; it was my solution that required
a mutable.
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      09-23-2003
John J. Lee:
> It was just a nit: you said (indirectly) that integers are mutable:
>
>
> | Either that or have startDocument create the mutable object, as
> [...]
> | self.count = 0


Indeed I did. In my head I was thinking "the things which change
when events come in" and that got converted to "mutable" when
I wrote it out.

Andrew



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why SAX parser reads truncated data ? Sanjeev Java 4 08-18-2008 08:56 AM
How to get Comments into SAX Parser? Bernard Java 2 11-14-2003 06:43 PM
Re: Illegal XML character SAX Parser exception William Brogden Java 1 06-30-2003 06:11 AM
Re: Illegal XML character SAX Parser exception Michael Lee Java 0 06-27-2003 03:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57