File: DataSet.sip

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (328 lines) | stat: -rw-r--r-- 9,961 bytes parent folder | download
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
/**
 *
 * This file is part of Tulip (www.tulip-software.org)
 *
 * Authors: David Auber and the Tulip development Team
 * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
 *
 * Tulip is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * Tulip is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 */

// +-------------------------------------------------------------------------+
// | Tulip Python Bindings                                                   |
// | inspired from bindings by the Booggie project development team          |
// | (http://booggie.org/)                                                   |
// +-------------------------------------------------------------------------+

%ModuleHeaderCode
extern PyObject* convertTlpDataSetToPyDict(const tlp::DataSet &dataSet, PyObject *dict = NULL);
extern tlp::DataSet* convertPyDictToTlpDataSet(PyObject *dict, tlp::DataSet *refDataSet=NULL, const std::string &algoName = "");
extern bool setDataSetEntryFromPyObject(tlp::DataSet *dataSet, const std::string &entry, PyObject *value, tlp::DataType *refDataType = NULL, const std::string &algoName = "");
%End

%ModuleCode
PyObject* convertTlpDataSetToPyDict(const tlp::DataSet &dataSet, PyObject *dict) {
  if (!dict) dict = PyDict_New();
  std::pair<std::string, tlp::DataType*> entry;
  forEach(entry, dataSet.getValues()) {
    PyObject *key = sipConvertFromNewType(new std::string(entry.first), sipFindType("std::string"), NULL);
    PyObject *val = NULL;
    if (entry.second->getTypeName() == std::string(typeid(tlp::StringCollection).name())) {
      tlp::StringCollection *sc = reinterpret_cast<tlp::StringCollection*>(entry.second->value);
      val = sipConvertFromNewType(new std::string(sc->getCurrentString()), sipFindType("std::string"), NULL);
    } else {
      val = getPyObjectFromDataType(entry.second);
    }
    PyDict_SetItem(dict, key, val);
  }
  return dict;
}

extern tlp::DataSet* convertPyDictToTlpDataSet(PyObject *dict, tlp::DataSet *refDataSet, const std::string &algoName) {
  PyObject *key = NULL;
  PyObject *val = NULL;
  Py_ssize_t pos = 0;
  int state=0, err=0;
  tlp::DataSet *ret = new tlp::DataSet();
  if (refDataSet) {
    *ret = *refDataSet;
  }
  while (PyDict_Next(dict, &pos, &key, &val)) {
    std::string *keyStr = reinterpret_cast<std::string*>(sipConvertToType(key, sipFindType("std::string"), Py_None, SIP_NOT_NONE, &state, &err));
    tlp::DataType *dataType = NULL;
    if (refDataSet) {
      dataType = refDataSet->getData(*keyStr);
    }
    if (!setDataSetEntryFromPyObject(ret, *keyStr, val, dataType, algoName)) {
      delete ret;
      ret = NULL;
      break;
    }
  }

  return ret;
}

bool setDataSetEntryFromPyObject(tlp::DataSet *dataSet, const std::string &key, PyObject *value, tlp::DataType *refDataType, const std::string &algoName) {
  bool ret = true;
  tlp::DataType* dataType = NULL;
  if (refDataType) {
    dataType = refDataType;
  }
  if (dataType && dataType->getTypeName() == std::string(typeid(tlp::StringCollection).name()) &&
#if PY_MAJOR_VERSION >= 3
  PyUnicode_Check(value)) {
#else
  PyString_Check(value)) {
#endif
      tlp::StringCollection *sc = reinterpret_cast<tlp::StringCollection*>(dataType->value);
#if PY_MAJOR_VERSION >= 3
      std::string entry(convertPythonUnicodeObjectToStdString(value));
#else
      std::string entry(PyString_AS_STRING(value));
#endif

      std::vector<std::string> values = sc->getValues();
      if (std::find(values.begin(), values.end(), entry) != values.end()) {
          sc->setCurrent(entry);
          dataSet->set(key, *sc);
      } else {
          std::string msg = "Invalid value provided (\"" + entry + "\") to string parameter '" + key +
                            "' of algorithm '" + algoName + "'.\n";

          msg += "Possible values are : ";
          for (size_t i = 0 ; i < values.size() ; ++i) {
              replaceAll(values[i], "\n", "\\n");
              msg += values[i];
              if (i != values.size() -1) {
                  msg += ", ";
              }
          }
          PyErr_SetString(PyExc_Exception, msg.c_str());
          ret = false;
      }

  } else {
      ValueSetter valSetter(dataSet, key);
      if (!setCppValueFromPyObject(value, valSetter, dataType)) {
          std::string msg;
          if (!dataType) {
            msg = "Object of type ";
            msg += std::string(value->ob_type->tp_name);
            msg += " can not be stored in a Tulip dataset.";
          } else {
            msg = "Invalid type for the value provided to parameter '" + key + "' ";
            msg += "for algorithm '" + algoName + "'\n";
            msg += "Provided Python type is '" + std::string(value->ob_type->tp_name) + "'\n";
            msg += "Expected C++ type is '" + tlp::demangleClassName(dataType->getTypeName().c_str()) + "'";
          }
          PyErr_SetString(PyExc_Exception, msg.c_str());
          ret = false;
      }
  }
  delete dataType;
  return ret;
}

%End

namespace tlp {

struct DataSet {

%TypeCode
#include <tulip/PythonCppTypesConverter.h>
%End

%ConvertToTypeCode

  PyObject *key = NULL;
  PyObject *val = NULL;
  Py_ssize_t pos = 0;

  if (sipIsErr == NULL) {
    if (sipCanConvertToType(sipPy, sipFindType("tlp::DataSet"), SIP_NOT_NONE|SIP_NO_CONVERTORS)) {
      return 1;
    } else if (PyDict_Check(sipPy)) {
      while (PyDict_Next(sipPy, &pos, &key, &val)) {
        if (!sipCanConvertToType(key, sipFindType("std::string"), SIP_NOT_NONE)) {
          return 0;
        }
      }
      return 1;
    }
    return 0;
  }

  int state=0, err=0;

  if (PyDict_Check(sipPy)) {
    *sipCppPtr = convertPyDictToTlpDataSet(sipPy);
  } else {
    *sipCppPtr = new tlp::DataSet(*reinterpret_cast<tlp::DataSet*>(sipConvertToType(sipPy, sipFindType("tlp::DataSet"), Py_None, SIP_NOT_NONE|SIP_NO_CONVERTORS, &state, &err)));
  }

  return sipGetState(sipTransferObj);

%End

%ConvertFromTypeCode
  return convertTlpDataSetToPyDict(*sipCpp);
%End

%Docstring

.. deprecated:: 4.8
   The direct use of that class is now deprecated as it is now mapped to a Python dictionnary
   for commodity of use (see :ref:`Deprecation of direct use of the tlp.DataSet class <deprecatedDataSet>`).

This class is used to store a set of parameters to transmit to a Tulip algorithm.
Below is an exhaustive list of data types that can be stored in a Tulip data set:

  * boolean
  * integer
  * float
  * string
  * :class:`tlp.node`
  * :class:`tlp.edge`
  * :class:`tlp.Graph`
  * :class:`tlp.Coord`
  * :class:`tlp.Color`
  * :class:`tlp.Size`
  * :class:`tlp.DataSet`
  * :class:`tlp.ColorScale`
  * :class:`tlp.StringCollection`
  * :class:`tlp.BooleanProperty`
  * :class:`tlp.ColorProperty`
  * :class:`tlp.DoubleProperty`
  * :class:`tlp.IntegerProperty`
  * :class:`tlp.LayoutProperty`
  * :class:`tlp.SizeProperty`
  * :class:`tlp.StringProperty`
  * :class:`tlp.PropertyInterface`
  * list of those types

If you try to store an unsupported data type, an exception will be thrown.	
Each value stored in a data set is associated to a string key. Reading / Writing a value in 
a data set is done through the [] operator as illustrated below::

	# data set creation
	dataSet = tlp.DataSet()
	
	# writing integer values
	dataSet["height"] = 100
	dataSet["width"] = 300
	
	# reading integer values
	area = dataSet["height"] * dataSet["width"] 

When reading a data set value, a reference is returned not a copy.

%End

	DataSet();
	
	DataSet(const tlp::DataSet &set);

	bool exist(const std::string &str) const;
%Docstring
tlp.DataSet.exist(key)

Checks if the data set contains a value associated to a particular key.

:param key: the key to check
:type key: string
:rtype: boolean
%End

	void remove(const std::string &str);
%Docstring
tlp.DataSet.remove(key)

Remove the value associated to a particular key from the data set if it exists.

:param key: the key of the value to remove.
:type key: string
%End
	
	unsigned int size() const;
%Docstring
tlp.DataSet.size()

Returns the number of values stored in the data set.

:rtype: integer
%End
	
	std::vector<std::string> getKeys() const;
%Docstring
tlp.DataSet.getKeys()

Returns the list of keys associated to the values stored in the data set.

:rtype: list of string
%End	
	
%MethodCode
	std::vector<std::string> ret;
	tlp::Iterator< std::pair<std::string, tlp::DataType*> > *it = sipCpp->getValues();
	while (it->hasNext()) {
		ret.push_back(it->next().first);
	}
	delete it;
	sipRes = new std::vector<std::string>(ret);
%End	
	
	void __setitem__(const std::string &attributeName, SIP_PYOBJECT po);
%MethodCode
	sipIsErr = setDataSetEntryFromPyObject(sipCpp, *a0, a1) ? 0 : 1;
%End
	
	
	SIP_PYOBJECT __getitem__(const std::string &attributeName);
%MethodCode
    sipRes = NULL;
    tlp::Iterator< std::pair<std::string, tlp::DataType*> > *it = sipCpp->getValues();
    tlp::DataType* dataType = NULL;
    while (it->hasNext()) {
        std::pair<std::string, tlp::DataType*> p = it->next();
        if (p.first == *a0) {
            dataType = p.second;
        }
    }
    delete it;
	
    sipRes = getPyObjectFromDataType(dataType, true);
	
    if (!sipRes) {
        std::ostringstream oss;
        oss << "Dataset entry \"" << *a0 << "\" does not exist.";
        sipIsErr = 1;
        PyErr_SetString(PyExc_AttributeError, oss.str().c_str());
    }
%End	

    SIP_PYOBJECT toDict();
%Docstring
tlp.DataSet.toDict()

Converts that data set to a Python dictionnary.

:rtype: a dictionnary reflecting the data set content
%End
%MethodCode
    sipRes = convertTlpDataSetToPyDict(*sipCpp);
%End

};
};