array and range wrapper work again

This commit is contained in:
Christian Zimmermann 2024-02-08 01:01:47 +01:00
parent 564b3eebb5
commit a06bb976a0
6 changed files with 107 additions and 87 deletions

View file

@ -1,6 +1,7 @@
#include <Python.h> #include <Python.h>
#include "array_wrapper.h" #include "array_wrapper.h"
#include "range_wrapper.h"
using namespace CNORXZ; using namespace CNORXZ;
@ -16,8 +17,12 @@ PyMODINIT_FUNC PyInit_cnorxz()
{ {
PyObject* m; PyObject* m;
PyTypeObject* carray_type = PyCArrayBType_init(); PyTypeObject* cxarray_type = PyCxArrayBType_init();
if(carray_type == NULL){ PyTypeObject* cxrange_type = PyCxRangeType_init();
if(cxarray_type == NULL){
return NULL;
}
if(cxrange_type == NULL){
return NULL; return NULL;
} }
@ -26,9 +31,11 @@ PyMODINIT_FUNC PyInit_cnorxz()
return NULL; return NULL;
} }
Py_INCREF(carray_type); Py_INCREF(cxarray_type);
Py_INCREF(cxrange_type);
PyModule_AddObject(m, "CArray", (PyObject*) carray_type); PyModule_AddObject(m, "Array", (PyObject*) cxarray_type);
PyModule_AddObject(m, "Range", (PyObject*) cxrange_type);
return m; return m;
} }

View file

@ -59,30 +59,36 @@ namespace CNORXZ
}; };
template <typename T> template <typename T>
class CArrayWrapper : public CArrayWrapperBase class ArrayWrapper : public CArrayWrapperBase
{ {
private: private:
Sptr<CArrayBase<T>> mArr; Sptr<ArrayBase<T>> mArr;
public: public:
CArrayWrapper() : mArr( std::make_shared<MArray<T>>() ) {} ArrayWrapper() : mArr( std::make_shared<MArray<T>>() ) {}
CArrayWrapper(const RangePtr& r) : mArr( std::make_shared<MArray<T>>(r) ) {} ArrayWrapper(const RangePtr& r) : mArr( std::make_shared<MArray<T>>(r) ) {}
virtual RangePtr range() const override final { return mArr->range(); } virtual RangePtr range() const override final { return mArr->range(); }
virtual SizeT size() const override final { return mArr->size(); } virtual SizeT size() const override final { return mArr->size(); }
#ifdef HAVE_CEREAL #ifdef HAVE_CEREAL
virtual void writeFile(cer::Format f, const String& fname) const override final virtual void writeFile(cer::Format f, const String& fname) const override final
{ {
if(f == cer::Format::XML) { cer::writeXMLFile(fname, *mArr); } auto a = std::dynamic_pointer_cast<MArray<T>>( mArr );
else if(f == cer::Format::JSON) { cer::writeJSONFile(fname, *mArr); } if(a){
else { cer::writeBINARYFile(fname, *mArr); } if(f == cer::Format::XML) { cer::writeXMLFile(fname, *a); }
else if(f == cer::Format::JSON) { cer::writeJSONFile(fname, *a); }
else { cer::writeBINARYFile(fname, *a); }
}
} }
virtual void readFile(cer::Format f, const String& fname) const override final virtual void readFile(cer::Format f, const String& fname) const override final
{ {
if(f == cer::Format::XML) { cer::readXMLFile(fname, *mArr); } auto a = std::dynamic_pointer_cast<MArray<T>>( mArr );
else if(f == cer::Format::JSON) { cer::readJSONFile(fname, *mArr); } if(a){
else { cer::readBINARYFile(fname, *mArr); } if(f == cer::Format::XML) { cer::readXMLFile(fname, *a); }
else if(f == cer::Format::JSON) { cer::readJSONFile(fname, *a); }
else { cer::readBINARYFile(fname, *a); }
}
} }
#endif #endif
virtual int typenum() const override final { return Typenum<T>::value; } virtual int typenum() const override final { return Typenum<T>::value; }
@ -93,18 +99,18 @@ namespace CNORXZ
} }
struct PyCArrayB struct PyCxArrayB
{ {
PyObject_HEAD PyObject_HEAD
CNORXZ::CArrayWrapperBase* ptrObj; CNORXZ::CArrayWrapperBase* ptrObj;
}; };
int PyCArrayB_init(PyCArrayB* self, PyObject* args, PyObject* kwds); int PyCxArrayB_init(PyCxArrayB* self, PyObject* args, PyObject* kwds);
void PyCArrayB_dealloc(PyCArrayB* self); void PyCxArrayB_dealloc(PyCxArrayB* self);
PyObject* PyCArrayB_size(PyCArrayB* self); PyObject* PyCxArrayB_size(PyCxArrayB* self);
PyTypeObject* PyCArrayBType_init(); PyTypeObject* PyCxArrayBType_init();
extern PyMethodDef PyCArrayB_methods[]; extern PyMethodDef PyCxArrayB_methods[];
extern PyTypeObject PyCArrayBType; extern PyTypeObject PyCxArrayBType;
#endif #endif

View file

@ -7,35 +7,35 @@
namespace CNORXZ namespace CNORXZ
{ {
class CRangeWrapper class RangeWrapper
{ {
public: public:
DEFAULT_MEMBERS(CRangeWrapper); DEFAULT_MEMBERS(RangeWrapper);
RangeWrapper(const RangePtr r) : mR(r) {}
inline bool _init() const { return mR != nullptr; } inline bool _init() const { return mR != nullptr; }
inline SizeT size() const { return mR->size(); } inline SizeT size() const { return mR->size(); }
inline SizeT dim() const { return mR->dim(); } inline SizeT dim() const { return mR->dim(); }
inline String stringMeta(SizeT pos) const { mR->stringMeta(pos); } inline String stringMeta(SizeT pos) const { return mR->stringMeta(pos); }
private: private:
RangePtr mR; RangePtr mR;
}; };
} }
struct PyCRange struct PyCxRange
{ {
PyObject_HEAD PyObject_HEAD
CNORXZ::CRangeWrapper* ptrObj; CNORXZ::RangeWrapper* ptrObj;
}; };
int PyCRange_init(PyCRange* self, PyObject* args, PyObject* kwds); int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds);
void PyCRange_dealloc(PyCRange* self); void PyCxRange_dealloc(PyCxRange* self);
PyObject* PyCRange_size(PyCRange* self); PyObject* PyCxRange_size(PyCxRange* self);
PyObject* PyCRange_dim(PyCRange* self); PyObject* PyCxRange_dim(PyCxRange* self);
PyObject* PyCRange_stringMeta(PyCRange* self, PyObject* args, PyObject* kwds); PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds);
PyTypeObject* PyCRange_init(); PyTypeObject* PyCxRangeType_init();
extern PyMethodDef PyCRange_methods[]; extern PyMethodDef PyCxRange_methods[];
extern PyTypeObject PyCRangeType; extern PyTypeObject PyCxRangeType;
#endif #endif

View file

@ -1,11 +1,12 @@
#include "array_wrapper.h" #include "array_wrapper.h"
#include "range_wrapper.h"
#include "numpy/arrayobject.h" #include "numpy/arrayobject.h"
using namespace CNORXZ; using namespace CNORXZ;
using namespace CNORXZ::cer; using namespace CNORXZ::cer;
int PyCArrayB_init(PyCArrayB* self, PyObject* args, PyObject* kwds) int PyCxArrayB_init(PyCxArrayB* self, PyObject* args, PyObject* kwds)
{ {
static char* kwlist[] = { "type", "extension" , NULL }; static char* kwlist[] = { "type", "extension" , NULL };
SizeT ext = 0; SizeT ext = 0;
@ -18,21 +19,21 @@ int PyCArrayB_init(PyCArrayB* self, PyObject* args, PyObject* kwds)
if(ext != 0){ if(ext != 0){
RangePtr r = CRangeFactory(ext).create(); RangePtr r = CRangeFactory(ext).create();
// TODO: different types!!! // TODO: different types!!!
self->ptrObj = new CArrayWrapper<Double>(r); self->ptrObj = new ArrayWrapper<Double>(r);
} }
else { else {
self->ptrObj = new CArrayWrapper<Double>(); self->ptrObj = new ArrayWrapper<Double>();
} }
return 0; return 0;
} }
void PyCArrayB_dealloc(PyCArrayB* self) void PyCxArrayB_dealloc(PyCxArrayB* self)
{ {
delete self->ptrObj; delete self->ptrObj;
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
} }
PyObject* PyCArrayB_size(PyCArrayB* self) PyObject* PyCxArrayB_size(PyCxArrayB* self)
{ {
if(not self->ptrObj->range()){ if(not self->ptrObj->range()){
PyErr_SetString(PyExc_RuntimeError, "array not initialized"); PyErr_SetString(PyExc_RuntimeError, "array not initialized");
@ -42,14 +43,18 @@ PyObject* PyCArrayB_size(PyCArrayB* self)
return Py_BuildValue("k", retval); return Py_BuildValue("k", retval);
} }
PyObject* PyCArrayB_range(PyCArrayB* self) PyObject* PyCxArrayB_range(PyCxArrayB* self)
{ {
if(not self->ptrObj->range()){ if(not self->ptrObj->range()){
PyErr_SetString(PyExc_RuntimeError, "array not initialized"); PyErr_SetString(PyExc_RuntimeError, "array not initialized");
return NULL; return NULL;
} }
const RangePtr retval = self->ptrObj->range(); const RangePtr r = self->ptrObj->range();
return Py_BuildValue("k", retval); PyCxRange* o;
o = PyObject_New(PyCxRange, &PyCxRangeType);
o->ptrObj = new RangeWrapper(r);
//return Py_BuildValue("k", retval);
return Py_BuildValue("O", o);
} }
Format formatFromString(const char* fstr) Format formatFromString(const char* fstr)
@ -72,7 +77,7 @@ Format formatFromString(const char* fstr)
return f; return f;
} }
void PyCArrayB_writeFile(PyCArrayB* self, PyObject* args, PyObject* kwds) void PyCxArrayB_writeFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
{ {
if(not self->ptrObj->range()){ if(not self->ptrObj->range()){
PyErr_SetString(PyExc_RuntimeError, "array not initialized"); PyErr_SetString(PyExc_RuntimeError, "array not initialized");
@ -89,7 +94,7 @@ void PyCArrayB_writeFile(PyCArrayB* self, PyObject* args, PyObject* kwds)
self->ptrObj->writeFile(f, fn); self->ptrObj->writeFile(f, fn);
} }
void PyCArrayB_readFile(PyCArrayB* self, PyObject* args, PyObject* kwds) void PyCxArrayB_readFile(PyCxArrayB* self, PyObject* args, PyObject* kwds)
{ {
static char* kwlist[] = { "format", "filename" , NULL }; static char* kwlist[] = { "format", "filename" , NULL };
char* fname = NULL; char* fname = NULL;
@ -103,7 +108,7 @@ void PyCArrayB_readFile(PyCArrayB* self, PyObject* args, PyObject* kwds)
self->ptrObj->readFile(f, fn); self->ptrObj->readFile(f, fn);
} }
PyObject* PyCArrayB_npa(PyCArrayB* self) PyObject* PyCxArrayB_npa(PyCxArrayB* self)
{ {
// return viewing (!) numpy array // return viewing (!) numpy array
if(not self->ptrObj->range()){ if(not self->ptrObj->range()){
@ -121,7 +126,8 @@ PyObject* PyCArrayB_npa(PyCArrayB* self)
PyObject* o = PyArray_SimpleNew PyObject* o = PyArray_SimpleNew
( static_cast<int>( d ) , dims.data(), self->ptrObj->typenum() ); ( static_cast<int>( d ) , dims.data(), self->ptrObj->typenum() );
void* data = PyArray_DATA(o); void* data = PyArray_DATA(o);
const npy_intp s = PyArray_NBYTES(o); //const npy_intp s = PyArray_NBYTES(o);
const SizeT s = static_cast<SizeT>( PyArray_NBYTES(o) );
if(s != self->ptrObj->datasize()) { if(s != self->ptrObj->datasize()) {
PyErr_SetString(PyExc_RuntimeError, "lib error"); PyErr_SetString(PyExc_RuntimeError, "lib error");
} }
@ -129,31 +135,33 @@ PyObject* PyCArrayB_npa(PyCArrayB* self)
return o; return o;
} }
PyMethodDef PyCArrayB_methods[] = { PyMethodDef PyCxArrayB_methods[] = {
{ "size", (PyCFunction) PyCArrayB_size, METH_VARARGS| METH_KEYWORDS, "return size of the array" }, { "size", (PyCFunction) PyCxArrayB_size, METH_VARARGS| METH_KEYWORDS, "return size of the array" },
{ "range", (PyCFunction) PyCArrayB_range, METH_VARARGS| METH_KEYWORDS, "return array range" }, { "range", (PyCFunction) PyCxArrayB_range, METH_VARARGS| METH_KEYWORDS, "return array range" },
{ "writeFile", (PyCFunction) PyCArrayB_writeFile, METH_VARARGS| METH_KEYWORDS, "write array to file" }, { "writeFile", (PyCFunction) PyCxArrayB_writeFile, METH_VARARGS| METH_KEYWORDS, "write array to file" },
{ "readFile", (PyCFunction) PyCArrayB_readFile, METH_VARARGS| METH_KEYWORDS, "read array from file" }, { "readFile", (PyCFunction) PyCxArrayB_readFile, METH_VARARGS| METH_KEYWORDS, "read array from file" },
{ "npa", (PyCFunction) PyCArrayB_npa, METH_VARARGS| METH_KEYWORDS, "return numpy array (view)" }, { "npa", (PyCFunction) PyCxArrayB_npa, METH_VARARGS| METH_KEYWORDS, "return numpy array (view)" },
{ NULL, NULL, 0, NULL } { NULL, NULL, 0, NULL }
}; };
PyTypeObject PyCArrayBType = { PyVarObject_HEAD_INIT(NULL,0) "cnorxz.CArray" }; PyTypeObject PyCxArrayBType = { PyVarObject_HEAD_INIT(NULL,0) "cnorxz.Array" };
PyTypeObject* PyCArrayBType_init() PyTypeObject* PyCxArrayBType_init()
{ {
PyCArrayBType.tp_new = PyType_GenericNew; PyCxArrayBType.tp_new = PyType_GenericNew;
PyCArrayBType.tp_basicsize = sizeof(PyCArrayB); PyCxArrayBType.tp_basicsize = sizeof(PyCxArrayB);
PyCArrayBType.tp_dealloc = (destructor) PyCArrayB_dealloc; PyCxArrayBType.tp_dealloc = (destructor) PyCxArrayB_dealloc;
PyCArrayBType.tp_flags = Py_TPFLAGS_DEFAULT; PyCxArrayBType.tp_flags = Py_TPFLAGS_DEFAULT;
PyCArrayBType.tp_doc = "cnorxz CArray wrapper"; PyCxArrayBType.tp_doc = "cnorxz Array wrapper";
PyCArrayBType.tp_methods = PyCArrayB_methods; PyCxArrayBType.tp_methods = PyCxArrayB_methods;
PyCArrayBType.tp_init = (initproc) PyCArrayB_init; PyCxArrayBType.tp_init = (initproc) PyCxArrayB_init;
if(PyType_Ready(&PyCArrayBType) < 0) { //import_array();
if(PyType_Ready(&PyCxArrayBType) < 0) {
return NULL; return NULL;
} }
else { else {
return &PyCArrayBType; return &PyCxArrayBType;
} }
} }

View file

@ -3,18 +3,18 @@
using namespace CNORXZ; using namespace CNORXZ;
int PyCRange_init(PyCRange* self, PyObject* args, PyObject* kwds) int PyCxRange_init(PyCxRange* self, PyObject* args, PyObject* kwds)
{ {
//!!! //!!!
} }
void PyCRange_dealloc(PyCRange* self) void PyCxRange_dealloc(PyCxRange* self)
{ {
delete self->ptrObj; delete self->ptrObj;
Py_TYPE(self)->tp_free(self); Py_TYPE(self)->tp_free(self);
} }
PyObject* PyCRange_size(PyCRange* self) PyObject* PyCxRange_size(PyCxRange* self)
{ {
if(not self->ptrObj->_init()){ if(not self->ptrObj->_init()){
const SizeT retval = 0; const SizeT retval = 0;
@ -24,7 +24,7 @@ PyObject* PyCRange_size(PyCRange* self)
return Py_BuildValue("k", retval); return Py_BuildValue("k", retval);
} }
PyObject* PyCRange_dim(PyCRange* self) PyObject* PyCxRange_dim(PyCxRange* self)
{ {
if(not self->ptrObj->_init()){ if(not self->ptrObj->_init()){
const SizeT retval = 0; const SizeT retval = 0;
@ -34,37 +34,35 @@ PyObject* PyCRange_dim(PyCRange* self)
return Py_BuildValue("k", retval); return Py_BuildValue("k", retval);
} }
PyObject* PyCRange_stringMeta(PyCRange* self, PyObject* args, PyObject* kwds) PyObject* PyCxRange_stringMeta(PyCxRange* self, PyObject* args, PyObject* kwds)
{ {
//!!! //!!!
} }
PyMethodDef PyCRange_methods[] = { PyMethodDef PyCxRange_methods[] = {
{ "size", (PyCFunction) PyCRange_size, METH_VARARGS, "return range size" }, { "size", (PyCFunction) PyCxRange_size, METH_VARARGS, "return range size" },
{ "dim", (PyCFunction) PyCRange_dim, METH_VARARGS, "return range dimension" }, { "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, "return range dimension" },
{ "stringMeta", (PyCFunction) PyCRange_stringMeta, METH_VARARGS, { "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS,
"return string meta data for given range position" }, "return string meta data for given range position" },
{ NULL } { NULL }
}; };
PyTypeObject PyCRangeType = { PyVarObject_HEAD_INIT(NULL,0) "cnorxz.CRange" }; PyTypeObject PyCxRangeType = { PyVarObject_HEAD_INIT(NULL,0) "cnorxz.Range" };
PyTypeObject* PyCRangeType_init() PyTypeObject* PyCxRangeType_init()
{ {
PyCRangeType.tp_new = PyType_GenericNew; PyCxRangeType.tp_new = PyType_GenericNew;
PyCRangeType.tp_basicsize = sizeof(PyCRange); PyCxRangeType.tp_basicsize = sizeof(PyCxRange);
PyCRangeType.tp_dealloc = (destructor) PyCRange_dealloc; PyCxRangeType.tp_dealloc = (destructor) PyCxRange_dealloc;
PyCRangeType.to_flags = Py_TPFLAGS_DEFAULT; PyCxRangeType.tp_flags = Py_TPFLAGS_DEFAULT;
PyCRangeType.tp_doc = "cnorxz CRange wrapper"; PyCxRangeType.tp_doc = "cnorxz Range wrapper";
PyCRangeType.tp_methods = PyCRange_methods; PyCxRangeType.tp_methods = PyCxRange_methods;
PyCRangeType.tp_init = (initproc) PyCRange_init; PyCxRangeType.tp_init = (initproc) PyCxRange_init;
import_array(); if(PyType_Ready(&PyCxRangeType) < 0){
if(PyType_Ready(&PyCRangeType) < 0){
return NULL; return NULL;
} }
else { else {
return &PyCRangeType; return &PyCxRangeType;
} }
} }

View file

@ -40,6 +40,7 @@ lib_dirs.append( "/home/chizeta/repos/cnorxz/install/lib" )
extra_compile_args = sysconfig.get_config_var('CFLAGS').split() extra_compile_args = sysconfig.get_config_var('CFLAGS').split()
cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split() cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split()
cnorxz_flags.remove("-Werror") cnorxz_flags.remove("-Werror")
cnorxz_flags.append("-Wno-write-strings")
extra_compile_args += cnorxz_flags extra_compile_args += cnorxz_flags
default_extension_args = dict( default_extension_args = dict(
@ -79,7 +80,7 @@ if __name__ == "__main__":
version = version, version = version,
include_dirs = inc_dirs, include_dirs = inc_dirs,
ext_modules = [Extension( ext_modules = [Extension(
'cnorxz', ['cnorxz/core/core.cpp','cnorxz/core/lib/array_wrapper.cpp'], 'cnorxz', ['cnorxz/core/core.cpp','cnorxz/core/lib/array_wrapper.cpp','cnorxz/core/lib/range_wrapper.cpp'],
define_macros=[('HAVE_CEREAL',None)], define_macros=[('HAVE_CEREAL',None)],
**default_extension_args **default_extension_args
)], )],