File: PConv.h

package info (click to toggle)
pymol 1.8.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,248 kB
  • ctags: 24,095
  • sloc: cpp: 474,635; python: 75,034; ansic: 22,888; sh: 236; makefile: 78; csh: 21
file content (378 lines) | stat: -rw-r--r-- 12,901 bytes parent folder | download | duplicates (2)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378

/* 
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific. 
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information. 
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-* 
-* 
-*
Z* -------------------------------------------------------------------
*/
#ifndef _H_PConv
#define _H_PConv

#include"os_python.h"

#include"Base.h"
#include"OVLexicon.h"

#include <map>
#include <set>
#include <string>
#include <vector>

/* Convenient conversion routines for C<->Python data interchange
   
   Note that all of these routines assume that we have the global
   interpreter lock - blocking all other threads.
   
   There are three ways to get it:
   
   - call PBlock() [followe by PUnblock() when done]
   
   - call PBlockAndUnlockAPI - [followed by PLockAPIAndUnblock() when
   done]
   
   - or in response to a call to the PM API, you will have the main
   Python thread by default.  [Note that within an
   APIEntry(),APIExit() block the lock is released, so these
   functions should be called outside of that block].

*/

// CPythonVal macros
#define CPythonVal PyObject
#define CPythonVal_PyString_Check                       PyString_Check
#define CPythonVal_PyList_Check                         PyList_Check
#define CPythonVal_PyList_Size                          PyList_Size
#define CPythonVal_PyList_GetItem(G, list, i)           PyList_GetItem(list, i)
#define CPythonVal_PyDict_GetItemString(G, p, key)      PyDict_GetItemString(p, key)
#define CPythonVal_PConvPyIntToInt                                              PConvPyIntToInt
#define CPythonVal_PConvPyIntToInt_From_List(G, list, i, ptr)                   PConvPyIntToInt(PyList_GetItem(list, i), ptr)
#define CPythonVal_PConvPyFloatToFloat_From_List(G, list, i, ptr)               PConvPyFloatToFloat(PyList_GetItem(list, i), ptr)
#define CPythonVal_PConvPyListToIntArrayInPlace(G, obj, ff, ll)                 PConvPyListToIntArrayInPlace(obj, ff, ll)
#define CPythonVal_PConvPyListToIntArrayInPlace_From_List(G, list, i, ff, ll)   PConvPyListToIntArrayInPlace(PyList_GetItem(list, i), ff, ll)
#define CPythonVal_PConvPyListToFloatArrayInPlaceAutoZero_From_List(G, list, i, ...) \
                   PConvPyListToFloatArrayInPlaceAutoZero(PyList_GetItem(list, i), __VA_ARGS__)
#define CPythonVal_PConvPyListToFloatVLANoneOkay_From_List(G, list, i, f)       PConvPyListToFloatVLANoneOkay(PyList_GetItem(list, i), f)
#define CPythonVal_PConvPyListToLabPosVLA(G, obj, vla_ptr)                      PConvPyListToLabPosVLA(obj, vla_ptr)
#define CPythonVal_PConvPyStrToStr_From_List(G, list, i, ptr, l)                PConvPyStrToStr(PyList_GetItem(list, i), ptr, l)

#define CPythonVal_Free(obj)
#define CPythonVal_FreeAll(PYOBJECT)

#define CPythonVal_New(G, PYOBJECT)                     PYOBJECT
#define CPythonVal_Append_List(LIST, ITEM)              PyList_Append(LIST, ITEM)
#define CPythonVal_New_List()                           PyList_New(0)
#define CPythonVal_New_Tuple(SIZE)                      PyTuple_New(SIZE)
#define CPythonVal_Tuple_SetItem(TUPLE, ITEM, VAL)      PyTuple_SetItem(TUPLE, ITEM, VAL)
#define CPythonVal_New_String(BUF, LEN)                 PyString_FromStringAndSize(BUF, LEN)
#define CPythonVal_New_Boolean(VAL)                     (VAL ? PyBool_FromLong(1) : PyBool_FromLong(0))
#define CPythonVal_New_Integer(VAL)                     PyInt_FromLong(VAL)
#define CPythonVal_New_Float(VAL)                       PyFloat_FromDouble(VAL)

#define CPythonVal_IsNone(PYOBJECT)                     (PYOBJECT == Py_None)

/* == error-checking routines: true = success, false = failure. */


/* NOTE: the string routines will write strings up to the specified
 * length, PLUS a NULL...so watch out for array overruns */

int PConvAttrToStrMaxLen(PyObject * obj, const char *attr, char *str, ov_size ll);

int PConvPyListToBitmask(PyObject * obj, int *bitmask, ov_size ll);
int PConvPyListToExtent(PyObject * obj, float *mn, float *mx);

int PConvAttrToFloatArrayInPlace(PyObject * obj, const char *attr, float *ff, ov_size ll);
int PConvAttrToIntArrayInPlace(PyObject * obj, const char *attr, int *ff, ov_size ll);
int PConvAttrToPtr(PyObject * obj, const char *name, void **cobj);

int PConvCObjectToPtr(PyObject * obj, void **ptr);
int PConvPyListToStrVLAList(PyObject * obj, char **vla, int *n_str);

int PConvPyListToStringVLA(PyObject * obj, char **vla_ptr);
#define PConvPyListToIntVLA(obj, f)     PConvPyListToIntArrayImpl(obj, f, true)

int PConvPyStrToStr(PyObject * obj, char *ptr, int l);
#ifndef _PYMOL_NOPY
int PConvPyStrToStrPtr(PyObject * obj, char **ptr);
#endif
int PConvPyStrToLexRef(PyObject * obj, OVLexicon * lex, int *lex_ref);
int PConvPyFloatToFloat(PyObject * obj, float *ptr);
int PConvPyIntToChar(PyObject * obj, char *ptr);
int PConvPyIntToInt(PyObject * obj, int *ptr);
int PConvPyBoolToInt(PyObject * obj, int *ptr);
int PConvPyListToLabPosVLA(PyObject * obj, LabPosType ** vla_ptr);


/* Jenarix conventions -- returns before args */

ov_status PConvPyTupleToIntVLA(int **result, PyObject * tuple);
ov_status PConvPyTupleToFloatVLA(float **result, PyObject * tuple);


/* === end === */


/* categories below... */

PyObject *PConvFloatVLAToPyList(const float *vla);
PyObject *PConvFloatVLAToPyTuple(float *vla);
PyObject *PConvIntVLAToPyList(const int *vla);
PyObject *PConvIntVLAToPyTuple(int *vla);
PyObject *PConvIntArrayToPyList(const int *f, int l, bool dump_binary=false);
PyObject *PConvSIntArrayToPyList(const short int *f, int l);
PyObject *PConvSCharArrayToPyList(const signed char *f, int l);
PyObject *PConvLabPosVLAToPyList(const LabPosType * vla, int l);

void PConvFloat3ToPyObjAttr(PyObject * obj, const char *attr, const float *v);
void PConvFloatToPyObjAttr(PyObject * obj, const char *attr, float f);
void PConvIntToPyObjAttr(PyObject * obj, const char *attr, int i);
void PConvInt2ToPyObjAttr(PyObject * obj, const char *attr, const int *v);
void PConvStringToPyObjAttr(PyObject * obj, const char *attr, const char *f);

int PConvPyObjectToFloat(PyObject * object, float *value);
int PConvPyObjectToInt(PyObject * object, int *value);
int PConvPyObjectToChar(PyObject * object, char *value);


/* NOTE: the string routines will write strings up to the specified
 * length, PLUS a NULL...so watch out for array overruns */

int PConvPyObjectToStrMaxLen(PyObject * object, char *value, int ln);
int PConvPyObjectToStrMaxClean(PyObject * object, char *value, int ln);

PyObject *PConvStringListToPyList(int l, const char * const *str);
PyObject *PConvStringVLAToPyList(const char *str);

void PConv44PyListTo44f(PyObject * src, float *dest);   /* note loss of precision */

#define PConvPyListToFloatVLA(obj, f)   PConvPyListToFloatArrayImpl(obj, f, true)
int PConvPyListToFloatVLANoneOkay(PyObject * obj, float **f);
int PConvPyList3ToFloatVLA(PyObject * obj, float **f);
#define PConvPyListToFloatArray(obj, f) PConvPyListToFloatArrayImpl(obj, f, false)
int PConvPyListToFloatArrayImpl(PyObject * obj, float **f, bool as_vla);
int PConvPyListToDoubleArray(PyObject * obj, double **f);
int PConvPyListToFloatArrayInPlace(PyObject * obj, float *ff, ov_size ll);
int PConvPyListOrTupleToFloatArrayInPlace(PyObject * obj, float *ff, ov_size ll);
int PConvPyListToFloatArrayInPlaceAutoZero(PyObject * obj, float *ii, ov_size ll);

int PConvPyListToDoubleArrayInPlace(PyObject * obj, double *ff, ov_size ll);

PyObject *PConvFloatArrayToPyList(const float *f, int l, bool dump_binary=false);
PyObject *PConvFloatArrayToPyListNullOkay(const float *f, int l);
PyObject *PConvDoubleArrayToPyList(const double *f, int l);

#define PConvPyListToIntArray(obj, f)   PConvPyListToIntArrayImpl(obj, f, false)
int PConvPyListToIntArrayImpl(PyObject * obj, int **f, bool as_vla);
int PConvPyListToIntArrayInPlace(PyObject * obj, int *ff, ov_size ll);
int PConvPyListToIntArrayInPlaceAutoZero(PyObject * obj, int *ii, ov_size ll);

int PConvPyListToSIntArrayInPlaceAutoZero(PyObject * obj, short int *ii, ov_size ll);
int PConvPyListToSCharArrayInPlaceAutoZero(PyObject * obj, signed char *ii, ov_size ll);

PyObject *PConv3DIntArrayTo3DPyList(int ***array, int *dim);

PyObject *PConvPickleLoads(PyObject * str);
PyObject *PConvPickleDumps(PyObject * obj);
PyObject *PConvAutoNone(PyObject * result);     /* automatically own Py_None */
PyObject *PConvIntToPyDictItem(PyObject * dict, const char *key, int i);

/* ============================================================ */
/*
 * PConvToPyObject: Convert any standart type (primitives
 * and c++ std library) to a python object.
 *
 * Return value: New reference.
 */
inline PyObject * PConvToPyObject(PyObject * v) {
  return v;
}

inline PyObject * PConvToPyObject(int v) {
  return PyInt_FromLong(v);
}

inline PyObject * PConvToPyObject(float v) {
  return PyFloat_FromDouble(v);
}

inline PyObject * PConvToPyObject(double v) {
  return PyFloat_FromDouble(v);
}

inline PyObject * PConvToPyObject(const std::string &v) {
  return PyString_FromString(v.c_str());
}

inline PyObject * PConvToPyObject(const char * v) {
  return PyString_FromString(v);
}

inline PyObject * PConvToPyObject(const float * v, int n) {
  return PConvFloatArrayToPyList((float*)v, n);
}

template <class T>
PyObject * PConvToPyObject(const std::vector<T> &v) {
  int n = v.size();
  PyObject * o = PyList_New(n);

  for (int i = 0; i < n; ++i) {
    PyList_SetItem(o, i, PConvToPyObject(v[i]));
  }

  return o;
}

/*
 * Convert a set to a Python list
 */
template <class T>
PyObject * PConvToPyObject(const std::set<T> &v) {
  size_t i = 0, n = v.size();
  PyObject * o = PyList_New(n);

  for (auto it = v.begin(); it != v.end(); ++it) {
    PyList_SET_ITEM(o, i++, PConvToPyObject(*it));
  }

  return o;
}

/*
 * Convert a map to a flat Python list
 *
 * {k1: v1, k2: v2, ...} -> [k1, v1, k2, v2, ...]
 */
template <class K, class V>
PyObject * PConvToPyObject(const std::map<K, V> &v) {
  size_t i = 0, n = v.size();
  PyObject * o = PyList_New(n * 2);

  for (auto it = v.begin(); it != v.end(); ++it) {
    PyList_SET_ITEM(o, i++, PConvToPyObject(it->first));
    PyList_SET_ITEM(o, i++, PConvToPyObject(it->second));
  }

  return o;
}

/* ============================================================ */
/*
 * PConvFromPyObject: Templated conversion of a python object to a
 * standart type (primitives and c++ std library).
 */

inline bool PConvFromPyObject(PyMOLGlobals *, PyObject * obj, int &out) {
  out = PyInt_AsLong(obj);
  return true;
}

inline bool PConvFromPyObject(PyMOLGlobals *, PyObject * obj, float &out) {
  out = PyFloat_AsDouble(obj);
  return true;
}

inline bool PConvFromPyObject(PyMOLGlobals *, PyObject * obj, double &out) {
  out = PyFloat_AsDouble(obj);
  return true;
}

inline bool PConvFromPyObject(PyMOLGlobals *, PyObject * obj, std::string &out) {
  out = PyString_AsSomeString(obj);
  return true;
}

inline bool PConvFromPyObject(PyMOLGlobals *, PyObject * obj, float * out) {
  return PConvPyListToFloatArrayInPlace(obj, out, 0);
}

template <class T>
bool PConvFromPyObject(PyMOLGlobals * G, PyObject * obj, std::vector<T> &out) {
  if (!PyList_Check(obj))
    return false;

  int n = PyList_Size(obj);

  out.clear();
  out.reserve(n);

  for (int i = 0; i < n; ++i) {
    PyObject *item = PyList_GET_ITEM(obj, i);

    T t;
    if (!PConvFromPyObject(G, item, t))
      return false;

    out.push_back(t);
  }

  return true;
}

/*
 * Convert a Python list to a set
 */
template <class T>
bool PConvFromPyObject(PyMOLGlobals * G, PyObject * obj, std::set<T> &out) {
  if (!PyList_Check(obj))
    return false;

  int n = PyList_Size(obj);

  out.clear();

  for (int i = 0; i < n; ++i) {
    PyObject *item = PyList_GET_ITEM(obj, i);

    T t;
    if (!PConvFromPyObject(G, item, t))
      return false;

    out.insert(t);
  }

  return true;
}

/*
 * Convert a flat Python list to a map, even indices are keys and odd
 * indices are values.
 *
 * [a, b, c, d, ...] -> {a: b, c: d, ...}
 */
template <class K, class V>
bool PConvFromPyObject(PyMOLGlobals * G, PyObject * obj, std::map<K, V> &out) {
  if (!PyList_Check(obj))
    return false;

  int n = PyList_Size(obj);

  out.clear();

  for (int i = 0; i < n - 1;) {
    PyObject *key   = PyList_GET_ITEM(obj, i++);
    PyObject *value = PyList_GET_ITEM(obj, i++);

    K k;
    if (!PConvFromPyObject(G, key, k))
      return false;

    if (!PConvFromPyObject(G, value, out[k]))
      return false;
  }

  return true;
}

/* ============================================================ */

#endif