Slices when extending python with C++
Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- Tradiční i moderní adventní a novoroční zvyky, sváteční jídlo a pití, výzdoba a dárky... - čtěte vánoční a silvestrovský speciál portálu VOLNÝ.cz na http://web.volny.cz/data/click.php?id=1301 -- http://mail.python.org/mailman/listinfo/python-list
Slices when extending python with C++
Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- Tradiční i moderní adventní a novoroční zvyky, sváteční jídlo a pití, výzdoba a dárky... - čtěte vánoční a silvestrovský speciál portálu VOLNÝ.cz na http://web.volny.cz/data/click.php?id=1301 Hallo,I have kind of special question when extening python with C++ implemented modules.I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y]It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regardsDan T. -- http://mail.python.org/mailman/listinfo/python-list
[no subject]
Hallo, I have kind of special question when extening python with C++ implemented modules. I try to implement a class, behaving also like an array. And I need to implement slice-getters. I implemented PySequenceMethods.sq_slice to get "simple" slices like: myobj[x:y] It works perfectly, without problems. But now I have a problem when need to access the array with "special" slices, like: myobj[x:y:step] myobj[::-1] # to revert the array I would like to ask which method must be implemented to get this "special" slice getters. The "simple" slice PySequenceMethods.sq_slice getter accepts only two indexes - from, to, but not a step (which would solve the issue). Thank you very much for your ansver, best regards Dan T. -- http://mail.python.org/mailman/listinfo/python-list
Re: Slices when extending python with C++
Dear Robert, thank you very much for your answer. I understand what you mean and I have looked at slice object and C-api methods it provides. It should be easy to implement it. The only question is how exactly yo implement the general getter, since sq_item you mention (assume you mean PySequenceMethods.sq_item) has the following signature: PyObject * (* ssizeargfunc)(PyObject *, Py_ssize_t) accepting Py_ssize_t as the index, not a PyObject * which would hold the slice. So, how exactly to implement the getter? As a general method named __getitem__ registered in PyMethodDef? Or in another way? Thank you very much again. Regards, Dan - PŮVODNÍ ZPRÁVA - Od: "Robert Kern" Komu: [email protected] Předmět: Re: Slices when extending python with C++ Datum: 28.12.2011 - 10:24:42 > On 12/27/11 11:02 PM, [email protected] wrote: > > Hallo, > > I have kind of special question when extening > > python with C++ > > > implemented modules. > > > > I try to implement a class, behaving also like > > an array. And I need > > > to implement slice-getters. I implemented > > PySequenceMethods.sq_slice > > > to get "simple" slices like: > > > > myobj[x:y] > > > > It works perfectly, without problems. But now I > > have a problem when > > > need to access the array with "special" slices, > > like: > > > > > myobj[x:y:step] > > myobj[::-1] # to revert the array > > > > I would like to ask which method must be > > implemented to get this > > > "special" slice getters. The "simple" slice > > PySequenceMethods.sq_slice getter accepts only > > two indexes - from, > > > to, but not a step (which would solve the > > issue). > > > The sq_slice slot is deprecated, just like the > __getslice__() method on the > Python side. Instead, implement sq_item to accept > a slice object in addition to > integer indices. > > http://docs.python.org/c-api/slice.html > > -- > Robert Kern > > "I have come to believe that the whole world is an > enigma, a harmless enigma > that is made terrible by our own mad attempt to > interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Tradiční i moderní adventní a novoroční zvyky, sváteční jídlo a pití, výzdoba a dárky... - čtěte vánoční a silvestrovský speciál portálu VOLNÝ.cz na http://web.volny.cz/data/click.php?id=1301 -- http://mail.python.org/mailman/listinfo/python-list
Re: Slices when extending python with C++
Great, it's working! Thank you very much, Robert! Dan T. - PŮVODNÍ ZPRÁVA - Od: "Robert Kern" Komu: [email protected] Předmět: Re: Slices when extending python with C++ Datum: 28.12.2011 - 14:18:36 > On 12/28/11 1:01 PM, [email protected] wrote: > > Dear Robert, > > > > thank you very much for your answer. I > > understand what you mean and > > > I have looked at slice object and C-api methods > > it provides. It > > > should be easy to implement it. > > > > The only question is how exactly yo implement > > the general getter, > > > since sq_item you mention (assume you mean > > PySequenceMethods.sq_item) has the following > > signature: > > > > > PyObject * (* ssizeargfunc)(PyObject *, > > Py_ssize_t) > > > > > accepting Py_ssize_t as the index, not a > > PyObject * which would hold > > > the slice. > > > > So, how exactly to implement the getter? As a > > general method named > > > __getitem__ registered in PyMethodDef? Or in > > another way? > > > Sorry, PyMappingMethods.mp_subscript is the > general function that you need to > implement. > > -- > Robert Kern > > "I have come to believe that the whole world is an > enigma, a harmless enigma > that is made terrible by our own mad attempt to > interpret it as though it had > an underlying truth." > -- Umberto Eco > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Tradiční i moderní adventní a novoroční zvyky, sváteční jídlo a pití, výzdoba a dárky... - čtěte vánoční a silvestrovský speciál portálu VOLNÝ.cz na http://web.volny.cz/data/click.php?id=1301 -- http://mail.python.org/mailman/listinfo/python-list
