472,353 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 2611
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Fabian Kr?ger | last post by:
Hello, I got a weird problem and need your help and ideas... I´ve written an php application which imports data in XML format and writes this...
6
by: Gernot Frisch | last post by:
I want to have a class that provides 2 methods with the same name (do1, do2) that cann be called from a function (Fkt) but Fkt does not know/care...
1
by: Treefrog | last post by:
Hi, I have a program that creates some data, this data needs to be parsed but depending on the data created, it'll need to be parsed in a...
61
by: academic | last post by:
When I declare a reference variable I initialize it to Nothing. Now I'm wondering if that best for String variables - is "" better? With...
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be...
1
by: Eric Capps | last post by:
This may be more of a Java question, but I feel that JavaScript experts may be more qualified to help me find a solution. In short: is it...
7
by: amygdala | last post by:
Hi all, I'm starting this new project in which I'ld like to implement sort of a design pattern I have seen being used in the CodeIgniter...
0
by: balean | last post by:
Hi guys, I need help. I am trying to extract data from XML file. I am using all the fucntions needed to parse the xml data every thing working...
4
by: cjt22 | last post by:
Hi there. I just wondered whether anyone could recommend the correct way I should be passing command line parameters into my program. I am...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.