|
Boost.PythonHeader <boost/python/slice.hpp> |
slice
slice
synopsisslice
constructorsslice
observer functionsExposes a TypeWrapper for the Python slice type.
slice
Exposes the extended slicing protocol by wrapping the built-in slice
type. The semantics of the constructors and member functions defined
below can be fully understood by reading the TypeWrapper concept
definition. Since slice
is publicly derived from object
, the public object
interface applies to slice
instances as well.
slice
synopsisnamespace boost { namespace python { class slice : public object { public: slice(); // create an empty slice, equivalent to [::] template <typename Int1, typename Int2> slice(Int1 start, Int2 stop); template <typename Int1, typename Int2, typename Int3> slice(Int1 start, Int2 stop, Int3 step); // Access the parameters this slice was created with. object start(); object stop(); object step(); // The return type of slice::get_indices() template <typename RandomAccessIterator> struct range { RandomAccessIterator start; RandomAccessIterator stop; int step; }; template <typename RandomAccessIterator> range<RandomAccessIterator> get_indices( RandomAccessIterator const& begin, RandomAccessIterator const& end); }; }}
slice
constructorsslice();
slice
with default stop, start, and
step values. Equivalent to the slice object created as part of the Python
expression base[::].
template <typename Int1, typename Int2> slice(Int1 start, Int2 stop);
start
, stop
, and step
are of type slice_nil
or convertible to type object
.slice(start,stop)
,
or as part of the Python expression base[start:stop]
.error_already_set
and sets a Python TypeError
exception if no conversion is possible from the arguments to type object
.template <typename Int1, typename Int2, typename Int3> slice(Int1 start, Int2 stop, Int3 step);
start
, stop
, and step
are slice_nil
or convertible to type object
.slice(start,stop,step)
,
or as part of the Python expression base[start:stop:step]
.error_already_set
and sets a Python TypeError
exception if no conversion is possible from the arguments to type
object.slice
observer functionsobject slice::start() const; object slice::stop() const; object slice::step() const;
template <typename RandomAccessIterator> slice::range<RandomAccessIterator> slice::get_indices( RandomAccessIterator const& begin, RandomAccessIterator const& end) const;
TypeError
exception if any of this slice's arguments
are neither references to PyNone
nor convertible to int
. Throws
std::invalid_argument
if the resulting range would be empty. You
should always wrap calls to slice::get_indices()
within try { ...; } catch (std::invalid_argument) {}
to
handle this case and take appropriate action.using namespace boost::python; // Perform an extended slice of a Python list. // Warning: extended slicing was not supported for built-in types prior // to Python 2.3 list odd_elements(list l) { return l[slice(_,_,2)]; } // Perform a multidimensional extended slice of a Numeric.array numeric::array even_columns(numeric::array arr) { // select every other column, starting with the second, of a 2-D array. // Equivalent to "return arr[:, 1::2]" in Python. return arr[make_tuple( slice(), slice(1,_,2))]; } // Perform a summation over a slice of a std::vector. double partial_sum(std::vector<double> const& Foo, const slice index) { slice::range<std::vector<double>::const_iterator> bounds; try { bounds = index.get_indices<>(Foo.begin(), Foo.end()); } catch (std::invalid_argument) { return 0.0; } double sum = 0.0; while (bounds.start != bounds.stop) { sum += *bounds.start; std::advance( bounds.start, bounds.step); } sum += *bounds.start; return sum; }
Revised 07 Febuary, 2004
© Copyright Jonathan Brandmeyer,
2004. Modification, copying and redistribution of this document
is permitted under the terms and conditions of the Boost Software
License, version 1.0.