Passing data out of a Sax parser

T

Tim Rowe

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.
 
A

Andrew Dalke

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
(e-mail address removed)
 
J

John J. Lee

Andrew Dalke said:
Tim Rowe:

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
 
A

Andrew Dalke

Me:
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
(e-mail address removed)
 
J

John J. Lee

Andrew Dalke said:
Me:

John J. Lee:

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
 
T

Tim Rowe

Tim Rowe:

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.
 
A

Andrew Dalke

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top