itom
|
Conversion class with mainly static methods, which convert values between Qt and standard C++ data types and PyObject* values. More...
Public Types | |
enum | UnicodeEncodings { utf_8 , latin_1 , iso_8859_1 , mbcs , ascii , utf_16 , utf_16_LE , utf_16_BE , utf_32 , utf_32_BE , utf_32_LE , other } |
Static Public Member Functions | |
static PyObject * | QStringToPyObject (const QString &str) |
converts QString to Python string (unicode!) | |
static PyObject * | QStringListToPyObject (const QStringList &list) |
converts QStringList to Python tuple | |
static PyObject * | QStringListToPyList (const QStringList &list) |
converts QStringList to Python list | |
static PyObject * | QDateToPyDate (const QDate &date) |
converts QDate to Python datetime.date object | |
static PyObject * | QDateTimeToPyDateTime (const QDateTime &datetime) |
converts QDateTime to Python datetime.datetime object | |
static PyObject * | QTimeToPyTime (const QTime &time) |
converts QTime to Python datetime.time object | |
static QString | PyObjGetRepresentation (PyObject *val) |
get string representation of py object | |
static QString | PyObjGetString (PyObject *val) |
get string value from py object | |
static QString | PyObjGetString (PyObject *val, bool strict, bool &ok) |
get string value from py object | |
static std::string | PyObjGetStdStringAsLatin1 (PyObject *val, bool strict, bool &ok) |
get std::string value from py object. std::string has latin1 encoding | |
static QByteArray | PyObjGetBytes (PyObject *val, bool strict, bool &ok) |
get bytes from py object | |
static QSharedPointer< char > | PyObjGetBytesShared (PyObject *val, bool strict, bool &ok) |
get bytes from py object | |
static int | PyObjGetInt (PyObject *val, bool strict, bool &ok) |
get int from py object | |
static unsigned int | PyObjGetUInt (PyObject *val, bool strict, bool &ok) |
get unsigned int from py object | |
static qint64 | PyObjGetLongLong (PyObject *val, bool strict, bool &ok) |
get int64 from py object | |
static quint64 | PyObjGetULongLong (PyObject *val, bool strict, bool &ok) |
get int64 from py object | |
static double | PyObjGetDouble (PyObject *val, bool strict, bool &ok) |
get double from py object | |
static QVector< double > | PyObjGetDoubleArray (PyObject *val, bool strict, bool &ok) |
get double-array from py object | |
static complex128 | PyObjGetComplex (PyObject *val, bool strict, bool &ok) |
get complex from py object | |
static QVector< complex128 > | PyObjGetComplexArray (PyObject *val, bool strict, bool &ok) |
get complex-array from py object | |
static QVector< int > | PyObjGetIntArray (PyObject *val, bool strict, bool &ok) |
get int-array from py object | |
static bool | PyObjGetBool (PyObject *val, bool strict, bool &ok) |
get bool from py object | |
static QVector< ito::Shape > | PyObjGetShapeVector (PyObject *val, bool &ok) |
get shape vector from py object | |
static QVector< ito::ByteArray > | PyObjGetByteArrayList (PyObject *val, bool strict, bool &ok) |
get ito::ByteArray list from py object | |
static ito::DataObject * | PyObjGetDataObjectNewPtr (PyObject *val, bool strict, bool &ok, ito::RetVal *retVal=nullptr, bool addNumpyOrgTags=false) |
static QSharedPointer< ito::DataObject > | PyObjGetSharedDataObject (PyObject *val, bool strict, bool &ok, ito::RetVal *retVal=nullptr) |
static QStringList | PyObjToStringList (PyObject *val, bool strict, bool &ok) |
create a string list from python sequence | |
static QVariant | PyObjToQVariant (PyObject *val, int type=-1) |
conversion from PyObject* to QVariant | |
static bool | PyObjToVoidPtr (PyObject *val, void **retPtr, int *retType, int type=-1, bool strict=false) |
tries to convert PyObject* to known data type and returns deep-copy of the value, given as char* | |
static PyObject * | GetPyBool (bool val) |
convert QVariant from PyObject | |
static PyObject * | QVariantToPyObject (const QVariant &v) |
conversion from given QVariant to appropriate PyObject* | |
static PyObject * | QVariantMapToPyObject (const QVariantMap &m) |
conversion from given QVariantMap to python-dictionary. | |
static PyObject * | QVariantListToPyObject (const QVariantList &l) |
conversion from given QVariantList to python-tuple. | |
static PyObject * | DataObjectToPyObject (const ito::DataObject &dObj) |
static PyObject * | AddInBaseToPyObject (ito::AddInBase *aib) |
static PyObject * | ConvertQtValueToPythonInternal (int type, const void *data) |
method internally used for conversion from given type-id (QMetaType) and corresponding char*-pointer to PyObject* | |
static PyObject * | QByteArrayToPyUnicode (const QByteArray &ba, const char *errors="replace") |
static PyObject * | QByteArrayToPyUnicodeSecure (const QByteArray &ba, const char *errors="replace") |
static PyObject * | QByteArrayUtf8ToPyUnicode (const QByteArray &ba, const char *errors="replace") |
static PyObject * | QByteArrayUtf8ToPyUnicodeSecure (const QByteArray &ba, const char *errors="replace") |
static PyObject * | ByteArrayToPyUnicode (const char *byteArray, Py_ssize_t len=0, const char *errors="replace") |
static PyObject * | PyUnicodeToPyByteObject (PyObject *unicode, const char *errors="replace") |
Static Private Member Functions | |
static int | guessQMetaTypeFromPyObject (PyObject *val) |
static void | baseObjectDeleterDataObject (ito::DataObject *sharedObject) |
Static Private Attributes | |
static UnicodeEncodings | textEncoding = PythonQtConversion::latin_1 |
static QByteArray | textEncodingName = "latin_1" |
static QHash< char *, PyObject * > | m_pyBaseObjectStorage = QHash<char*, PyObject*>() |
Friends | |
class | PythonEngine |
Conversion class with mainly static methods, which convert values between Qt and standard C++ data types and PyObject* values.
Parts of this class are taken from the project PythonQt (http://pythonqt.sourceforge.net/)
|
static |
method internally used for conversion from given type-id (QMetaType) and corresponding char*-pointer to PyObject*
This method is the opposite from PyObjToVoidPtr and converts a pair given by type-id (see QMetaType) and corresponding char*-pointer, which to the variable's content to the appropriate python type.
A python error is returned if conversion failed.
type | is given type-id (QMetaType) |
data | is the content, casted to char* |
|
static |
convert QVariant from PyObject
conversion from bool to python boolean type
Returns new reference to Py_True or Py_False depending on input val.
val | is the given boolean input |
|
static |
get bool from py object
conversion from PyObject* to bool
tries to convert the given PyObject* val as boolean variable and returns its value. If val is no boolean object the output depends on the param strict. If strict==false, the output is true, if the integer conversion (strict==false) of val is unequal to zero.
val | is the given python object |
strict | indicates if only real boolean types should be converted |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get ito::ByteArray list from py object
conversion from PyObject* to vector of ito::ByteArray
tries to interpret given PyObject* as list of strings and converts it to QVector<ito::ByteArray>. If strict is true, we do not want to convert a string to a stringlist, since single strings in python are also detected to be sequences.
Strings are converted to the byte array using the latin1 coded, if possible.
val | is the given python object |
strict | indicates if any object fitting to the sequence protocol is interpreted as string list, too [true] |
ok | (ByRef) is set to true if conversion was successful, else false |
|
static |
get bytes from py object
conversion from PyObject* to QString
If val is a byte-object, it is directly converted into a QString. If val is an unicode-object, its value is converted using the current encoding and returned. In any other case the string-like-representation is only returned if strict is set to false.
val | is the given python object |
strict | indicates if only real byte or unicode objects can be converted to string |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get double from py object
conversion from PyObject* to double
If val is a floating-point object, it is directly converted into a double variable. Otherwise, the output depends on strict. If strict is equal to false, any fixed-point object is interpreted as double and the result is returned. Additionally boolean variables are returned as 0.0 (false) or 1.0 (true) if strict is false, too.
val | is the given python object |
strict | indicates if only real floating point numbers should be converted |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get int from py object
conversion from PyObject* to integer
If val is a fixed-point object, it is directly converted into an integer variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.
val | is the given python object |
strict | indicates if only real integer or long types should be converted |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get int64 from py object
conversion from PyObject* to long long (64bit, if possible)
If val is a fixed-point object, it is directly converted into an qint64 variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.
val | is the given python object |
strict | indicates if only real integer or long types should be converted |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get string representation of py object
string representation of PyObject*
returns a string or a string-like interpretation of the given PyObject*. If this object is no byte- or unicode-object, its string representation is returned, obtained by calling the tp_repr-slot of the corresponding Type-struct.
val | is the given python object |
|
static |
get shape vector from py object
get shape vector from pyObj
|
static |
converts the python object to a DataObject. If the returned value is destroyed, possible base objects will be safely removed, too. If strict is true, the returned DataObject is exactly the same object, than wrapped by the given python object. Else, numpy-like arrays can also be accepted and will be converted (shallow or deep copy, whatever is possible) to a dataObject.
|
static |
get std::string value from py object. std::string has latin1 encoding
conversion from PyObject* to std::string
If val is a byte-object, it is directly converted into a std::string. If val is an unicode-object, its value is converted using the current encoding and returned. In any other case the string-like-representation is only returned if strict is set to false.
val | is the given python object |
strict | indicates if only real byte or unicode objects can be converted to string |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get string value from py object
conversion from PyObject* to QString
If val is a byte-object, it is directly converted into a QString. If val is an unicode-object, its value is converted using the current encoding and returned. In any other case the string-like-representation is only returned if strict is set to false.
val | is the given python object |
strict | indicates if only real byte or unicode objects can be converted to string |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get unsigned int from py object
conversion from PyObject* to unsigned integer
If val is a fixed-point object, it is directly converted into an integer variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.
val | is the given python object |
strict | indicates if only real integer or long types should be converted |
ok | (ByRef) is set to true if conversion succeeded. |
|
static |
get int64 from py object
conversion from PyObject* to unsigned integer 64bit
If val is a fixed-point object, it is directly converted into an quint64 variable. Otherwise, the output depends on strict. If strict is equal to false, any floating point object is rounded using floor and the result is returned. Additionally boolean variables are returned as 0 (false) or 1 (true) if strict is false, too.
val | is the given python object |
strict | indicates if only real integer or long types should be converted |
ok | (ByRef) is set to true if conversion succeeded. Conversion fails if number is smaller than zero. |
|
static |
conversion from PyObject* to QVariant
convert python object to qvariant, if type is given it will try to create a qvariant of that type, otherwise it will guess from the python type
methods tries to convert PyObject* to QVariant. Type indicates the desired type of QVariant, given by the type-number of QMetaType. If type==-1, the right type is guessed by checking the PyObject*-type. If conversion failed, QVariant() is returned.
val | is the given python object |
type | is the desired Qt-type (with respect to QMetaType) or -1, if resulting type should be guessed by input |
|
static |
create a string list from python sequence
conversion from PyObject* to QStringList
tries to interpret given PyObject* as list of strings and converts it to QStringList. If strict is true, we do not want to convert a string to a stringlist, since single strings in python are also detected to be sequences.
val | is the given python object |
strict | indicates if any object fitting to the sequence protocol is interpreted as string list, too [true] |
ok | (ByRef) is set to true if conversion was successful, else false |
|
static |
tries to convert PyObject* to known data type and returns deep-copy of the value, given as char*
convert python object to char* using QMetaType. if type is given it will try to create a char* of that type, otherwise it will guess from the python type. If fails, NULL is returned
methods tries to convert PyObject* to QVariant. Type indicates the desired type of QVariant, given by the type-number of QMetaType. If type==-1, the right type is guessed by checking the PyObject*-type. If conversion failed, QVariant() is returned.
The deep-copy is created using QMetaType::create(...)
val | is the given python object |
retPtr | is the resulting pointer to type char*, containing the converted deep copy or NULL, if error |
retType | is an integer-pointer, containing the type-id of the converted result (with respect to QMetaType) or -1, if failure |
type | is the desired QMetaType-type-id or -1, if conversion-type should be guessed considering the type of val. |
strict | decides whether the given PyObject should strictly be converted into the C-type or a cast like int->string is allowed, too. |
|
static |
converts QStringList to Python list
conversion from given QStringList to list of python-unicode objects
returns new reference to list of Python-unicode objects, containing the content of given QStringList. Every single string is converted using QStringToPyObject.
list | is reference to QStringList |
|
static |
converts QStringList to Python tuple
conversion from given QStringList to tuple of python-unicode objects
returns new reference to tuple of Python-unicode objects, containing the content of given QStringList. Every single string is converted using QStringToPyObject.
list | is reference to QStringList |
|
static |
converts QString to Python string (unicode!)
conversion from given QString to PyObject*
returns new reference to Python-unicode object, containing the content of given QString. If str is empty or null, the resulting string is empty string ("")
str | is reference to QString |
|
static |
conversion from given QVariantList to python-tuple.
returns new reference to python-tuple type. Each item of QVariantList is one element in tuple. The values are converted using QVariantToPyObject.
m | is reference to QVariantMap |
|
static |
conversion from given QVariantMap to python-dictionary.
returns new reference to python-dictionary type. Each key of QVariantMap is one element in dictionary. The values are converted using QVariantToPyObject.
m | is reference to QVariantMap |
|
static |
conversion from given QVariant to appropriate PyObject*
returns new reference to PyObject*, which contains the conversion from given QVariant-variable.
v | is reference to QVariant |
|
staticprivate |
if any PyObject is converted into a QVariant-object, dataObject or any other class, and if this PyObject has a base-pointer unequal to None, this base pointer is incremented during the lifetime of the dataObject, passed to QVariant. If this dataObject is destroyed, the baseObjectDeleterDataObject deleter method is called and decrements the base PyObject.
Be careful: For decrementing the refcount, the GIL must be hold by this deleter!