469,312 Members | 2,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

Passing data out of a Sax parser

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.
Jul 18 '05 #1
6 2565
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
da***@dalkescientific.com
Jul 18 '05 #2
"Andrew Dalke" <ad****@mindspring.com> 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
Jul 18 '05 #3
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
da***@dalkescientific.com
Jul 18 '05 #4
"Andrew Dalke" <ad****@mindspring.com> 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
Jul 18 '05 #5
On Sat, 20 Sep 2003 00:53:06 GMT, "Andrew Dalke"
<ad****@mindspring.com> 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.
Jul 18 '05 #6
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
da***@dalkescientific.com
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Fabian Kr?ger | last post: by
6 posts views Thread by Gernot Frisch | last post: by
1 post views Thread by Treefrog | last post: by
1 post views Thread by Eric Capps | last post: by
7 posts views Thread by amygdala | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.