XML parser that sorts elements?
Hi everyone, I am a total newbie to XML parsing. I've written a couple of toy examples under the instruction of tutorials available on the web. The problem I want to solve is this. I have an XML snippet (in a string) that looks like this: hello goodbye and I want to alphabetize not only the attributes of an element, but I also want to alphabetize the elements in the same scope: goodbye hello I've found a "Canonizer" class, that subclasses saxlib.HandlerBase, and played around with it and vaguely understand what it's doing. But what I get out of it is hello goodbye in other words it sorts the attributes of each element, but doesn't touch the order of the elements. How can I sort the elements? I think I want to subclass the parser, to present the elements to the content handler in different order, but I couldn't immediately find any examples of the parser being subclassed. Thanks for any pointers! --JMike -- http://mail.python.org/mailman/listinfo/python-list
Re: XML parser that sorts elements?
Diez B. Roggisch wrote: > You can sort them by obtaining them as tree of nodes, e.g. using element > tree or minidom. > > But you should be aware that this will change the structure of your document > and it isn't always desirable to do so - e.g. html pages would look funny > to say the least if sorted in that way. > > Diez In this particular case, I need to sort the elements, and the specific application I'm testing guarantees that the order of the elements "in the same scope" (this may not be the right term in XML semantics, but it's what I know how to say) does not matter. That probably means that the specific application I'm testing is not using XML in a standard way, but so be it. I'm looking at minidom now and I think maybe there's enough documentation there that I can get a handle on it and do what I need to do. Thanks. (But if anyone else has a specific example I can crib from, that'd be great.) --JMike -- http://mail.python.org/mailman/listinfo/python-list
Re: XML parser that sorts elements?
Paul McGuire wrote: ... > Here is a snippet from an interactive Python session, working with the > "batteries included" xml.dom.minidom. The solution is not necessarily in > the parser, it may be instead in what you do with the parsed document > object. > > This is not a solution to your actual problem, but I hope it gives you > enough to work with to find your own solution. > > HTH, > -- Paul Whoa. Outstanding. Excellent. Thank you! --JMike -- http://mail.python.org/mailman/listinfo/python-list
Re: XML parser that sorts elements?
Paul McGuire wrote: > >>> doc.childNodes[0].childNodes = sorted( > ... [n for n in doc.childNodes[0].childNodes > ... if n.nodeType==doc.ELEMENT_NODE], > ... key=lambda n:n.nodeName) > >>> print doc.toprettyxml() > > > > goodbye > > > hello > > My requirements changed a bit, so now I'm sorting second level elements by their values of a specific attribute (where the specific attribute can be chosen). But the solution is still mainly what you posted here. It was just a matter of supplying a different function for 'key'. It's up and running live now and all is well. Thanks again! (A bonus side effect of this is that it let me sneak "sorted()" into our test infrastructure, which gave me reason to get our IT guys to upgrade a mismash of surprisingly old Python versions up to Python 2.5 everywhere.) --JMike -- http://mail.python.org/mailman/listinfo/python-list
Command line arguments on Vista
So I write this sript called printargs.py: -- #!/usr/local/bin/python import sys print 'there are %d args' % len(sys.argv) for arg in sys.argv: print 'arg: %s' % arg -- and make it executable. On pretty much every platform I can get my hands on, when I run printargs.py booga -a wooga I get this output: there are 4 args arg: printargs.py arg: booga arg: -a arg: wooga But on Windows Vista, when I run that command, I get there are 1 args arg: printargs.py What's up with that? --JMike -- http://mail.python.org/mailman/listinfo/python-list
Re: Command line arguments on Vista
By the way, note that if I say (on Vista) python printargs.py booga -a wooga I get the desired output: > there are 4 args > arg: printargs.py > arg: booga > arg: -a > arg: wooga So the quesiton still stands, what's up with that? Thanks, --JMike -- http://mail.python.org/mailman/listinfo/python-list
Re: Command line arguments on Vista
Some further information: perl seems to do the same thing (losing arguments). We think it may have something to do with file association. Any ideas anyone? --JMike -- http://mail.python.org/mailman/listinfo/python-list
Re: Command line arguments on Vista
Thanks for the answers; that was the problem exactly. --JMike Duncan Booth wrote: > It sounds like the registry entry for running Python files is messed up. > Can you go to a command line and see what the command 'ftype Python.File' > displays? (Assuming that command lines and ftype still work on Vista) > > The output should be: > Python.File="C:\Python25\python.exe" "%1" %* > > but if it only says: > Python.File="C:\Python25\python.exe" "%1" > > then you would get the behaviour you observed (on any version of Windows, > not just Vista). -- http://mail.python.org/mailman/listinfo/python-list
User-defined exception: "global name 'TestRunError' is not defined"
I'm using some legacy code that has a user-defined exception in it. The top level program includes this line from TestRunError import * It also imports several other modules. These other modules do not explicitly import TestRunError. TestRunError is raised in various places throughout the modules. There are a few cases where something goes wrong with the program and I get this error: FATAL ERROR: global name 'TestRunError' is not defined I realize this is kind of a silly question to ask in the general sense without showing more of the code, but does anyone have any suggestions as to the most likely causes of this error coming up? Could it be something like an error happening where it is not explicitly in a try block, or an error happening while I'm already in an except block, or something like that? Thanks, --JMike -- http://mail.python.org/mailman/listinfo/python-list