def traverse(element, query, deep=False):
<EOL>part = query[<NUM_LIT:0>]<EOL>if not part:<EOL><INDENT>query = query[<NUM_LIT:1>:]<EOL>part = query[<NUM_LIT:0>]<EOL>deep = True<EOL><DEDENT>part, predicate = xpath_re.match(query[<NUM_LIT:0>]).groups()<EOL>for c in element._children:<EOL><INDENT>if part in ('<STR_LIT:*>', c.tagname) and c._match(predicate):<EOL><INDENT>if len(query) == <NUM_LIT:1>:<EOL><INDENT>yield c<EOL><DEDENT>else:<EOL><INDENT>for e in traverse(c, query[<NUM_LIT:1>:]):<EOL><INDENT>yield e<EOL><DEDENT><DEDENT><DEDENT>if deep:<EOL><INDENT>for e in traverse(c, query, deep=True):<EOL><INDENT>yield e<EOL><DEDENT><DEDENT><DEDENT>
Helper function to traverse an element tree rooted at element, yielding nodes matching the query.