473,383 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 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 2660
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 data to a MySQL database to have a faster access....
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 about it's type. class base { public:...
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 different method. I need the facility to add different...
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 Nothing I assume no memory is set aside nor GC'ed But...
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 included in a class object. The data file looks...
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 possible to call a Java method from JavaScript, passing...
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 framework. Basically, the site will examine the URI and...
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 fine, but what I am looking for is to take the string...
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 currently using the following code: def main(argv =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.