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

False AttributeError. #164

Closed
Ismael-VC opened this issue Jul 8, 2015 · 5 comments
Closed

False AttributeError. #164

Ismael-VC opened this issue Jul 8, 2015 · 5 comments

Comments

@Ismael-VC
Copy link

I was trying to answer this question at StackOverflow:

So I try to use PyCall:

And I get this error at Out [7]:

PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'dict' object has no attribute 'worksheets'",)
  File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/current.py", line 188, in write
    s = writes(nb, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/current.py", line 154, in writes
    return versions[version].writes_json(nb, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/v3/nbjson.py", line 48, in writes
    nb = split_lines(nb)
  File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/v3/rwbase.py", line 85, in split_lines
    for ws in nb.worksheets:

while loading In[7], in expression starting on line 3

 in pyerr_check at /home/juser/.julia/v0.3/PyCall/src/exception.jl:58
 in pycall at /home/juser/.julia/v0.3/PyCall/src/PyCall.jl:91
 in fn at /home/juser/.julia/v0.3/PyCall/src/conversions.jl:188
 in anonymous at In[7]:4
 in open at iostream.jl:137

But the dictionary in question indeed does have a worksheets key, as can be seen at Out [8].

@stevengj
Copy link
Member

stevengj commented Jul 8, 2015

The dict has an "worksheets" key, but not a worksheets attribute.

I think the problem here may be that the conversion to a Julia Dict is losing some Python info. Try using lower-level functions that give you more control over the type conversions:

nbf = pyimport("IPython.nbformat.current")
nb = pycall(nbf["new_notebook"], PyDict)

or even nb = pycall(nbf["new_notebook"], PyObject)

@Ismael-VC
Copy link
Author

@stevengj I did what you told me, but now in this part:

fname = "test.ipynb"

open(fname, "w") do f
    nbf[:write](nb, f, "ipynb")
end

I have this error:

PyError (:PyObject_Call) <type 'exceptions.TypeError'>
TypeError("'unicode' does not have the buffer interface",)
  File "/usr/local/lib/python2.7/dist-packages/IPython/nbformat/current.py", line 191, in write
    return fp.write(s)

while loading In[6], in expression starting on line 3

 in pyerr_check at /home/juser/.julia/v0.3/PyCall/src/exception.jl:58
 in pycall at /home/juser/.julia/v0.3/PyCall/src/PyCall.jl:91
 in fn at /home/juser/.julia/v0.3/PyCall/src/conversions.jl:188
 in anonymous at In[6]:4
 in open at iostream.jl:137

I don't know hot to convert an ASCIIString into a normal Python string (not unicode). I tried aliasing python with python3 and also this:

function PyCall.PyObject(s::ASCIIString)
    PyCall.@pyinitialize
    PyObject(PyCall.@pycheckn ccall(pystring_fromstring,
                                 PyPtr, (Ptr{Uint8},), bytestring(s)))
end

but then I get:

PyObject(ascii("test"))

s not defined
while loading In[15], in expression starting on line 1

 in PyObject at In[12]:88

I haven't been able to figure out why.

@yuyichao
Copy link
Collaborator

yuyichao commented Jul 9, 2015

@Ismael-VC I checked the python source and AFAICT the issue is not passing a bytestring to python, rather python is passing a string (unicode) to julia and julia don't know how to write it to the file object.

My working example here

Also note that the interface is deprecated (You can see the deprecation warning from a ipython notebook) and AFAICT it is encouraged to specify the version of the notebook format directly.

@yuyichao
Copy link
Collaborator

yuyichao commented Jul 9, 2015

Also, to convert a python str to bytes you can do what you would do in python, i.e. calling encode on it.

@stevengj
Copy link
Member

stevengj commented Jul 9, 2015

@stevengj stevengj closed this as completed Jul 9, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants