# HG changeset patch # User paulb # Date 1116164917 0 # Node ID 7f0ba3749a7ff5bcfc5dd1dd9cd40cf109ac00cc # Parent 5b15d727aa91b2d81d407558b793e00a0ef91fcf [project @ 2005-05-15 13:48:37 by paulb] Added libxml2dom support for the "begat" test. diff -r 5b15d727aa91 -r 7f0ba3749a7f tests/begat.py --- a/tests/begat.py Sat May 14 23:32:41 2005 +0000 +++ b/tests/begat.py Sun May 15 13:48:37 2005 +0000 @@ -7,28 +7,70 @@ """ import libxml2macro as n_ -import time, os + +def test_begat_libxml2macro(n_doc, full_xpath): + l = [] -raw_input("Start your engines with ps -p %s -fv" % os.getpid()) -t = time.time() + if full_xpath: + for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"): + text = n_node.nodeValue + l.append(text) + else: + # NOTE: Code corresponding to this was suggested for cElementTree, but why not + # NOTE: take full advantage of XPath if you have most of the code written in C? -l = [] -n_doc = parseFile("ot.xml") -for n_node in n_doc.xpath("//v[contains(text(), 'begat')]"): - text = n_node.nodeValue - l.append(text) + for n_node in n_doc.xpath("//v"): + text = n_node.nodeValue + if text.find(u'begat') != -1: + l.append(text) + + return l + +def test_begat_libxml2dom(doc, full_xpath): + l = [] + + if full_xpath: + for node in doc.xpath("//v[contains(text(), 'begat')]"): + text = node.nodeValue + l.append(text) + else: + # NOTE: Code corresponding to this was suggested for cElementTree, but why not + # NOTE: take full advantage of XPath if you have most of the code written in C? -# NOTE: Code corresponding to this was suggested for cElementTree, but why not -# NOTE: take full advantage of XPath if you have most of the code written in C? -# -# for n_node in n_doc.xpath("//v"): -# text = n_node.nodeValue -# if text.find(u'begat') != -1: -# print text + for node in doc.xpath("//v"): + text = node.nodeValue + if text.find(u'begat') != -1: + l.append(text) + + return l + +if __name__ == "__main__": + import time, os + import sys + + ot_locations = [arg for arg in sys.argv if arg.endswith("ot.xml")] + full_xpath = "--full" in sys.argv + use_libxml2dom = "libxml2dom" in sys.argv + use_libxml2macro = "libxml2macro" in sys.argv -print "Time taken", time.time() - t -raw_input("Stop your engines!") + if len(ot_locations) == 0: + print "Please specify the location of the ot.xml file." + sys.exit(1) + + raw_input("Start your engines with ps -p %s -fv" % os.getpid()) + t = time.time() -print l + if use_libxml2macro: + n_doc = parseFile(ot_locations[0]) + l = test_begat_libxml2macro(n_doc, full_xpath) + else: # use_libxml2dom: + import libxml2dom + doc = libxml2dom.parse(ot_locations[0]) + l = test_begat_libxml2dom(doc, full_xpath) + + print "Time taken", time.time() - t + raw_input("Stop your engines!") + + print l # vim: tabstop=4 expandtab shiftwidth=4