Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
4f164217
Commit
4f164217
authored
Jun 22, 2016
by
Ivan Smirnov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add dtype_of<T>() function, update the tests
parent
036e8cd3
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
20 deletions
+40
-20
example/example20.cpp
+20
-10
example/example20.py
+2
-1
example/example20.ref
+13
-9
include/pybind11/numpy.h
+5
-0
No files found.
example/example20.cpp
View file @
4f164217
...
@@ -15,14 +15,14 @@
...
@@ -15,14 +15,14 @@
namespace
py
=
pybind11
;
namespace
py
=
pybind11
;
struct
Struct
{
struct
S
impleS
truct
{
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
S
impleS
truct
&
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 {
...
@@ -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
;
S
impleS
truct
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) {
...
@@ -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
<
S
impleS
truct
>::
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
(
S
impleS
truct
,
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
<
S
impleS
truct
>
);
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
<
S
impleS
truct
>
);
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
);
}
}
example/example20.py
View file @
4f164217
...
@@ -4,7 +4,7 @@ from __future__ import print_function
...
@@ -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):
...
@@ -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'
],
...
...
example/example20.ref
View file @
4f164217
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
\ No newline at end of file
include/pybind11/numpy.h
View file @
4f164217
...
@@ -169,6 +169,11 @@ template <typename T> struct format_descriptor
...
@@ -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
>
{
...
...
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