PageRenderTime 5ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Demo/xml/elem_count.py

http://unladen-swallow.googlecode.com/
Python | 36 lines | 26 code | 10 blank | 0 comment | 6 complexity | 2db5bb3c32e2e01981f380ae0b3063a8 MD5 | raw file
 1import sys
 2
 3from xml.sax import make_parser, handler
 4
 5class FancyCounter(handler.ContentHandler):
 6
 7    def __init__(self):
 8        self._elems = 0
 9        self._attrs = 0
10        self._elem_types = {}
11        self._attr_types = {}
12
13    def startElement(self, name, attrs):
14        self._elems = self._elems + 1
15        self._attrs = self._attrs + len(attrs)
16        self._elem_types[name] = self._elem_types.get(name, 0) + 1
17
18        for name in attrs.keys():
19            self._attr_types[name] = self._attr_types.get(name, 0) + 1
20
21    def endDocument(self):
22        print "There were", self._elems, "elements."
23        print "There were", self._attrs, "attributes."
24
25        print "---ELEMENT TYPES"
26        for pair in  self._elem_types.items():
27            print "%20s %d" % pair
28
29        print "---ATTRIBUTE TYPES"
30        for pair in  self._attr_types.items():
31            print "%20s %d" % pair
32
33
34parser = make_parser()
35parser.setContentHandler(FancyCounter())
36parser.parse(sys.argv[1])