Skip to content

Add module's name argument in %%cython magic#4131

Merged
minrk merged 5 commits into
ipython:masterfrom
rossant:cython-magic-name-argument
Sep 25, 2013
Merged

Add module's name argument in %%cython magic#4131
minrk merged 5 commits into
ipython:masterfrom
rossant:cython-magic-name-argument

Conversation

@rossant
Copy link
Copy Markdown
Contributor

@rossant rossant commented Aug 29, 2013

This new option lets the user specify an explicit module name for its Cython cell rather than letting IPython generating a random name automatically. With this option, one can reuse a function defined in one %%cython cell magic in another %%cython cell magic, by doing an import of the module corresponding to the first cell magic.

@dhirschfeld
Copy link
Copy Markdown

I'm not sure this will play nice with the force argument - having previously compiled and imported the module I think you'd get a link error because the old version couldn't be deleted. I'm not sure this is avoidable so it may just be a case of documenting it in the help.

If you wanted to cimport a cdef function in the another %%cython block doesn't it have to be defined in a corresponding pxd so isn't this just limited to importing the slow python (def/cpdef) defined functions?

@rossant
Copy link
Copy Markdown
Contributor Author

rossant commented Aug 29, 2013

It seems to work with cdef too, unless I'm missing something:

%load_ext cythonmagic

%%cython -n mod1
cdef double f(double x):
    return x*x

%%cython
from mod1 import f
def g(x):
    return f(x)

g(3)
9

@rossant
Copy link
Copy Markdown
Contributor Author

rossant commented Aug 29, 2013

Forget it, I hadn't cleared the cache, it actually doesn't work with cdef!

And you're probably right about "force", I need to think about it...

@rossant
Copy link
Copy Markdown
Contributor Author

rossant commented Aug 29, 2013

Now I'm actually wondering if this is the best way to do it. Basically my point was that when you define a function in a cell (with %%cython or not), it is available in the namespace. But if it's a Cython function, it is not available in another %%cython cell, which may be slightly confusing for a beginner. Besides, it makes this %%cython less useful in my opinion.

Using explicit names might work, but it would force one to give names to every cell and to import these small modules manually in the other %%cython cells. It would be great if it could work automatically, i.e. a function defined in one %%cython cell is directly available in another %%cython cell. It might be a bit tricky to implement. Thoughts?

@ellisonbg
Copy link
Copy Markdown
Member

@rossant what are your plans for this PR? If you plan on continuing, can you create a todo to guide reviewers.

@rossant
Copy link
Copy Markdown
Contributor Author

rossant commented Sep 20, 2013

I think this particular PR is done. As discussed above, it does not completely solve the more general problem I had in mind, which is to simplify reuse of different Cython magics within a notebook. I might open a new issue/PR about this later.

Comment thread IPython/extensions/cythonmagic.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this have multiple names? Why have you made this a list?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple mistake -- fixed.

Comment thread IPython/extensions/cythonmagic.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this just be module_name = args.name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove str I get

D:\Git\ipython\IPython\extensions\cythonmagic.py in cython(self, line, cell)
    240                 extra_link_args = args.link_args,
    241                 libraries = args.lib,
--> 242                 language = 'c++' if args.cplus else 'c',
    243             )
    244             build_extension = self._get_build_extension()

C:\WinPython-64bit-2.7.5.1\python-2.7.5.amd64\lib\distutils\extension.pyc in __init__(self, name, sources, include_dirs, define_macros, undef_macros, library_dirs, libraries, runtime_library_dirs, extra_objects, extra_compile_args, extra_link_args, export_symbols, swig_opts, depends, language, **kw)
    104                   **kw                      # To catch unknown keywords
    105                  ):
--> 106         assert type(name) is StringType, "'name' must be a string"
    107         assert (type(sources) is ListType and
    108                 map(type, sources) == [StringType]*len(sources)), \

AssertionError: 'name' must be a string

on Python 2.7.5, Windows 8 64 bits.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then let's use py3compat.unicode_to_str

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done

minrk added a commit that referenced this pull request Sep 25, 2013
Add module's name argument in %%cython magic

This new option lets the user specify an explicit module name for its Cython cell rather than letting IPython generating a random name automatically. With this option, one can reuse a function defined in one %%cython cell magic in another %%cython cell magic, by doing an import of the module corresponding to the first cell magic.
@minrk minrk merged commit 929d291 into ipython:master Sep 25, 2013
mattvonrocketstein pushed a commit to mattvonrocketstein/ipython that referenced this pull request Nov 3, 2014
Add module's name argument in %%cython magic

This new option lets the user specify an explicit module name for its Cython cell rather than letting IPython generating a random name automatically. With this option, one can reuse a function defined in one %%cython cell magic in another %%cython cell magic, by doing an import of the module corresponding to the first cell magic.
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

Successfully merging this pull request may close these issues.

4 participants