Add dtype_of<T>() function, update the tests

This commit is contained in:
Ivan Smirnov 2016-06-22 01:07:20 +01:00
parent 036e8cd32f
commit 4f164217e4
4 changed files with 39 additions and 20 deletions

View File

@ -15,14 +15,14 @@
namespace py = pybind11; namespace py = pybind11;
struct Struct { struct SimpleStruct {
bool x; bool x;
uint32_t y; uint32_t y;
float z; float z;
}; };
std::ostream& operator<<(std::ostream& os, const Struct& v) { std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) {
return os << v.x << "," << v.y << "," << v.z; return os << "s:" << v.x << "," << v.y << "," << v.z;
} }
struct PackedStruct { struct PackedStruct {
@ -32,16 +32,16 @@ struct PackedStruct {
} __attribute__((packed)); } __attribute__((packed));
std::ostream& operator<<(std::ostream& os, const PackedStruct& v) { 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 NestedStruct {
Struct a; SimpleStruct a;
PackedStruct b; PackedStruct b;
} __attribute__((packed)); } __attribute__((packed));
std::ostream& operator<<(std::ostream& os, const NestedStruct& v) { 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 <typename T> template <typename T>
@ -80,21 +80,31 @@ void print_recarray(py::array_t<S> arr) {
} }
void print_format_descriptors() { void print_format_descriptors() {
std::cout << py::format_descriptor<Struct>::value() << std::endl; std::cout << py::format_descriptor<SimpleStruct>::value() << std::endl;
std::cout << py::format_descriptor<PackedStruct>::value() << std::endl; std::cout << py::format_descriptor<PackedStruct>::value() << std::endl;
std::cout << py::format_descriptor<NestedStruct>::value() << std::endl; std::cout << py::format_descriptor<NestedStruct>::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<SimpleStruct>()) << std::endl;
std::cout << to_str(py::dtype_of<PackedStruct>()) << std::endl;
std::cout << to_str(py::dtype_of<NestedStruct>()) << std::endl;
}
void init_ex20(py::module &m) { 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(PackedStruct, x, y, z);
PYBIND11_DTYPE(NestedStruct, a, b); PYBIND11_DTYPE(NestedStruct, a, b);
m.def("create_rec_simple", &create_recarray<Struct>); m.def("create_rec_simple", &create_recarray<SimpleStruct>);
m.def("create_rec_packed", &create_recarray<PackedStruct>); m.def("create_rec_packed", &create_recarray<PackedStruct>);
m.def("create_rec_nested", &create_nested); m.def("create_rec_nested", &create_nested);
m.def("print_format_descriptors", &print_format_descriptors); m.def("print_format_descriptors", &print_format_descriptors);
m.def("print_rec_simple", &print_recarray<Struct>); m.def("print_rec_simple", &print_recarray<SimpleStruct>);
m.def("print_rec_packed", &print_recarray<PackedStruct>); m.def("print_rec_packed", &print_recarray<PackedStruct>);
m.def("print_rec_nested", &print_recarray<NestedStruct>); m.def("print_rec_nested", &print_recarray<NestedStruct>);
m.def("print_dtypes", &print_dtypes);
} }

View File

@ -4,7 +4,7 @@ from __future__ import print_function
import numpy as np import numpy as np
from example import ( from example import (
create_rec_simple, create_rec_packed, create_rec_nested, print_format_descriptors, 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)) np.testing.assert_equal(arr, np.array(data, dtype=dtype))
print_format_descriptors() print_format_descriptors()
print_dtypes()
simple_dtype = np.dtype({'names': ['x', 'y', 'z'], simple_dtype = np.dtype({'names': ['x', 'y', 'z'],
'formats': ['?', 'u4', 'f4'], 'formats': ['?', 'u4', 'f4'],

View File

@ -1,12 +1,15 @@
T{?:x:xxxI:y:f:z:} T{?:x:xxxI:y:f:z:}
T{?:x:=I:y:f:z:} T{?:x:=I:y:f:z:}
T{T{?:x:xxxI:y:f:z:}:a:T{?:x:=I:y:f:z:}:b:} T{T{?:x:xxxI:y:f:z:}:a:T{?:x:=I:y:f:z:}:b:}
0,0,0 {'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':12}
1,1,1.5 [('x', '?'), ('y', '<u4'), ('z', '<f4')]
0,2,3 [('a', {'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':12}), ('b', [('x', '?'), ('y', '<u4'), ('z', '<f4')])]
0,0,0 s:0,0,0
1,1,1.5 s:1,1,1.5
0,2,3 s:0,2,3
0,0,0|1,1,1.5 p:0,0,0
1,1,1.5|0,2,3 p:1,1,1.5
0,2,3|1,3,4.5 p:0,2,3
n:a=s:0,0,0;b=p:1,1,1.5
n:a=s:1,1,1.5;b=p:0,2,3
n:a=s:0,2,3;b=p:1,3,4.5

View File

@ -169,6 +169,11 @@ template <typename T> struct format_descriptor
} }
}; };
template <typename T>
object dtype_of() {
return detail::npy_format_descriptor<T>::descr();
}
NAMESPACE_BEGIN(detail) NAMESPACE_BEGIN(detail)
template <typename T> struct npy_format_descriptor<T, typename std::enable_if<std::is_integral<T>::value>::type> { template <typename T> struct npy_format_descriptor<T, typename std::enable_if<std::is_integral<T>::value>::type> {