Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide iterator to iterate over all nodes #10

Open
jonlooney opened this issue Apr 13, 2016 · 0 comments
Open

Provide iterator to iterate over all nodes #10

jonlooney opened this issue Apr 13, 2016 · 0 comments
Assignees
Milestone

Comments

@jonlooney
Copy link
Contributor

When we parse XML, we should provide a way to iterate over all nodes at a given level, in the order received.

Currently, you can iterate over all nodes at a given level this way:

for node in root['a'].values():
    for subnode in node.list():
        pass

But, this has two problems:

  1. It uses two levels of loops to do something common. (And, even if you could use list comprehension to combine this to a single loop, the list comprehension would be more complicated than it should be to do something this common.)
  2. This will lose the original input ordering.

Ideally, we should be able to do something like:

for node in root['a'].nodes():
    pass

Expected implementation:

  • Maintain an internal list of nodes over which to iterate.
  • When items are added/deleted in an XMLDictNode, update the node list.
    • Requires overriding the __setitem__, __delitem__, and clear methods
  • When items are added/deleted in an XMLListNode, update the parent's ordered list.
    • Requires overriding the __setitem__, __delitem__, __delslice__, and pop methods; however, this will probably actually require switching theXMLListNodeto be a subclass ofMutableSequence`.

Some quirks to be considered:

  • The in-place dict method shouldn't modify order.
  • Replacements should inherit the position of their parents.
  • What if there is no parent? Nested lists? Other strange things?
@jonlooney jonlooney self-assigned this Apr 13, 2016
@jonlooney jonlooney added this to the 1.1 milestone Apr 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant