[IPython-user] Using yaml with ipython is goofy
Ryan James
rgjames@ucdavis....
Sat Feb 14 20:28:28 CST 2009
On Sat, 2009-02-14 at 20:10 -0600, Robert Kern wrote:
> On 2009-02-14 16:42, Robert Kern wrote:
> > On 2009-02-14 16:19, Matthew Wilson wrote:
>
> >> 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. :-)
>
> Hmm, nevermind. From the regular Python prompt, the error message is different:
>
> >>> import yaml
> >>> yaml.load('''
> ... !!python/object:numpy.Y
> ... name: Yahoo
> ... ''')
> Traceback (most recent call last):
> File "<stdin>", line 4, in <module>
> File "/Users/rkern/bzr/runsnakerun/__init__.py", line 66, in load
>
> File "build/bdist.macosx-10.4-i386/egg/yaml/constructor.py", line 38, in get_data
> File "build/bdist.macosx-10.4-i386/egg/yaml/constructor.py", line 45, in
> construct_document
> File "build/bdist.macosx-10.4-i386/egg/yaml/constructor.py", line 96, in
> construct_object
> File "build/bdist.macosx-10.4-i386/egg/yaml/constructor.py", line 572, in
> construct_python_object
> File "build/bdist.macosx-10.4-i386/egg/yaml/constructor.py", line 544, in
> make_python_instance
> File "build/bdist.macosx-10.4-i386/egg/yaml/constructor.py", line 517, in
> find_python_name
> yaml.constructor.ConstructorError: while constructing a Python object
> cannot find 'Y' in the module 'numpy'
> in "<string>", line 2, column 1:
> !!python/object:numpy.Y
> ^
> >>>
>
> So I don't know what the problem is.
>
but if you add Y to the __main__ FakeModule, it works:
In [1]: class Y(object):
...: pass
...:
In [2]: import yaml
In [3]: import __main__
In [4]: __main__.Y = Y
In [5]: y = yaml.load("!!python/object:__main__.Y\nname: Yahoo")
In [6]: y
Out[6]: <__main__.Y object at 0x28c5a50>
In [7]: y.name
Out[7]: 'Yahoo'
is it possible to add a hook or something so that when things are
interactively added to user_ns they also get added to __main__, so that
everything that %who returns is also in __main__, just like in the
standard interpreter?
More information about the IPython-user
mailing list