Is this anything?

By Filip Salomonsson; published on November 24, 2006.

Update: No, this is not anything.

Just a dump for now. Using ElementTree and Python 2.5:

from xml.etree.ElementTree import iterparse
from xml.etree.ElementPath import _compile, xpath_descendant_or_self

def itertake(source, path):
    path = _compile(path).path
    tags = []
    for event, elem in iterparse(source, ("start", "end")):
        if event == "start":
            tags.append(elem.tag)
        else:
            if _match_path(tags, path):
                yield elem
            elem.clear()
            tags.pop()


def _match_path(tags, path):
    if tags == path: return True
    if not tags or not path: return False
    part = path[0]
    if isinstance(part, xpath_descendant_or_self):
        return any(_match_path(tags[j+1:], path[1:])
                   for j in range(len(tags)))
    else:
        if tags[0] == part or part == "*":
            return _match_path(tags[1:], path[1:])