From 4f164217e48ae1dbce5cfb896195c7b56521fc2a Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Wed, 22 Jun 2016 01:07:20 +0100 Subject: [PATCH] Add dtype_of() function, update the tests --- example/example20.cpp | 30 ++++++++++++++++++++---------- example/example20.py | 3 ++- example/example20.ref | 21 ++++++++++++--------- include/pybind11/numpy.h | 5 +++++ 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/example/example20.cpp b/example/example20.cpp index 1d4088fd3..201c470e0 100644 --- a/example/example20.cpp +++ b/example/example20.cpp @@ -15,14 +15,14 @@ namespace py = pybind11; -struct Struct { +struct SimpleStruct { bool x; uint32_t y; float z; }; -std::ostream& operator<<(std::ostream& os, const Struct& v) { - return os << v.x << "," << v.y << "," << v.z; +std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) { + return os << "s:" << v.x << "," << v.y << "," << v.z; } struct PackedStruct { @@ -32,16 +32,16 @@ struct PackedStruct { } __attribute__((packed)); std::ostream& operator<<(std::ostream& os, const PackedStruct& v) { - return os << v.x << "," << v.y << "," << v.z; + return os << "p:" << v.x << "," << v.y << "," << v.z; } struct NestedStruct { - Struct a; + SimpleStruct a; PackedStruct b; } __attribute__((packed)); std::ostream& operator<<(std::ostream& os, const NestedStruct& v) { - return os << v.a << "|" << v.b; + return os << "n:a=" << v.a << ";b=" << v.b; } template @@ -80,21 +80,31 @@ void print_recarray(py::array_t arr) { } void print_format_descriptors() { - std::cout << py::format_descriptor::value() << std::endl; + std::cout << py::format_descriptor::value() << std::endl; std::cout << py::format_descriptor::value() << std::endl; std::cout << py::format_descriptor::value() << std::endl; } +void print_dtypes() { + auto to_str = [](py::object obj) { + return (std::string) (py::str) ((py::object) obj.attr("__str__"))(); + }; + std::cout << to_str(py::dtype_of()) << std::endl; + std::cout << to_str(py::dtype_of()) << std::endl; + std::cout << to_str(py::dtype_of()) << std::endl; +} + void init_ex20(py::module &m) { - PYBIND11_DTYPE(Struct, x, y, z); + PYBIND11_DTYPE(SimpleStruct, x, y, z); PYBIND11_DTYPE(PackedStruct, x, y, z); PYBIND11_DTYPE(NestedStruct, a, b); - m.def("create_rec_simple", &create_recarray); + m.def("create_rec_simple", &create_recarray); m.def("create_rec_packed", &create_recarray); m.def("create_rec_nested", &create_nested); m.def("print_format_descriptors", &print_format_descriptors); - m.def("print_rec_simple", &print_recarray); + m.def("print_rec_simple", &print_recarray); m.def("print_rec_packed", &print_recarray); m.def("print_rec_nested", &print_recarray); + m.def("print_dtypes", &print_dtypes); } diff --git a/example/example20.py b/example/example20.py index 7dd60e346..c581cf3f7 100644 --- a/example/example20.py +++ b/example/example20.py @@ -4,7 +4,7 @@ from __future__ import print_function import numpy as np from example import ( create_rec_simple, create_rec_packed, create_rec_nested, print_format_descriptors, - print_rec_simple, print_rec_packed, print_rec_nested + print_rec_simple, print_rec_packed, print_rec_nested, print_dtypes ) @@ -12,6 +12,7 @@ def check_eq(arr, data, dtype): np.testing.assert_equal(arr, np.array(data, dtype=dtype)) print_format_descriptors() +print_dtypes() simple_dtype = np.dtype({'names': ['x', 'y', 'z'], 'formats': ['?', 'u4', 'f4'], diff --git a/example/example20.ref b/example/example20.ref index 315a82a00..32e2b4b7a 100644 --- a/example/example20.ref +++ b/example/example20.ref @@ -1,12 +1,15 @@ T{?:x:xxxI:y:f:z:} T{?:x:=I:y:f:z:} T{T{?:x:xxxI:y:f:z:}:a:T{?:x:=I:y:f:z:}:b:} -0,0,0 -1,1,1.5 -0,2,3 -0,0,0 -1,1,1.5 -0,2,3 -0,0,0|1,1,1.5 -1,1,1.5|0,2,3 -0,2,3|1,3,4.5 +{'names':['x','y','z'], 'formats':['?',' struct format_descriptor } }; +template +object dtype_of() { + return detail::npy_format_descriptor::descr(); +} + NAMESPACE_BEGIN(detail) template struct npy_format_descriptor::value>::type> {