Skip to content

Commit

Permalink
Finish simple example for new data-type and add test.
Browse files Browse the repository at this point in the history
  • Loading branch information
teoliphant committed Mar 4, 2008
1 parent 91ee5fe commit 0dba087
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
17 changes: 17 additions & 0 deletions numpy/doc/newdtype_example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import floatint.floatint as ff
import numpy as np

# Setting using array is hard because
# The parser doesn't stop at tuples always
# So, the setitem code will be called with scalars on the
# wrong shaped array.
# But we can get a view as an ndarray of the given type:
g = np.array([1,2,3,4,5,6,7,8]).view(ff.floatint_type)

# Now, the elements will be the scalar type associated
# with the ndarray.
print g[0]
print type(g[1])

# Now, you need to register ufuncs and more arrfuncs to do useful things...

18 changes: 14 additions & 4 deletions numpy/doc/newdtype_example/floatint.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static PyArray_ArrFuncs _PyFloatInt_Funcs;

#define _ALIGN(type) offsetof(struct {char c; type v;},v)

/* Need to inherit from scalar type... */
/* The scalar-type */

static PyArray_Descr _PyFloatInt_Dtype = {
PyObject_HEAD_INIT(NULL)
Expand Down Expand Up @@ -98,8 +98,8 @@ static PyArray_Descr * _register_dtype(void)
{
int userval;
PyArray_InitArrFuncs(&_PyFloatInt_Funcs);
/* Add copyswap, copyswapn,
nonzero, getitem, setitem, cast */
/* Add copyswap,
nonzero, getitem, setitem*/
_PyFloatInt_Funcs.copyswap = twoint_copyswap;
_PyFloatInt_Funcs.getitem = (PyArray_GetItemFunc *)twoint_getitem;
_PyFloatInt_Funcs.setitem = (PyArray_SetItemFunc *)twoint_setitem;
Expand All @@ -113,7 +113,7 @@ static PyArray_Descr * _register_dtype(void)
/* Initialization function for the module (*must* be called init<name>) */

PyMODINIT_FUNC initfloatint(void) {
PyObject *m, *d, *s;
PyObject *m, *d;
PyArray_Descr *dtype;

/* Create the module and add the functions */
Expand All @@ -130,7 +130,17 @@ PyMODINIT_FUNC initfloatint(void) {

if (PyType_Ready(&PyFloat_Type) < 0) return;
PyFloatInt_Type.tp_base = &PyFloat_Type;
/* This is only needed because we are sub-typing the
Float type and must pre-set some function pointers
to get PyType_Ready to fill in the rest.
*/
PyFloatInt_Type.tp_alloc = PyType_GenericAlloc;
PyFloatInt_Type.tp_new = PyFloat_Type.tp_new;
PyFloatInt_Type.tp_dealloc = PyFloat_Type.tp_dealloc;
PyFloatInt_Type.tp_free = PyObject_Del;
if (PyType_Ready(&PyFloatInt_Type) < 0) return;
/* End specific code */


dtype = _register_dtype();
Py_XINCREF(dtype);
Expand Down

0 comments on commit 0dba087

Please sign in to comment.