Popular recipes by Orri Ganel http://code.activestate.com/recipes/users/2259404/2008-08-01T19:24:34-07:00ActiveState Code RecipesArbitrary order attribute writing with ElementTree (Python)
2008-08-01T19:24:34-07:00Orri Ganelhttp://code.activestate.com/recipes/users/2259404/http://code.activestate.com/recipes/576403-arbitrary-order-attribute-writing-with-elementtree/
<p style="color: grey">
Python
recipe 576403
by <a href="/recipes/users/2259404/">Orri Ganel</a>
(<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/element/">element</a>, <a href="/recipes/tags/elementtree/">elementtree</a>, <a href="/recipes/tags/element_tree/">element_tree</a>, <a href="/recipes/tags/etree/">etree</a>, <a href="/recipes/tags/order/">order</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/write/">write</a>, <a href="/recipes/tags/writing/">writing</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 5.
</p>
<p>Modified version of ElementTree with two additional parameters to the write() method: "sortflag" and "sortcmp". "sortflag" defaults to "default", which results in unmodified behavior. "sortcmp" defaults to None, which results in unmodified behavior. See discussion for usage and justification. Changes made begin on line 655.</p>
<p>EDIT: in most cases, unless sortflag happened to be intended for the root, it would be ignored; added sortflag and sortcmp to self._write() call on line 724. Expect another revision in the near future to allow for specifying different orders for different xml tags.</p>
<p>EDIT, the second: Added tag-specific ordering.</p>
flatten(...) (Python)
2005-01-14T05:56:26-08:00Orri Ganelhttp://code.activestate.com/recipes/users/2259404/http://code.activestate.com/recipes/363051-flatten/
<p style="color: grey">
Python
recipe 363051
by <a href="/recipes/users/2259404/">Orri Ganel</a>
.
Revision 2.
</p>
<p>Function for flattening sequences (currently works on tuples and lists, potentially works on user-defined sequences as well, since it only explicitly disallows strings and dictionaries as sequences). [Edit]: added a "Thinking . . . " progress measurement of sorts, and a (probably inaccurate) timer.</p>