stchris / untangle

Converts XML to Python objects
MIT License
612 stars 83 forks source link

can't dir() the parsed object #5

Closed iffy closed 11 years ago

iffy commented 12 years ago

I'm not sure if this is a SAX problem. If it is, just close it. But here's the problem:

>>> filename = 'posts-july-2012.xml'
>>> from untangle import parse
>>> o = parse(filename)
>>> dir(o)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/untangle.py", line 66, in __getattr__
    raise IndexError('Unknown key <%s>' % key)
IndexError: Unknown key <__dir__>
stchris commented 12 years ago

Thanks for reporting this. Can you please post some XML which causes this?

bsilverthorn commented 11 years ago

I'm not the original reporter, but do see the same issue with the following XML (TwiML):

<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Gather action="http://example.com/calls/1/twiml?event=start" numDigits="1" timeout="0">
    <Play>http://example.com/barcall_message_url.wav</Play>
  </Gather>
  <Redirect>http://example.com/calls/1/twiml?event=start</Redirect>
</Response>
iffy commented 11 years ago

Thank @bsilverthorn, I haven't gone back to that project for a few months.

mipedja commented 11 years ago

Untangle does not have a __dir__. If the expected behavior is to list children names, then this should do it:

def __dir__(self):
    children_names = [x._name for x in self.children]
    return children_names

If @stchris agrees with this behavior, I can submit a pull request for it.

bsilverthorn commented 11 years ago

A sensible default here would be great. As is, IPython throws a stack trace at you whenever you try to tab-complete, which (1) has become ingrained behavior, (2) would be nice for exploring an untangle object while debugging, and (3) seems surprising for new users (e.g., me).

stchris commented 11 years ago

Sorry to jump in so late in this discussion. Of course a pull request for the dir function is welcome. In the meantime I'll try to come up with something useful myself and add some test cases. I'll also test it under IPython/bpython.

stchris commented 11 years ago

I implemented the suggestion from @mipedja, because it seemed sensible to list the list of children. Also tested this with IPython, it seems to work as expected with tab completion.