Add module's name argument in %%cython magic#4131
Conversation
|
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? |
|
It seems to work with cdef too, unless I'm missing something: |
|
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... |
|
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? |
|
@rossant what are your plans for this PR? If you plan on continuing, can you create a todo to guide reviewers. |
|
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. |
There was a problem hiding this comment.
can this have multiple names? Why have you made this a list?
There was a problem hiding this comment.
Simple mistake -- fixed.
Added test_cython_name.
There was a problem hiding this comment.
Shouldn't this just be module_name = args.name?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
then let's use py3compat.unicode_to_str
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.
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.
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.