15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

SciPy Reference Guide, Release 0.8.dev<br />

>>> a = 1<br />

>>> inline("",[’a’])<br />

The argument conversion code inserted for a is:<br />

/* argument conversion code */<br />

int a = py_to_int (get_variable("a",raw_locals,raw_globals),"a");<br />

get_variable() reads the variable a from the local and global namespaces. py_to_int() has the following<br />

form:<br />

static int py_to_int(PyObject* py_obj,char* name)<br />

{<br />

if (!py_obj || !PyInt_Check(py_obj))<br />

handle_bad_type(py_obj,"int", name);<br />

return (int) PyInt_AsLong(py_obj);<br />

}<br />

Similarly, the float and complex conversion routines look like:<br />

static double py_to_float(PyObject* py_obj,char* name)<br />

{<br />

if (!py_obj || !PyFloat_Check(py_obj))<br />

handle_bad_type(py_obj,"float", name);<br />

return PyFloat_AsDouble(py_obj);<br />

}<br />

static std::complex py_to_complex(PyObject* py_obj,char* name)<br />

{<br />

if (!py_obj || !PyComplex_Check(py_obj))<br />

handle_bad_type(py_obj,"complex", name);<br />

return std::complex(PyComplex_RealAsDouble(py_obj),<br />

PyComplex_ImagAsDouble(py_obj));<br />

}<br />

NumPy conversions do not require any clean up code.<br />

String, List, Tuple, and Dictionary Conversion<br />

Strings, Lists, Tuples and Dictionary conversions are all converted to SCXX types by default. For the following code,<br />

>>> a = [1]<br />

>>> inline("",[’a’])<br />

The argument conversion code inserted for a is:<br />

/* argument conversion code */<br />

Py::List a = py_to_list(get_variable("a",raw_locals,raw_globals),"a");<br />

get_variable() reads the variable a from the local and global namespaces. py_to_list() and its friends has<br />

the following form:<br />

static Py::List py_to_list(PyObject* py_obj,char* name)<br />

{<br />

if (!py_obj || !PyList_Check(py_obj))<br />

handle_bad_type(py_obj,"list", name);<br />

106 Chapter 1. SciPy Tutorial

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!