[IPython-user] Using yaml with ipython is goofy
Robert Kern
robert.kern@gmail....
Sat Feb 14 16:42:00 CST 2009
On 2009-02-14 16:19, Matthew Wilson wrote:
> I tried playing with yaml in an ipython session and got weird results.
> yaml.load returned a string!
>
> First, here's what happens in a not-ipython session:
>
> $ python
> Python 2.6b2+ (trunk:65208, Jul 23 2008, 17:02:49)
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import yaml
> >>> class Y:
> ... pass
> ...
> >>> yaml.load("""
> ... !!python/object:__main__.Y
> ... name: Yahoo
> ... """)
> <__main__.Y instance at 0x82ec5ec>
> >>> type(yaml.load("""
> ... !!python/object:__main__.Y
> ... name: Yahoo
> ... """))
> <type 'instance'>
>
>
> Now this is what happens in my beloved ipython:
>
> $ ipython
> /home/matt/virtualenvs/pitz-2.6/lib/python2.6/site-packages/ipython-0.9.1-py2.6.egg/IPython/Magic.py:38:
> DeprecationWarning: the sets module is deprecated
> from sets import Set
> Python 2.6b2+ (trunk:65208, Jul 23 2008, 17:02:49)
> Type "copyright", "credits" or "license" for more information.
>
> IPython 0.9.1 -- An enhanced Interactive Python.
> ? -> Introduction and overview of IPython's features.
> %quickref -> Quick reference.
> help -> Python's own help system.
> object? -> Details about 'object'. ?object also works, ?? prints more.
>
> In [1]: import yaml
>
> In [2]: class Y:
> ...: pass
> ...:
>
> In [3]: y = yaml.load("""
> ...: !!python/object:__main__.Y
> ...: name: Yahoo
> ...: """)
>
>
> And then I got a big traceback, ending with:
>
> ScannerError: mapping values are not allowed here
> in "<string>", line 3, column 5:
> name: Yahoo
> ^
>
> So, what is going on here? What am I doing wrong?
IPython's __main__ is kind of weird and suboptimal in a few respects. If you
import __main__, you can see that it doesn't have a Y in it. yaml is probably
trying to import __main__.Y and failing. For example, instead of __main__ try
another module that doesn't have a Y; you'll get the same error message. For the
suboptimal error message, you can blame yaml. :-)
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the IPython-user
mailing list