Passing data out of a Sax parser

Discussion in 'Python' started by Tim Rowe, Sep 20, 2003.

  1. Tim Rowe

    Tim Rowe Guest

    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.
    Tim Rowe, Sep 20, 2003
    #1
    1. Advertising

  2. Tim Rowe

    Andrew Dalke Guest

    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
    Andrew Dalke, Sep 20, 2003
    #2
    1. Advertising

  3. Tim Rowe

    John J. Lee Guest

    "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
    John J. Lee, Sep 21, 2003
    #3
  4. Tim Rowe

    Andrew Dalke Guest

    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
    Andrew Dalke, Sep 22, 2003
    #4
  5. Tim Rowe

    John J. Lee Guest

    "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
    John J. Lee, Sep 22, 2003
    #5
  6. Tim Rowe

    Tim Rowe Guest

    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.
    Tim Rowe, Sep 22, 2003
    #6
  7. Tim Rowe

    Andrew Dalke Guest

    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
    Andrew Dalke, Sep 23, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Brogden
    Replies:
    1
    Views:
    8,336
    Manoj S. P.
    Jun 30, 2003
  2. Michael Lee
    Replies:
    0
    Views:
    1,536
    Michael Lee
    Jun 27, 2003
  3. Bernard
    Replies:
    2
    Views:
    1,040
    Bernard
    Nov 14, 2003
  4. Martin Schlatter

    Encoding problem with SAX parser

    Martin Schlatter, Dec 10, 2003, in forum: Java
    Replies:
    2
    Views:
    835
    Martin Schlatter
    Dec 14, 2003
  5. Sanjeev
    Replies:
    4
    Views:
    1,049
    Mike Schilling
    Aug 18, 2008
Loading...

Share This Page