Created
August 14, 2012 00:31
-
-
Save bfroehle/3345121 to your computer and use it in GitHub Desktop.
Cython Magic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "Cython Magic" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%load_ext cythonmagic", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%%cython --annotate\nfrom libc.stdlib cimport strtol as _strtol\n\ndef strtol(bytes n, int base=10):\n cdef int ret\n cdef char *nptr = n\n cdef char *endptr\n ret = _strtol(nptr, &endptr, base)\n if endptr == nptr or endptr[0] != 0:\n raise ValueError(\"Invalid string %r at at character %d\" % (n, endptr-nptr+1))\n return ret", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"html": "<!-- Generated by Cython 0.16 on Mon Aug 13 17:07:01 2012 -->\n<html>\n\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<style type=\"text/css\">\n\nbody { font-family: courier; font-size: 12; }\n\n.code { font-size: 9; color: #444444; display: none; margin-left: 20px; }\n.py_c_api { color: red; }\n.py_macro_api { color: #FF7000; }\n.pyx_c_api { color: #FF3000; }\n.pyx_macro_api { color: #FF7000; }\n.refnanny { color: #FFA000; }\n\n.error_goto { color: #FFA000; }\n\n.tag { }\n\n.coerce { color: #008000; border: 1px dotted #008000 }\n\n.py_attr { color: #FF0000; font-weight: bold; }\n.c_attr { color: #0000FF; }\n\n.py_call { color: #FF0000; font-weight: bold; }\n.c_call { color: #0000FF; }\n\n.line { margin: 0em }\n\n</style>\n<script>\nfunction toggleDiv(id) {\n theDiv = document.getElementById(id);\n if (theDiv.style.display == 'none') theDiv.style.display = 'block';\n else theDiv.style.display = 'none';\n}\n</script>\n</head>\n <body>\n<p>Generated by Cython 0.16 on Mon Aug 13 17:07:01 2012\n<pre class='line' style='background-color: #FFFFff' onclick='toggleDiv(\"line1\")'> 1: from libc.stdlib cimport strtol as _strtol</pre>\n<pre id='line1' class='code' style='background-color: #FFFFff'></pre><pre class='line' style='background-color: #FFFFff' onclick='toggleDiv(\"line2\")'> 2: </pre>\n<pre id='line2' class='code' style='background-color: #FFFFff'></pre><pre class='line' style='background-color: #FFFF52' onclick='toggleDiv(\"line3\")'> 3: def strtol(bytes n, int base=10):</pre>\n<pre id='line3' class='code' style='background-color: #FFFF52'>\n/* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":3\n * from libc.stdlib cimport strtol as _strtol\n * \n * def strtol(bytes n, int base=10): # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * cdef int ret\n * cdef char *nptr = n\n */\n\nstatic PyObject *__pyx_pf_46_cython_magic_61312305a41c94b0224908425e795b16_strtol(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_n, int __pyx_v_base) {\n int __pyx_v_ret;\n char *__pyx_v_nptr;\n char *__pyx_v_endptr;\n PyObject *__pyx_r = NULL;\n <span class='refnanny'>__Pyx_RefNannyDeclarations</span>\n <span class='pyx_c_api'><span class='refnanny'>__Pyx_RefNannySetupContext</span></span>(\"strtol\", 0);\n\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":3\n * from libc.stdlib cimport strtol as _strtol\n * \n * def strtol(bytes n, int base=10): # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * cdef int ret\n * cdef char *nptr = n\n */\n __pyx_k_tuple_2 = <span class='py_c_api'>PyTuple_New</span>(5);<span class='error_goto'> if (unlikely(!__pyx_k_tuple_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_k_tuple_2);\n <span class='pyx_macro_api'>__Pyx_INCREF</span>(((PyObject *)__pyx_n_s__n));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_k_tuple_2, 0, ((PyObject *)__pyx_n_s__n));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_n_s__n));\n <span class='pyx_macro_api'>__Pyx_INCREF</span>(((PyObject *)__pyx_n_s__base));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_k_tuple_2, 1, ((PyObject *)__pyx_n_s__base));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_n_s__base));\n <span class='pyx_macro_api'>__Pyx_INCREF</span>(((PyObject *)__pyx_n_s__ret));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_k_tuple_2, 2, ((PyObject *)__pyx_n_s__ret));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_n_s__ret));\n <span class='pyx_macro_api'>__Pyx_INCREF</span>(((PyObject *)__pyx_n_s__nptr));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_k_tuple_2, 3, ((PyObject *)__pyx_n_s__nptr));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_n_s__nptr));\n <span class='pyx_macro_api'>__Pyx_INCREF</span>(((PyObject *)__pyx_n_s__endptr));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_k_tuple_2, 4, ((PyObject *)__pyx_n_s__endptr));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_n_s__endptr));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_k_tuple_2));\n\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":3\n * from libc.stdlib cimport strtol as _strtol\n * \n * def strtol(bytes n, int base=10): # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * cdef int ret\n * cdef char *nptr = n\n */\n __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_61312305a41c94b0224908425e795b16_1strtol, NULL, __pyx_n_s_5);<span class='error_goto'> if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_t_1);\n if (<span class='py_c_api'>PyObject_SetAttr</span>(__pyx_m, __pyx_n_s__strtol, __pyx_t_1) <code><</code> 0)<span class='error_goto'> {__pyx_filename = __pyx_f[0]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_1); __pyx_t_1 = 0;\n</pre><pre class='line' style='background-color: #FFFFff' onclick='toggleDiv(\"line4\")'> 4: cdef int ret</pre>\n<pre id='line4' class='code' style='background-color: #FFFFff'></pre><pre class='line' style='background-color: #FFFF7f' onclick='toggleDiv(\"line5\")'> 5: cdef char *nptr = n</pre>\n<pre id='line5' class='code' style='background-color: #FFFF7f'>\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":5\n * def strtol(bytes n, int base=10):\n * cdef int ret\n * cdef char *nptr = n # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * cdef char *endptr\n * ret = _strtol(nptr, &endptr, base)\n */\n __pyx_t_1 = <span class='py_c_api'>PyBytes_AsString</span>(((PyObject *)__pyx_v_n));<span class='error_goto'> if (unlikely((!__pyx_t_1) && <span class='py_c_api'>PyErr_Occurred</span>())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 5; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n __pyx_v_nptr = __pyx_t_1;\n</pre><pre class='line' style='background-color: #FFFFff' onclick='toggleDiv(\"line6\")'> 6: cdef char *endptr</pre>\n<pre id='line6' class='code' style='background-color: #FFFFff'></pre><pre class='line' style='background-color: #FFFFff' onclick='toggleDiv(\"line7\")'> 7: ret = _strtol(nptr, &endptr, base)</pre>\n<pre id='line7' class='code' style='background-color: #FFFFff'>\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":7\n * cdef char *nptr = n\n * cdef char *endptr\n * ret = _strtol(nptr, &endptr, base) # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * if endptr == nptr or endptr[0] != 0:\n * raise ValueError(\"Invalid string %r at at character %d\" % (n, endptr-nptr+1))\n */\n __pyx_v_ret = strtol(__pyx_v_nptr, (&__pyx_v_endptr), __pyx_v_base);\n</pre><pre class='line' style='background-color: #FFFFff' onclick='toggleDiv(\"line8\")'> 8: if endptr == nptr or endptr[0] != 0:</pre>\n<pre id='line8' class='code' style='background-color: #FFFFff'>\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":8\n * cdef char *endptr\n * ret = _strtol(nptr, &endptr, base)\n * if endptr == nptr or endptr[0] != 0: # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * raise ValueError(\"Invalid string %r at at character %d\" % (n, endptr-nptr+1))\n * return ret\n */\n __pyx_t_2 = (__pyx_v_endptr == __pyx_v_nptr);\n if (!__pyx_t_2) {\n __pyx_t_3 = ((__pyx_v_endptr[0]) != 0);\n __pyx_t_4 = __pyx_t_3;\n } else {\n __pyx_t_4 = __pyx_t_2;\n }\n if (__pyx_t_4) {\n</pre><pre class='line' style='background-color: #FFFF39' onclick='toggleDiv(\"line9\")'> 9: raise ValueError(\"Invalid string %r at at character %d\" % (n, endptr-nptr+1))</pre>\n<pre id='line9' class='code' style='background-color: #FFFF39'>\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":9\n * ret = _strtol(nptr, &endptr, base)\n * if endptr == nptr or endptr[0] != 0:\n * raise ValueError(\"Invalid string %r at at character %d\" % (n, endptr-nptr+1)) # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n * return ret\n */\n __pyx_t_5 = <span class='py_c_api'>PyInt_FromLong</span>(((__pyx_v_endptr - __pyx_v_nptr) + 1));<span class='error_goto'> if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_t_5);\n __pyx_t_6 = <span class='py_c_api'>PyTuple_New</span>(2);<span class='error_goto'> if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_t_6);\n <span class='pyx_macro_api'>__Pyx_INCREF</span>(((PyObject *)__pyx_v_n));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_t_6, 0, ((PyObject *)__pyx_v_n));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_v_n));\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_t_6, 1, __pyx_t_5);\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(__pyx_t_5);\n __pyx_t_5 = 0;\n __pyx_t_5 = <span class='py_c_api'>PyNumber_Remainder</span>(((PyObject *)__pyx_kp_s_1), ((PyObject *)__pyx_t_6));<span class='error_goto'> if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(((PyObject *)__pyx_t_5));\n <span class='pyx_macro_api'>__Pyx_DECREF</span>(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;\n __pyx_t_6 = <span class='py_c_api'>PyTuple_New</span>(1);<span class='error_goto'> if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_t_6);\n <span class='py_macro_api'>PyTuple_SET_ITEM</span>(__pyx_t_6, 0, ((PyObject *)__pyx_t_5));\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GIVEREF</span></span>(((PyObject *)__pyx_t_5));\n __pyx_t_5 = 0;\n __pyx_t_5 = <span class='py_c_api'>PyObject_Call</span>(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_6), NULL);<span class='error_goto'> if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_t_5);\n <span class='pyx_macro_api'>__Pyx_DECREF</span>(((PyObject *)__pyx_t_6)); __pyx_t_6 = 0;\n <span class='pyx_c_api'>__Pyx_Raise</span>(__pyx_t_5, 0, 0, 0);\n <span class='pyx_macro_api'>__Pyx_DECREF</span>(__pyx_t_5); __pyx_t_5 = 0;\n <span class='error_goto'> {__pyx_filename = __pyx_f[0]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n goto __pyx_L3;\n }\n __pyx_L3:;\n</pre><pre class='line' style='background-color: #FFFF73' onclick='toggleDiv(\"line10\")'> 10: return ret</pre>\n<pre id='line10' class='code' style='background-color: #FFFF73'>\n /* \"_cython_magic_61312305a41c94b0224908425e795b16.pyx\":10\n * if endptr == nptr or endptr[0] != 0:\n * raise ValueError(\"Invalid string %r at at character %d\" % (n, endptr-nptr+1))\n * return ret # <code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code><code><</code>\n */\n <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_r);\n __pyx_t_5 = <span class='py_c_api'>PyInt_FromLong</span>(__pyx_v_ret);<span class='error_goto'> if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;}</span>\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_GOTREF</span></span>(__pyx_t_5);\n __pyx_r = __pyx_t_5;\n __pyx_t_5 = 0;\n goto __pyx_L0;\n\n __pyx_r = Py_None; <span class='pyx_macro_api'>__Pyx_INCREF</span>(Py_None);\n goto __pyx_L0;\n __pyx_L1_error:;\n <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_5);\n <span class='pyx_macro_api'>__Pyx_XDECREF</span>(__pyx_t_6);\n <span class='pyx_c_api'>__Pyx_AddTraceback</span>(\"_cython_magic_61312305a41c94b0224908425e795b16.strtol\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n __pyx_r = NULL;\n __pyx_L0:;\n <span class='pyx_macro_api'><span class='refnanny'>__Pyx_XGIVEREF</span></span>(__pyx_r);\n <span class='pyx_c_api'><span class='refnanny'>__Pyx_RefNannyFinishContext</span></span>();\n return __pyx_r;\n}\n</pre></body></html>", | |
"output_type": "pyout", | |
"prompt_number": 4, | |
"text": "<IPython.core.display.HTML at 0x3e89050>" | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "strtol('hello', base=36)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": "29234652" | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "%timeit strtol('hello', base=36)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "1000000 loops, best of 3: 285 ns per loop\n" | |
} | |
], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment