[issue40689] The only supported minidom attribute iteration (NamedNodeMap) is O(n^2)
Niels Thykier
report at bugs.python.org
Tue May 19 15:55:58 EDT 2020
New submission from Niels Thykier <niels at thykier.net>:
Hi,
The only official supported iteration over all attributes on an element via the minidom XML API require an O(n²) iterator. It happens because the `item` method generates a list of all attribute names, look up the attribute name at the given index and then throws away the list (only to recompute it on next lookup).
There are also a `.values()` method that looks very promising, only it has "strings-attached" in the form of the following disclaimer:
"""There are also experimental methods that give this class more mapping behavior. [...]"""
(source: https://docs.python.org/3/library/xml.dom.html)
The word "experimental" makes it hard for me to ask projects to migrate to `.values()` because I have to convince them to accept the risk of adopting the "unsupported APIs".
For me, any of the following would solve the issue:
* Bless the mapping based API as supported to the same extend as `item` + `length` in the DOM API.
* Optimize `item` to avoid the performance issue.
* Provide an alternative (but supported) way of iterating over all attributes. Preferably one that enables you to get the node directly without having to use `Element.getAttributeNode()` or similar.
The code in question highlighting the problematic code in the minidom API:
```
class NamedNodeMap(object):
[...]
def item(self, index):
try:
return self[list(self._attrs.keys())[index]]
except IndexError:
return None
```
----------
components: XML
messages: 369384
nosy: nthykier
priority: normal
severity: normal
status: open
title: The only supported minidom attribute iteration (NamedNodeMap) is O(n^2)
type: performance
versions: Python 3.8
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40689>
_______________________________________
More information about the Python-bugs-list
mailing list