File: testxml1.py

package info (click to toggle)
python-xml 0.8.4-10.1%2Blenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,972 kB
  • ctags: 10,628
  • sloc: python: 46,730; ansic: 14,354; xml: 968; makefile: 201; sh: 20
file content (197 lines) | stat: -rw-r--r-- 5,895 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# basic tests

import sys
import time, string
from xml.parsers import sgmlop

try:
    from xml.parsers import xmllib
    have_xmllib = 1
except ImportError:
    have_xmllib = 0
    class xmllib:
        class FastXMLParser:pass
        class SlowXMLParser:pass

try:
    FILE, VERBOSE = sys.argv[1], 2
except IndexError:
    FILE, VERBOSE = "hamlet.xml", 1

print
print "test collecting parsers on", FILE
print

# --------------------------------------------------------------------
# sgmlop

class myCollector:
    def __init__(self):
        self.data = []
        self.text = []
    def finish_starttag(self, tag, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append(("start", tag, data))
    def handle_proc(self, tag, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append(("pi", tag, data))
    def handle_special(self, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append(("special", data))
    def handle_entityref(self, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append(("entity", data))
    def handle_data(self, data):
        self.text.append(data)
    def handle_cdata(self, data):
        self.text.append("CDATA" + data)

t = time.clock()
for i in range(1000):
    out = myCollector()
    fp = open(FILE)
    parser = sgmlop.XMLUnicodeParser()
    parser.register(out)
    b = 0
    while 1:
        data = fp.read(1024)
        if not data:
            break
        parser.feed(data)
        b = b + len(data)
    parser.close()
t1 = time.clock() - t

print "raw sgmlop:", len(out.data), "items;", round(t1, 3), "seconds;",
print round(b / t1 / 1024, 2), "kbytes per second"
print out.data

# --------------------------------------------------------------------
# xmllib

class FastXMLParser(xmllib.FastXMLParser):
    def __init__(self):
        xmllib.FastXMLParser.__init__(self)
        self.data = []
        self.text = []
    def unknown_starttag(self, tag, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("start", tag, data)
    def handle_proc(self, tag, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("pi", tag, data)
    def handle_special(self, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("special", data)
    def handle_entityref(self, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("entity", data)
    def handle_data(self, data):
        self.text.append(data)
    def handle_cdata(self, data):
        self.text.append("CDATA" + data)

if have_xmllib:
    t = time.clock()
    for i in range(1):
        fp = open(FILE)
        parser2 = FastXMLParser()
        b = 0
        while 1:
            data = fp.read(1024)
            if not data:
                break
            parser2.feed(data)
            b = b + len(data)
        parser2.close()
    t2 = time.clock() - t

    print "fast xmllib:", len(parser2.data), "items;", round(t2, 3), "seconds;",
    print round(b / t2 / 1024, 2), "kbytes per second"

class SlowXMLParser(xmllib.SlowXMLParser):
    def __init__(self):
        xmllib.SlowXMLParser.__init__(self)
        self.data = []
        self.text = []
    def unknown_starttag(self, tag, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("start", tag, data)
    def handle_proc(self, tag, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("pi", tag, data)
    def handle_special(self, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("special", data)
    def handle_entityref(self, data):
        if self.text:
            self.data.append(repr(string.join(self.text, "")))
            self.text = []
        self.data.append("entity", data)
    def handle_data(self, data):
        self.text.append(data)
    def handle_cdata(self, data):
        self.text.append("CDATA" + data)

if have_xmllib:
    t = time.clock()
    for i in range(1):
        fp = open(FILE)
        parser3 = SlowXMLParser()
        b = 0
        while 1:
            data = fp.read(1024)
            if not data:
                break
            parser3.feed(data)
            b = b + len(data)
        parser3.close()
    t3 = time.clock() - t

    print "slow xmllib:", len(parser3.data), "items;", round(t3, 3), "seconds;",
    print round(b / t3 / 1024, 2), "kbytes per second"

    print
    print "normalized timing:"
    print "slow xmllib", 1.0
    print "fast xmllib", round(t2 / t3, 2), "(%sx)" % round(t3 / t2, 1)
    print "sgmlop     ", round(t1 / t3, 2), "(%sx)" % round(t3 / t1, 1)
    print

    print "looking for differences:"

    items = min(len(parser2.data), len(parser3.data))

    for i in xrange(items):
        if parser2.data[i] != parser3.data[i]:
            for j in range(max(i-5, 0), min(i+5, items)):
                if parser2.data[j] != parser3.data[j]:
                    print "+", j+1, parser2.data[j]
                    print "*", j+1, parser3.data[j]
                else:
                    print "=", j+1, parser2.data[j]
            break
    else:
        print "   (none found)"