Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
4f164217
"tests/test_eigen_matrix.cpp" did not exist on "391c75447da13ef44cc2e119e005c0aeee442862"
Commit
4f164217
authored
Jun 22, 2016
by
Ivan Smirnov
Browse files
Add dtype_of<T>() function, update the tests
parent
036e8cd3
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
20 deletions
+39
-20
example/example20.cpp
example/example20.cpp
+20
-10
example/example20.py
example/example20.py
+2
-1
example/example20.ref
example/example20.ref
+12
-9
include/pybind11/numpy.h
include/pybind11/numpy.h
+5
-0
No files found.
example/example20.cpp
View file @
4f164217
...
...
@@ -15,14 +15,14 @@
namespace
py
=
pybind11
;
struct
Struct
{
struct
Simple
Struct
{
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
Simple
Struct
&
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
;
Simple
Struct
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
<
typename
T
>
...
...
@@ -80,21 +80,31 @@ void print_recarray(py::array_t<S> arr) {
}
void
print_format_descriptors
()
{
std
::
cout
<<
py
::
format_descriptor
<
Struct
>::
value
()
<<
std
::
endl
;
std
::
cout
<<
py
::
format_descriptor
<
Simple
Struct
>::
value
()
<<
std
::
endl
;
std
::
cout
<<
py
::
format_descriptor
<
PackedStruct
>::
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
)
{
PYBIND11_DTYPE
(
Struct
,
x
,
y
,
z
);
PYBIND11_DTYPE
(
Simple
Struct
,
x
,
y
,
z
);
PYBIND11_DTYPE
(
PackedStruct
,
x
,
y
,
z
);
PYBIND11_DTYPE
(
NestedStruct
,
a
,
b
);
m
.
def
(
"create_rec_simple"
,
&
create_recarray
<
Struct
>
);
m
.
def
(
"create_rec_simple"
,
&
create_recarray
<
Simple
Struct
>
);
m
.
def
(
"create_rec_packed"
,
&
create_recarray
<
PackedStruct
>
);
m
.
def
(
"create_rec_nested"
,
&
create_nested
);
m
.
def
(
"print_format_descriptors"
,
&
print_format_descriptors
);
m
.
def
(
"print_rec_simple"
,
&
print_recarray
<
Struct
>
);
m
.
def
(
"print_rec_simple"
,
&
print_recarray
<
Simple
Struct
>
);
m
.
def
(
"print_rec_packed"
,
&
print_recarray
<
PackedStruct
>
);
m
.
def
(
"print_rec_nested"
,
&
print_recarray
<
NestedStruct
>
);
m
.
def
(
"print_dtypes"
,
&
print_dtypes
);
}
example/example20.py
View file @
4f164217
...
...
@@ -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'
],
...
...
example/example20.ref
View file @
4f164217
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':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':12}
[('x', '?'), ('y', '<u4'), ('z', '<f4')]
[('a', {'names':['x','y','z'], 'formats':['?','<u4','<f4'], 'offsets':[0,4,8], 'itemsize':12}), ('b', [('x', '?'), ('y', '<u4'), ('z', '<f4')])]
s:0,0,0
s:1,1,1.5
s:0,2,3
p:0,0,0
p:1,1,1.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
\ No newline at end of file
include/pybind11/numpy.h
View file @
4f164217
...
...
@@ -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
)
template
<
typename
T
>
struct
npy_format_descriptor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment