Skip to content

Commit c7f1c5e

Browse files
author
Ralf W. Grosse-Kunstleve
committed
New example: nested.cpp
[SVN r10946]
1 parent 37b6e22 commit c7f1c5e

8 files changed

Lines changed: 109 additions & 2 deletions

File tree

build/filemgr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
bpl_exa + "/getting_started2.cpp",
2525
bpl_exa + "/simple_vector.cpp",
2626
bpl_exa + "/do_it_yourself_convts.cpp",
27+
bpl_exa + "/nested.cpp",
2728
bpl_exa + "/pickle1.cpp",
2829
bpl_exa + "/pickle2.cpp",
2930
bpl_exa + "/pickle3.cpp",
@@ -32,6 +33,7 @@
3233
bpl_exa + "/test_getting_started2.py",
3334
bpl_exa + "/test_simple_vector.py",
3435
bpl_exa + "/test_do_it_yourself_convts.py",
36+
bpl_exa + "/test_nested.py",
3537
bpl_exa + "/test_pickle1.py",
3638
bpl_exa + "/test_pickle2.py",
3739
bpl_exa + "/test_pickle3.py",
@@ -68,6 +70,7 @@
6870
"getting_started2",
6971
"simple_vector",
7072
"do_it_yourself_convts",
73+
"nested",
7174
"pickle1",
7275
"pickle2",
7376
"pickle3",

build/irix_CC.mak

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ DEPOBJ=$(OBJ) \
4444
getting_started1.o getting_started2.o \
4545
simple_vector.o \
4646
do_it_yourself_convts.o \
47+
nested.o \
4748
pickle1.o pickle2.o pickle3.o \
4849
noncopyable_export.o noncopyable_import.o \
4950
ivect.o dvect.o \
@@ -57,6 +58,7 @@ all: libboost_python.a \
5758
getting_started1.so getting_started2.so \
5859
simple_vector.so \
5960
do_it_yourself_convts.so \
61+
nested.so \
6062
pickle1.so pickle2.so pickle3.so \
6163
noncopyable_export.so noncopyable_import.so \
6264
ivect.so dvect.so \
@@ -84,6 +86,9 @@ simple_vector.so: $(OBJ) simple_vector.o
8486
do_it_yourself_convts.so: $(OBJ) do_it_yourself_convts.o
8587
$(LD) $(LDOPTS) $(OBJ) do_it_yourself_convts.o -o do_it_yourself_convts.so
8688

89+
nested.so: $(OBJ) nested.o
90+
$(LD) $(LDOPTS) $(OBJ) nested.o -o nested.so
91+
8792
pickle1.so: $(OBJ) pickle1.o
8893
$(LD) $(LDOPTS) $(OBJ) pickle1.o -o pickle1.so
8994

@@ -126,6 +131,7 @@ test:
126131
$(PYEXE) test_getting_started2.py
127132
$(PYEXE) test_simple_vector.py
128133
$(PYEXE) test_do_it_yourself_convts.py
134+
$(PYEXE) test_nested.py
129135
$(PYEXE) test_pickle1.py
130136
$(PYEXE) test_pickle2.py
131137
$(PYEXE) test_pickle3.py
@@ -142,6 +148,7 @@ clean:
142148
rm -f getting_started2.o getting_started2.so
143149
rm -f simple_vector.o simple_vector.so
144150
rm -f do_it_yourself_convts.o do_it_yourself_convts.so
151+
rm -f nested.o nested.so
145152
rm -f pickle1.o pickle1.so
146153
rm -f pickle2.o pickle2.so
147154
rm -f pickle3.o pickle3.so

build/linux_gcc.mak

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ DEPOBJ=$(OBJ) \
4545
getting_started1.o getting_started2.o \
4646
simple_vector.o \
4747
do_it_yourself_convts.o \
48+
nested.o \
4849
pickle1.o pickle2.o pickle3.o \
4950
noncopyable_export.o noncopyable_import.o \
5051
ivect.o dvect.o \
@@ -58,6 +59,7 @@ all: libboost_python.a \
5859
getting_started1.so getting_started2.so \
5960
simple_vector.so \
6061
do_it_yourself_convts.so \
62+
nested.so \
6163
pickle1.so pickle2.so pickle3.so \
6264
noncopyable_export.so noncopyable_import.so \
6365
ivect.so dvect.so \
@@ -85,6 +87,9 @@ simple_vector.so: $(OBJ) simple_vector.o
8587
do_it_yourself_convts.so: $(OBJ) do_it_yourself_convts.o
8688
$(LD) $(LDOPTS) $(OBJ) do_it_yourself_convts.o -o do_it_yourself_convts.so
8789

90+
nested.so: $(OBJ) nested.o
91+
$(LD) $(LDOPTS) $(OBJ) nested.o -o nested.so
92+
8893
pickle1.so: $(OBJ) pickle1.o
8994
$(LD) $(LDOPTS) $(OBJ) pickle1.o -o pickle1.so
9095

@@ -127,6 +132,7 @@ test:
127132
$(PYEXE) test_getting_started2.py
128133
$(PYEXE) test_simple_vector.py
129134
$(PYEXE) test_do_it_yourself_convts.py
135+
$(PYEXE) test_nested.py
130136
$(PYEXE) test_pickle1.py
131137
$(PYEXE) test_pickle2.py
132138
$(PYEXE) test_pickle3.py
@@ -143,6 +149,7 @@ clean:
143149
rm -f getting_started2.o getting_started2.so
144150
rm -f simple_vector.o simple_vector.so
145151
rm -f do_it_yourself_convts.o do_it_yourself_convts.so
152+
rm -f nested.o nested.so
146153
rm -f pickle1.o pickle1.so
147154
rm -f pickle2.o pickle2.so
148155
rm -f pickle3.o pickle3.so

build/mingw32.mak

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@
3030
# -fvtable-thunks eliminates the compiler warning, but
3131
# "import boost_python_test" still causes a crash.
3232

33-
ROOT=L:
33+
ROOT=R:
3434
BOOST_WIN="$(ROOT)\boost"
3535
BOOST_UNIX=$(HOME)/boost
3636

3737
PYEXE="C:\Program files\Python\python.exe"
3838
PYINC=-I"C:\usr\include\python1.5"
3939
PYLIB="C:\usr\lib\libpython15.a"
40+
#PYEXE="C:\Python21\python.exe"
41+
#PYINC=-I"C:\usr\include\python2.1"
42+
#PYLIB="C:\usr\lib\libpython21.a"
4043

4144
STDOPTS=-ftemplate-depth-21
4245
WARNOPTS=
@@ -60,6 +63,7 @@ all: libboost_python.a \
6063
getting_started1.pyd getting_started2.pyd \
6164
simple_vector.pyd \
6265
do_it_yourself_convts.pyd \
66+
nested.pyd \
6367
pickle1.pyd pickle2.pyd pickle3.pyd \
6468
noncopyable_export.pyd noncopyable_import.pyd \
6569
ivect.pyd dvect.pyd \
@@ -108,6 +112,12 @@ do_it_yourself_convts.pyd: $(OBJ) do_it_yourself_convts.o
108112
--def do_it_yourself_convts.def \
109113
$(OBJ) do_it_yourself_convts.o $(PYLIB)
110114

115+
nested.pyd: $(OBJ) nested.o
116+
dllwrap $(DLLWRAPOPTS) \
117+
--dllname nested.pyd \
118+
--def nested.def \
119+
$(OBJ) nested.o $(PYLIB)
120+
111121
pickle1.pyd: $(OBJ) pickle1.o
112122
dllwrap $(DLLWRAPOPTS) \
113123
--dllname pickle1.pyd \
@@ -178,6 +188,7 @@ test:
178188
$(PYEXE) test_getting_started2.py
179189
$(PYEXE) test_simple_vector.py
180190
$(PYEXE) test_do_it_yourself_convts.py
191+
$(PYEXE) test_nested.py
181192
$(PYEXE) test_pickle1.py
182193
$(PYEXE) test_pickle2.py
183194
$(PYEXE) test_pickle3.py

build/tru64_cxx.mak

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ DEPOBJ=$(OBJ) \
5555
getting_started1.o getting_started2.o \
5656
simple_vector.o \
5757
do_it_yourself_convts.o \
58+
nested.o \
5859
pickle1.o pickle2.o pickle3.o \
5960
noncopyable_export.o noncopyable_import.o \
6061
ivect.o dvect.o \
@@ -68,6 +69,7 @@ all: libboost_python.a \
6869
getting_started1.so getting_started2.so \
6970
simple_vector.so \
7071
do_it_yourself_convts.so \
72+
nested.so \
7173
pickle1.so pickle2.so pickle3.so \
7274
noncopyable_export.so noncopyable_import.so \
7375
ivect.so dvect.so \
@@ -99,6 +101,9 @@ simple_vector.so: $(OBJ) simple_vector.o
99101
do_it_yourself_convts.so: $(OBJ) do_it_yourself_convts.o
100102
$(LD) $(LDOPTS) $(OBJ) do_it_yourself_convts.o -o do_it_yourself_convts.so
101103

104+
nested.so: $(OBJ) nested.o
105+
$(LD) $(LDOPTS) $(OBJ) nested.o -o nested.so
106+
102107
pickle1.so: $(OBJ) pickle1.o
103108
$(LD) $(LDOPTS) $(OBJ) pickle1.o -o pickle1.so
104109

@@ -141,6 +146,7 @@ test:
141146
$(PYEXE) test_getting_started2.py
142147
$(PYEXE) test_simple_vector.py
143148
$(PYEXE) test_do_it_yourself_convts.py
149+
$(PYEXE) test_nested.py
144150
$(PYEXE) test_pickle1.py
145151
$(PYEXE) test_pickle2.py
146152
$(PYEXE) test_pickle3.py
@@ -157,6 +163,7 @@ clean:
157163
rm -f getting_started2.o getting_started2.so
158164
rm -f simple_vector.o simple_vector.so
159165
rm -f do_it_yourself_convts.o do_it_yourself_convts.so
166+
rm -f nested.o nested.so
160167
rm -f pickle1.o pickle1.so
161168
rm -f pickle2.o pickle2.so
162169
rm -f pickle3.o pickle3.so

build/vc60.mak

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
# 12 Apr 01 new macro ROOT to simplify configuration (R.W. Grosse-Kunstleve)
1111
# Initial version: R.W. Grosse-Kunstleve
1212

13-
ROOT=L:
13+
ROOT=R:
1414
BOOST_WIN="$(ROOT)\boost"
1515
BOOST_UNIX=$(HOME)/boost
1616

@@ -44,6 +44,7 @@ all: boost_python.lib \
4444
getting_started1.pyd getting_started2.pyd \
4545
simple_vector.pyd \
4646
do_it_yourself_convts.pyd \
47+
nested.pyd \
4748
pickle1.pyd pickle2.pyd pickle3.pyd \
4849
noncopyable_export.pyd noncopyable_import.pyd \
4950
ivect.pyd dvect.pyd \
@@ -70,6 +71,9 @@ simple_vector.pyd: $(OBJ) simple_vector.obj
7071
do_it_yourself_convts.pyd: $(OBJ) do_it_yourself_convts.obj
7172
$(LD) $(LDOPTS) $(OBJ) do_it_yourself_convts.obj $(PYLIB) /export:initdo_it_yourself_convts /out:"do_it_yourself_convts.pyd"
7273

74+
nested.pyd: $(OBJ) nested.obj
75+
$(LD) $(LDOPTS) $(OBJ) nested.obj $(PYLIB) /export:initnested /out:"nested.pyd"
76+
7377
pickle1.pyd: $(OBJ) pickle1.obj
7478
$(LD) $(LDOPTS) $(OBJ) pickle1.obj $(PYLIB) /export:initpickle1 /out:"pickle1.pyd"
7579

@@ -110,6 +114,7 @@ test:
110114
$(PYEXE) test_getting_started2.py
111115
$(PYEXE) test_simple_vector.py
112116
$(PYEXE) test_do_it_yourself_convts.py
117+
$(PYEXE) test_nested.py
113118
$(PYEXE) test_pickle1.py
114119
$(PYEXE) test_pickle2.py
115120
$(PYEXE) test_pickle3.py

example/nested.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Example by Ralf W. Grosse-Kunstleve
2+
3+
/*
4+
This example shows how convert a nested Python tuple.
5+
*/
6+
7+
#include <boost/python/class_builder.hpp>
8+
#include <stdio.h>
9+
10+
namespace {
11+
12+
boost::python::list
13+
show_nested_tuples(boost::python::tuple outer)
14+
{
15+
boost::python::list result;
16+
for (int i = 0; i < outer.size(); i++) {
17+
boost::python::tuple inner(
18+
BOOST_PYTHON_CONVERSION::from_python(outer[i].get(),
19+
boost::python::type<boost::python::tuple>()));
20+
for (int j = 0; j < inner.size(); j++) {
21+
double x = BOOST_PYTHON_CONVERSION::from_python(inner[j].get(),
22+
boost::python::type<double>());
23+
char buf[128];
24+
sprintf(buf, "(%d,%d) %.6g", i, j, x);
25+
result.append(BOOST_PYTHON_CONVERSION::to_python(std::string(buf)));
26+
}
27+
}
28+
return result;
29+
}
30+
31+
}
32+
33+
BOOST_PYTHON_MODULE_INIT(nested)
34+
{
35+
try
36+
{
37+
boost::python::module_builder this_module("nested");
38+
this_module.def(show_nested_tuples, "show_nested_tuples");
39+
}
40+
catch(...)
41+
{
42+
boost::python::handle_exception();
43+
}
44+
}

example/test_nested.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
r'''>>> import nested
2+
>>> s = nested.show_nested_tuples(((1,2,3), (4,5,6,7)))
3+
>>> for l in s:
4+
... print l
5+
(0,0) 1
6+
(0,1) 2
7+
(0,2) 3
8+
(1,0) 4
9+
(1,1) 5
10+
(1,2) 6
11+
(1,3) 7
12+
'''
13+
14+
def run(args = None):
15+
if args is not None:
16+
import sys
17+
sys.argv = args
18+
import doctest, test_nested
19+
return doctest.testmod(test_nested)
20+
21+
if __name__ == '__main__':
22+
import sys
23+
sys.exit(run()[0])

0 commit comments

Comments
 (0)