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 "array_wrapper.h"
#include "range_wrapper.h"
using namespace CNORXZ;
@ -16,8 +17,12 @@ PyMODINIT_FUNC PyInit_cnorxz()
{
PyObject* m;
PyTypeObject* carray_type = PyCArrayBType_init();
if(carray_type == NULL){
PyTypeObject* cxarray_type = PyCxArrayBType_init();
PyTypeObject* cxrange_type = PyCxRangeType_init();
if(cxarray_type == NULL){
return NULL;
}
if(cxrange_type == NULL){
return NULL;
}
@ -26,9 +31,11 @@ PyMODINIT_FUNC PyInit_cnorxz()
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;
}

View file

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

View file

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

View file

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

View file

@ -3,18 +3,18 @@
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;
Py_TYPE(self)->tp_free(self);
}
PyObject* PyCRange_size(PyCRange* self)
PyObject* PyCxRange_size(PyCxRange* self)
{
if(not self->ptrObj->_init()){
const SizeT retval = 0;
@ -24,7 +24,7 @@ PyObject* PyCRange_size(PyCRange* self)
return Py_BuildValue("k", retval);
}
PyObject* PyCRange_dim(PyCRange* self)
PyObject* PyCxRange_dim(PyCxRange* self)
{
if(not self->ptrObj->_init()){
const SizeT retval = 0;
@ -34,37 +34,35 @@ PyObject* PyCRange_dim(PyCRange* self)
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[] = {
{ "size", (PyCFunction) PyCRange_size, METH_VARARGS, "return range size" },
{ "dim", (PyCFunction) PyCRange_dim, METH_VARARGS, "return range dimension" },
{ "stringMeta", (PyCFunction) PyCRange_stringMeta, METH_VARARGS,
PyMethodDef PyCxRange_methods[] = {
{ "size", (PyCFunction) PyCxRange_size, METH_VARARGS, "return range size" },
{ "dim", (PyCFunction) PyCxRange_dim, METH_VARARGS, "return range dimension" },
{ "stringMeta", (PyCFunction) PyCxRange_stringMeta, METH_VARARGS,
"return string meta data for given range position" },
{ 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;
PyCRangeType.tp_basicsize = sizeof(PyCRange);
PyCRangeType.tp_dealloc = (destructor) PyCRange_dealloc;
PyCRangeType.to_flags = Py_TPFLAGS_DEFAULT;
PyCRangeType.tp_doc = "cnorxz CRange wrapper";
PyCRangeType.tp_methods = PyCRange_methods;
PyCRangeType.tp_init = (initproc) PyCRange_init;
PyCxRangeType.tp_new = PyType_GenericNew;
PyCxRangeType.tp_basicsize = sizeof(PyCxRange);
PyCxRangeType.tp_dealloc = (destructor) PyCxRange_dealloc;
PyCxRangeType.tp_flags = Py_TPFLAGS_DEFAULT;
PyCxRangeType.tp_doc = "cnorxz Range wrapper";
PyCxRangeType.tp_methods = PyCxRange_methods;
PyCxRangeType.tp_init = (initproc) PyCxRange_init;
import_array();
if(PyType_Ready(&PyCRangeType) < 0){
if(PyType_Ready(&PyCxRangeType) < 0){
return NULL;
}
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()
cnorxz_flags = subprocess.run([path_to_cnorxz+"/bin/cnorxz-config",'--flags'],stdout=subprocess.PIPE).stdout.decode('ascii').split()
cnorxz_flags.remove("-Werror")
cnorxz_flags.append("-Wno-write-strings")
extra_compile_args += cnorxz_flags
default_extension_args = dict(
@ -79,7 +80,7 @@ if __name__ == "__main__":
version = version,
include_dirs = inc_dirs,
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)],
**default_extension_args
)],