Mercurial > cpython-lru_cache-weakref
annotate Demo/xml/elem_count.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | c3a096bfbd6b |
| children | a5d7b5277951 7d16b42fcdbd |
| rev | line source |
|---|---|
|
15241
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
1 import sys |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
2 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
3 from xml.sax import make_parser, handler |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
4 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
5 class FancyCounter(handler.ContentHandler): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
6 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
7 def __init__(self): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
8 self._elems = 0 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
9 self._attrs = 0 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
10 self._elem_types = {} |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
11 self._attr_types = {} |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
12 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
13 def startElement(self, name, attrs): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
14 self._elems = self._elems + 1 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
15 self._attrs = self._attrs + len(attrs) |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
16 self._elem_types[name] = self._elem_types.get(name, 0) + 1 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
17 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
18 for name in attrs.keys(): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
19 self._attr_types[name] = self._attr_types.get(name, 0) + 1 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
20 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
21 def endDocument(self): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
22 print "There were", self._elems, "elements." |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
23 print "There were", self._attrs, "attributes." |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
24 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
25 print "---ELEMENT TYPES" |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
26 for pair in self._elem_types.items(): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
27 print "%20s %d" % pair |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
28 |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
29 print "---ATTRIBUTE TYPES" |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
30 for pair in self._attr_types.items(): |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
31 print "%20s %d" % pair |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
32 |
|
32736
c3a096bfbd6b
Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents:
15241
diff
changeset
|
33 |
|
15241
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
34 parser = make_parser() |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
35 parser.setContentHandler(FancyCounter()) |
|
657750c1d783
Demos of the new XML support from Lars Marius Garshol <larsga@garshol.priv.no>.
Fred Drake <fdrake@acm.org>
parents:
diff
changeset
|
36 parser.parse(sys.argv[1]) |
