[PyKDE] Request for more SIP features
Gerard Vermeulen
gerard.vermeulen at grenoble.cnrs.fr
Fri Nov 26 06:18:56 GMT 2004
Easy ones first:
(1) an uninstall target in the Makefile to undo the work of the install target
(2) the enums wrapped by SIP are mutable, but Python has the facility to define real constants:
>>> import termios
>>> print termios.B110
3
>>> termios.B110 = 0
>>> print termios.B110
0
>>>
(3) support for iterators. Motivation: I started to experiment with SIP template files
to wrap the STL. Of course, an iterator is not needed to traverse a std::vector, but
a Python iterator can remember the C++ pointer into a more advanced container while
returning a Python object. A naive approach does not work:
SIP file snippet:
class CellIterator
{
%TypeHeaderCode
#include <vector>
#include <sipIterCell.h>
class CellIterator
{
public:
CellIterator(Cell &cell): it(cell.begin()) {};
CellIterator * __iter__() { return this; }
int next() { return *it++; }
private:
Cell::iterator it;
};
%End
public:
CellIterator(Cell &);
int next();
CellIterator * __iter__();
};
Python interpreter (this would work for a Python class which implements __iter__() and next():
>>> import Iter
>>> c = Iter.Cell()
>>> c.push_back(0)
>>> c.push_back(1)
>>> for i in c: print i
...
0
1
>>> it = Iter.CellIterator(c)
>>> dir(it)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'next']
>>> for i in it: print i
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence
>>>
Gerard
More information about the PyQt
mailing list