![](https://secure.gravatar.com/avatar/b22439558ce8aa008389b409900babe5.jpg?s=120&d=mm&r=g)
I propose to add two function to multiarray module in Numeric: tofile(array, openedfile) array = fromfile(opendfile, dimension, typecode = "l") Now I use these functions in my module. Here is an implementation from module: ###############################################################################3 #include "Python.h" #define PY_ARRAY_UNIQUE_SYMBOL PyArray_arrayfile #include "Numeric/arrayobject.h" #define MAX_DIMS 40 static PyObject * PyArray_FromFile(PyObject *self, PyObject *args) { PyObject *f, *dimobj, *temp; PyArray_Descr *descr; PyArrayObject *ret = NULL; int i, n, dim[40], nt; char *typecode = "l"; FILE *fp; int type_num, N = 1; size_t nread; if (!PyArg_ParseTuple(args, "O!O!|s#:fromfile", \ &PyFile_Type, &f, &PyTuple_Type, &dimobj, &typecode, &nt)) return NULL; fp = PyFile_AsFile(f); if (fp == NULL) { PyErr_SetString(PyExc_TypeError, "1st argument must be open file"); return NULL; } n = PyTuple_Size(dimobj); if (n > MAX_DIMS) { PyErr_SetString(PyExc_TypeError, "dimension is too large"); return NULL; } if (n > 0) { for (i = 0; i < n; i++) { temp = PyTuple_GetItem(dimobj, i); dim[i] = (int)PyInt_AsLong(temp); } descr = PyArray_DescrFromType(*typecode); type_num = descr -> type_num; ret = (PyArrayObject *)PyArray_FromDims(n, dim, type_num); memcpy(ret->data, descr->zero, N*descr->elsize); N = 1; for (i = 0; i < n; i++) N *= dim[i]; nread = fread((char *)ret->data, descr->elsize, N, fp); if (nread < (size_t)N) { PyErr_SetString(PyExc_EOFError, "not enough items in file"); return NULL; } } return (PyObject *)ret; } static char PyArray_FromFile_doc [] = "fromfile(f, dimension, typecode = \'l\')\n\ \n\ Create array from open file f with given dimension and typecode.\n\ Note, that file must be open in binary mode."; static PyObject * PyArray_ToFile(PyObject *self, PyObject *args) { PyObject *f; PyArrayObject *A; int N; FILE *fp; if (!PyArg_ParseTuple(args, "O!O!:tofile", &PyArray_Type, &A, &PyFile_Type, &f)) return NULL; fp = PyFile_AsFile(f); if (fp == NULL) { PyErr_SetString(PyExc_TypeError, "arg must be open file"); return NULL; } N = PyArray_SIZE(A); if (N > 0) { if (fwrite(A->data, A->descr->elsize, N, fp) != (size_t)N) { PyErr_SetFromErrno(PyExc_IOError); clearerr(fp); return NULL; } } Py_INCREF(Py_None); return Py_None; } static char PyArray_ToFile_doc [] = "tofile(array, f)\n\ \n\ Write array to file open f.\n\ Note, that file must be open in binary mode."; static PyMethodDef arrayfile_module_methods[] = { {"fromfile", PyArray_FromFile, 1, PyArray_FromFile_doc}, {"tofile", PyArray_ToFile, 1, PyArray_ToFile_doc}, {NULL, NULL} /* sentinel */ }; ############################################################################ Zaur Shibzoukhov
participants (1)
-
Zaur Shibzoukhov