cxtream  0.5.1
C++17 data pipeline with Python bindings.
converter.hpp
1 /****************************************************************************
2  * cxtream library
3  * Copyright (c) 2017, Cognexa Solutions s.r.o.
4  * Author(s) Filip Matzner
5  *
6  * This file is distributed under the MIT License.
7  * See the accompanying file LICENSE.txt for the complete license agreement.
8  ****************************************************************************/
9 
10 #ifndef CXTREAM_PYTHON_STREAM_CONVERTER_HPP
11 #define CXTREAM_PYTHON_STREAM_CONVERTER_HPP
12 
13 #include <cxtream/python/range.hpp>
14 #include <cxtream/python/utility/pyboost_column_converter.hpp>
15 
16 #include <range/v3/view/transform.hpp>
17 
18 namespace cxtream::python::stream {
19 
27 template<typename Rng>
28 auto to_python(Rng&& rng)
29 {
30  // by forwarding rng to view::transform, we make sure that it is
31  // not an lvalue of a container
32  auto range_of_dicts = std::forward<Rng>(rng)
33  // transform the range of columns to a range of python types
34  | ranges::view::transform([](auto&& tuple) {
35  return utility::columns_to_python(std::forward<decltype(tuple)>(tuple));
36  });
37 
38  // make python iterator out of the range of python types
39  using PyRng = decltype(range_of_dicts);
40  return range<PyRng>{std::move(range_of_dicts)};
41 }
42 
43 } // end namespace cxtream::python::stream
44 #endif
constexpr auto transform(from_t< FromColumns... > f, to_t< ToColumns... > t, Fun fun, dim_t< Dim > d=dim_t< 1 >{})
Transform a subset of cxtream columns to a different subset of cxtream columns.
Definition: transform.hpp:177
auto to_python(Rng &&rng)
Make a Python range from a stream (i.e, a range of tuples of cxtream columns).
Definition: converter.hpp:28
boost::python::dict columns_to_python(Tuple tuple)
Convert a tuple of cxtream columns into a Python dict.
Python adapter for C++ ranges.
Definition: range.hpp:59