EN World - Morrus’ D&D / d20 News & Reviews Site - Scenario Writers Needed
EN World - Morrus’ D&D / d20 News & Reviews Site - Scenario Writers Needed
Reminder to myself to start keeping track of our B5 campaign in writing. 30,000 words by March?
EN World - Morrus’ D&D / d20 News & Reviews Site - Scenario Writers Needed
Reminder to myself to start keeping track of our B5 campaign in writing. 30,000 words by March?
M. David Peterson was wondering why people are using Python for XML development. I left the following as a comment on his blog; here it is for the rest of you too lazy to read his comments section.
First point - there’s nothing special about processing XML/XSLT from Python versus another language. Most likely you’re still going to be using either DOM or SAX to process your data.
There are three special features of Python that make it attractive in general.
1) Lists, dictionaries, tuples, and sets are native data types. Once you realize how many lines of code you spend creating and looping through collections in other languages, you’ll really, really appreciate this feature. This is what brought me into the language.
Most Python XML libraries allow you to write things like:
attributeNames = [a.name for a in element.attributes()]
Actually, they’d let you write something more elegant and interesting, but the point is that identical code in, say, C# would be five or six lines. The result here is an instance of the standard list class.
2) It is a terse but readable language. This is very important. The line of code above is typical and should be easy to understand no matter what your background. This makes it easy to debug and maintain.
A lot of people use it for prototyping. I have a fairly complex GUI application I’ve written that is less than 1,000 lines of code. An earlier version in C# was over 8,000 lines and had less than half the functionality.
3) Classes are first-class citizens. Adding a custom property to an object dynamically in other languages is several lines of not-always-trivial reflection code. In Python, I can write:
attributeNames.originalElement = element
It’s the combination of these three points that make python interesting. Being able to create classes on the fly as XML is read in and write code that uses those classes with no special steps is, I think, one of the big attractions. Add in the fact that all that traversal of nodes can be done using simple, built-in collection types and that’s icing on the cake.
For the record, Dive Into Python is great resource for those new to the language, and it includes a chapter on XML.
Powered by WordPress