Show More
@@ -54,14 +54,38 b' class CythonMagics(Magics):' | |||||
54 |
|
54 | |||
55 | @cell_magic |
|
55 | @cell_magic | |
56 | def cython_inline(self, line, cell): |
|
56 | def cython_inline(self, line, cell): | |
57 |
"""Compile and run a Cython code cell using Cython.inline. |
|
57 | """Compile and run a Cython code cell using Cython.inline. | |
|
58 | ||||
|
59 | This magic simply passes the body of the cell to Cython.inline | |||
|
60 | and returns the result. If the variables `a` and `b` are defined | |||
|
61 | in the user's namespace, here is a simple example that returns | |||
|
62 | their sum:: | |||
|
63 | ||||
|
64 | %%cython_inline | |||
|
65 | return a+b | |||
|
66 | ||||
|
67 | For most purposes, we recommend the usage of the `%%cython` magic. | |||
|
68 | """ | |||
58 | locs = self.shell.user_global_ns |
|
69 | locs = self.shell.user_global_ns | |
59 | globs = self.shell.user_ns |
|
70 | globs = self.shell.user_ns | |
60 | return Cython.inline(cell, locals=locs, globals=globs) |
|
71 | return Cython.inline(cell, locals=locs, globals=globs) | |
61 |
|
72 | |||
62 | @cell_magic |
|
73 | @cell_magic | |
63 | def cython_pyximport(self, line, cell): |
|
74 | def cython_pyximport(self, line, cell): | |
64 |
"""Compile and import a Cython code cell using pyximport. |
|
75 | """Compile and import a Cython code cell using pyximport. | |
|
76 | ||||
|
77 | The contents of the cell are written to a `.pyx` file in the current | |||
|
78 | working directory, which is then imported using `pyximport`. This | |||
|
79 | magic requires a module name to be passed:: | |||
|
80 | ||||
|
81 | %%cython_pyximport modulename | |||
|
82 | def f(x): | |||
|
83 | return 2.0*x | |||
|
84 | ||||
|
85 | The compiled module is then imported and all of its symbols are injected into | |||
|
86 | the user's namespace. For most purposes, we recommend the usage of the | |||
|
87 | `%%cython` magic. | |||
|
88 | """ | |||
65 | module_name = line.strip() |
|
89 | module_name = line.strip() | |
66 | if not module_name: |
|
90 | if not module_name: | |
67 | raise ValueError('module name must be given') |
|
91 | raise ValueError('module name must be given') | |
@@ -86,7 +110,19 b' class CythonMagics(Magics):' | |||||
86 | ) |
|
110 | ) | |
87 | @cell_magic |
|
111 | @cell_magic | |
88 | def cython(self, line, cell): |
|
112 | def cython(self, line, cell): | |
89 |
"""Compile and import everything from a Cython code cell. |
|
113 | """Compile and import everything from a Cython code cell. | |
|
114 | ||||
|
115 | The contents of the cell are written to a `.pyx` file in the | |||
|
116 | `~/.cython/magic` using a filename with the hash of the code. | |||
|
117 | This file is then cythonized and compiled. The resulting module | |||
|
118 | is imported and all of its symbols are injected into the user's | |||
|
119 | namespace. The usage is similar to that of `%%cython_pyximport` but | |||
|
120 | you don't have to pass a module name:: | |||
|
121 | ||||
|
122 | %%cython | |||
|
123 | def f(x): | |||
|
124 | return 2.0*x | |||
|
125 | """ | |||
90 | args = parse_argstring(self.cython, line) |
|
126 | args = parse_argstring(self.cython, line) | |
91 | code = cell if cell.endswith('\n') else cell+'\n' |
|
127 | code = cell if cell.endswith('\n') else cell+'\n' | |
92 | lib_dir=os.path.expanduser('~/.cython/magic') |
|
128 | lib_dir=os.path.expanduser('~/.cython/magic') |
General Comments 0
You need to be logged in to leave comments.
Login now