File: pykstpp.py

package info (click to toggle)
kst 2.0.8-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 30,748 kB
  • sloc: cpp: 97,086; ansic: 13,364; python: 2,970; sh: 761; yacc: 184; lex: 143; makefile: 141; javascript: 122; perl: 30; xml: 30
file content (265 lines) | stat: -rw-r--r-- 6,799 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as npy
from scipy.weave import inline
import pykstpp_h as pykstpp_h

def get_arr(arr,socket,handle):
    dtype2ctype = {
        npy.dtype(npy.float64): 'double',
        npy.dtype(npy.float32): 'float',
        npy.dtype(npy.int32): 'int',
        npy.dtype(npy.int16): 'short',
    }
    dt = dtype2ctype.get(arr.dtype)	#I'm leaving this code here in case anyone wants to mess with it.
    assert(dt=='double') # Only use float64 arrays!

    code = \
"""

//
// I don't usually program in python, but when I do, I program in C++
//


QLocalSocket s;
s.connectToServer(QString(socket.c_str()));
s.waitForConnected(300);
s.write("Vector::getBinaryArray("+QByteArray(handle.c_str())+")");
QDataStream ds(&s);
qint64 count;
s.waitForReadyRead(-1);
ds>>count;

PyArray_Dims dims;
dims.len = 1;
dims.ptr = new npy_intp[1];
dims.ptr[0] = (npy_intp)count;
PyArray_Resize(arr_array, &dims, 0, PyArray_ANYORDER);
delete[]dims.ptr;

NpyIter* it=NpyIter_New(arr_array,NPY_ITER_READWRITE,NPY_CORDER,NPY_NO_CASTING,NULL);
char**dataptr = NpyIter_GetDataPtrArray(it);
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(it, NULL);

do {
	char* data=*dataptr;	//i.e., byte array
	while(ds.atEnd()) {s.waitForReadyRead(-1);}	//possible optimization: call ds.atEnd() less
	ds>>((double*)data)[0];	//possible optimization: QByteArray switches the endianess twice...
} while(iternext(it));

""" #% (dt)


    support_code = \
"""
#include <QtCore>
#include <QLocalSocket>
"""

    socket=str(socket)
    handle=str(handle)
    inline(code, ['arr','socket','handle'],support_code=support_code,include_dirs=pykstpp_h.INCLUDES,libraries=pykstpp_h.LIBS,runtime_library_dirs=pykstpp_h.LIBDIRS)
    # I'm not sure if the above line can be made portable or not...








def get_matrix(arr,socket,handle):
    dtype2ctype = {
        npy.dtype(npy.float64): 'double',
        npy.dtype(npy.float32): 'float',
        npy.dtype(npy.int32): 'int',
        npy.dtype(npy.int16): 'short',
    }
    dt = dtype2ctype.get(arr.dtype)	#I'm leaving this code here in case anyone wants to mess with it.
    assert(dt=='double') # Only use float64 arrays!

    code = \
"""

//
// I don't usually program in python, but when I do, I program in C++
//


QLocalSocket s;
s.connectToServer(QString(socket.c_str()));
s.waitForConnected(300);
s.write("Matrix::getBinaryArray("+QByteArray(handle.c_str())+")");
QDataStream ds(&s);
qint64 count;
s.waitForReadyRead(-1);
qint32 nX,nY;
double minX,minY,stepX,stepY;
ds>>nX>>nY>>minX>>minY>>stepX>>stepY;

PyArray_Dims dims;
dims.len = 2;
dims.ptr = new npy_intp[2];
dims.ptr[0] = (npy_intp)nX;
dims.ptr[1] = (npy_intp)nY;
PyArray_Resize(arr_array, &dims, 0, PyArray_ANYORDER);
delete[]dims.ptr;

NpyIter* it=NpyIter_New(arr_array,NPY_ITER_READWRITE,NPY_CORDER,NPY_NO_CASTING,NULL);
char**dataptr = NpyIter_GetDataPtrArray(it);
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(it, NULL);

do {
	char* data=*dataptr;	//i.e., byte array
	while(ds.atEnd()) {s.waitForReadyRead(-1);}	//possible optimization: call ds.atEnd() less
	ds>>((double*)data)[0];	//possible optimization: QByteArray switches the endianess twice...
} while(iternext(it));

""" #% (dt)


    support_code = \
"""
#include <QtCore>
#include <QString>
#include <QDataStream>
#include <QLocalSocket>
"""

    socket=str(socket)
    handle=str(handle)
    inline(code, ['arr','socket','handle'],support_code=support_code,include_dirs=pykstpp_h.INCLUDES,libraries=pykstpp_h.LIBS,runtime_library_dirs=pykstpp_h.LIBDIRS)
    # I'm not sure if the above line can be made portable or not...









def set_arr(arr,socket,handle):
    dtype2ctype = {
        npy.dtype(npy.float64): 'double',
        npy.dtype(npy.float32): 'float',
        npy.dtype(npy.int32): 'int',
        npy.dtype(npy.int16): 'short',
    }
    dt = dtype2ctype.get(arr.dtype)
    assert(dt=='double') # Only use float64 arrays!

    code = \
"""

//
// I don't usually program in python, but when I do, I program in C++
//

QLocalSocket s;
s.connectToServer(QString(socket.c_str()));
s.waitForConnected(300);
s.write("EditableVector::setBinaryArray("+QByteArray(handle.c_str())+")");
s.waitForReadyRead(-1);
QDataStream ds(&s);
ds<<(qint64)PyArray_SIZE(arr_array);

NpyIter* it=NpyIter_New(arr_array,NPY_ITER_READWRITE,NPY_CORDER,NPY_NO_CASTING,NULL);
char**dataptr = NpyIter_GetDataPtrArray(it);
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(it, NULL);

int l=0;
int v=0;
do {
	++v;
	char* data=*dataptr;	//i.e., byte array
	ds<<((double*)data)[0];	//possible optimization: QByteArray switches the endianess twice...
	if(++l==512) {
		while(s.bytesToWrite()) {
			s.waitForBytesWritten(-1);
		}
		l=0;
	}
} while(iternext(it));
while(s.bytesToWrite()) {
	s.waitForBytesWritten(-1);
}

""" #% (dt)


    support_code = \
"""
#include <QtCore>
#include <QString>
#include <QDataStream>
#include <QLocalSocket>
"""

    socket=str(socket)
    handle=str(handle)
    inline(code, ['arr','socket','handle'],support_code=support_code,include_dirs=pykstpp_h.INCLUDES,libraries=pykstpp_h.LIBS,runtime_library_dirs=pykstpp_h.LIBDIRS)
    # I'm not sure if the above line can be made portable or not...




def set_matrix(arr,nX,nY,socket,handle):
    dtype2ctype = {
        npy.dtype(npy.float64): 'double',
        npy.dtype(npy.float32): 'float',
        npy.dtype(npy.int32): 'int',
        npy.dtype(npy.int16): 'short',
    }
    dt = dtype2ctype.get(arr.dtype)
    assert(dt=='double') # Only use float64 arrays!

    code = \
"""

//
// I don't usually program in python, but when I do, I program in C++
//

QLocalSocket s;
s.connectToServer(QString(socket.c_str()));
s.waitForConnected(300);
s.write("EditableMatrix::setBinaryArray("+QByteArray(handle.c_str())+","+QByteArray::number(nX)+","+QByteArray::number(nY)+","+"0.0,0.0,1.0,1.0)");
s.waitForReadyRead(-1);
QDataStream ds(&s);

NpyIter* it=NpyIter_New(arr_array,NPY_ITER_READWRITE,NPY_CORDER,NPY_NO_CASTING,NULL);
char**dataptr = NpyIter_GetDataPtrArray(it);
NpyIter_IterNextFunc *iternext = NpyIter_GetIterNext(it, NULL);

int l=0;
int v=0;
do {
	++v;
	char* data=*dataptr;	//i.e., byte array
	ds<<((double*)data)[0];	//possible optimization: QByteArray switches the endianess twice...
	if(++l==512) {
		while(s.bytesToWrite()) {
			s.waitForBytesWritten(-1);
		}
		l=0;
	}
} while(iternext(it));
while(s.bytesToWrite()) {
	s.waitForBytesWritten(-1);
}

""" #% (dt)


    support_code = \
"""
#include <QtCore>
#include <QString>
#include <QDataStream>
#include <QLocalSocket>
"""

    socket=str(socket)
    handle=str(handle)
    inline(code, ['arr','nX','nY','socket','handle'],support_code=support_code,include_dirs=pykstpp_h.INCLUDES,libraries=pykstpp_h.LIBS,runtime_library_dirs=pykstpp_h.LIBDIRS)
    # I'm not sure if the above line can be made portable or not...