diff --git a/example/Jamroot b/example/Jamroot index 27ac49e8..fe9d69ec 100644 --- a/example/Jamroot +++ b/example/Jamroot @@ -1,43 +1,35 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) -# Specify the path to the Boost project. If you move this project, -# adjust this path to refer to the Boost root directory. -use-project boost - : ../../.. ; +import python ; -# Set up the project-wide requirements that everything uses the -# boost_python library from the project whose global ID is -# /boost/python. -project - : requirements /boost/python//boost_python - /boost//headers - : usage-requirements /boost//headers - ; - -# Declare the three extension modules. You can specify multiple -# source files after the colon separated by spaces. -python-extension getting_started1 : getting_started1.cpp ; -python-extension getting_started2 : getting_started2.cpp ; -python-extension std_pair_ext : std_pair.cpp ; - -# A little "rule" (function) to clean up the syntax of declaring tests -# of these extension modules. -local rule run-test ( test-name : sources + ) +if ! [ python.configured ] { - import testing ; - testing.make-test run-pyd : $(sources) : : $(test-name) ; + ECHO "warning: no Python configured in user-config.jam" ; + ECHO "warning: will use default configuration" ; + using python ; } -# Declare test targets -run-test test1 : getting_started1 test_getting_started1.py ; -run-test test2 : getting_started2 test_getting_started2.py ; -run-test test3 : std_pair_ext test_std_pair.py ; +# Adjust the following if Boost.Python isn't installed in a default location +lib boost_python ; -# A target that runs all the tests -alias test : test1 test2 test3 ; +project + : requirements +# /path/to/boost/python + boost_python +; -# Only run tests when explicitly requested -explicit test test1 test2 test3 ; +rule run-test ( test-name : sources + ) +{ + import testing ; + testing.make-test run-pyd : $(sources) : : $(test-name) ; +} +build-project quickstart ; +build-project tutorial ; +if [ python.numpy ] +{ + build-project numpy ; +} diff --git a/example/README b/example/README deleted file mode 100644 index 29a94f67..00000000 --- a/example/README +++ /dev/null @@ -1,16 +0,0 @@ -.. Copyright David Abrahams 2006. Distributed under the Boost -.. Software License, Version 1.0. (See accompanying -.. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -To get started with the Boost Python Library, use the examples -getting_started1.cpp and getting_started2.cpp. Invoking - - bjam --toolset=your-toolset test - -in this directory will build and run the examples. See -http://www.boost.org/more/getting_started.html for details about the ---toolset= option. - -If you move this example from its place in the Boost development tree -you'll need to edit the two lines indicated in Jamroot and -boost-build.jam. diff --git a/example/README.md b/example/README.md new file mode 100644 index 00000000..b090cbe1 --- /dev/null +++ b/example/README.md @@ -0,0 +1,11 @@ +![logo](https://raw.githubusercontent.com/boostorg/python/develop/doc/images/bpl.png) + +# Examples + +This directory contains various examples using Boost.Python. +You may compile these using the `bjam` command either in this directory +or in any of the subdirectories. +You may need to adjust the paths in the Jamroot file if Boost.Python +is not installed in a default location. +See http://boostorg.github.io/python/doc/html/building/no_install_quickstart.html +for details. diff --git a/example/boost-build.jam b/example/boost-build.jam deleted file mode 100755 index 9b8d19e0..00000000 --- a/example/boost-build.jam +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Edit this path to point at the tools/build/src subdirectory of your -# Boost installation. Absolute paths work, too. -boost-build ../../../tools/build/src ; diff --git a/example/getting_started1.cpp b/example/getting_started1.cpp deleted file mode 100644 index 09d68103..00000000 --- a/example/getting_started1.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include - -namespace { // Avoid cluttering the global namespace. - - // A couple of simple C++ functions that we want to expose to Python. - std::string greet() { return "hello, world"; } - int square(int number) { return number * number; } -} - -namespace python = boost::python; - -// Python requires an exported function called init in every -// extension module. This is where we build the module contents. -BOOST_PYTHON_MODULE(getting_started1) -{ - // Add regular functions to the module. - python::def("greet", greet); - python::def("square", square); -} diff --git a/example/getting_started2.cpp b/example/getting_started2.cpp deleted file mode 100644 index ee8af32e..00000000 --- a/example/getting_started2.cpp +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include -#include -#include - -namespace { // Avoid cluttering the global namespace. - - // A friendly class. - class hello - { - public: - hello(const std::string& country) { this->country = country; } - std::string greet() const { return "Hello from " + country; } - private: - std::string country; - }; - - // A function taking a hello object as an argument. - std::string invite(const hello& w) { - return w.greet() + "! Please come soon!"; - } -} - -BOOST_PYTHON_MODULE(getting_started2) -{ - using namespace boost::python; - class_("hello", init()) - // Add a regular member function. - .def("greet", &hello::greet) - // Add invite() as a member of hello! - .def("invite", invite) - ; - - // Also add invite() as a regular function to the module. - def("invite", invite); -} diff --git a/example/numpy/Jamfile b/example/numpy/Jamfile new file mode 100644 index 00000000..ac70a6ff --- /dev/null +++ b/example/numpy/Jamfile @@ -0,0 +1,29 @@ +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import python ; + +# Adjust the following if Boost.Python isn't installed in a default location +lib boost_numpy + : + : /usr/local/Boost/lib + /usr/local/Boost/include + ; + +project numpy + : requirements + /usr/local/Boost/include + boost_numpy + . + ; + +exe simple : simple.cpp boost_numpy /python//python ; +exe dtype : dtype.cpp boost_numpy /python//python ; +exe ndarray : ndarray.cpp /python//python ; +exe fromdata : fromdata.cpp /python//python ; +exe ufunc : ufunc.cpp /python//python ; +exe wrap : wrap.cpp /python//python ; + +python-extension gaussian : gaussian.cpp ; diff --git a/example/numpy/dtype.cpp b/example/numpy/dtype.cpp index 41c15db3..749a36b5 100644 --- a/example/numpy/dtype.cpp +++ b/example/numpy/dtype.cpp @@ -12,11 +12,11 @@ * */ -#include +#include #include namespace p = boost::python; -namespace np = boost::numpy; +namespace np = boost::python::numpy; int main(int argc, char **argv) { diff --git a/example/numpy/fromdata.cpp b/example/numpy/fromdata.cpp index 04dd5e1d..bd073cc6 100644 --- a/example/numpy/fromdata.cpp +++ b/example/numpy/fromdata.cpp @@ -8,11 +8,11 @@ * manipulate data in either Python or C++ and have the changes reflected in both. */ -#include +#include #include namespace p = boost::python; -namespace np = boost::numpy; +namespace np = boost::python::numpy; int main(int argc, char **argv) diff --git a/example/numpy/gaussian.cpp b/example/numpy/gaussian.cpp index 85045382..5f138b39 100644 --- a/example/numpy/gaussian.cpp +++ b/example/numpy/gaussian.cpp @@ -3,7 +3,7 @@ // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#include +#include #include #include @@ -14,7 +14,7 @@ const double M_PI = boost::math::constants::pi(); #endif namespace bp = boost::python; -namespace bn = boost::numpy; +namespace bn = boost::python::numpy; /** * A 2x2 matrix class, purely for demonstration purposes. diff --git a/example/numpy/ndarray.cpp b/example/numpy/ndarray.cpp index 510fd94a..d7b57aa7 100644 --- a/example/numpy/ndarray.cpp +++ b/example/numpy/ndarray.cpp @@ -11,11 +11,11 @@ * non-unit strides. */ -#include +#include #include namespace p = boost::python; -namespace np = boost::numpy; +namespace np = boost::python::numpy; #if _MSC_VER using boost::uint8_t; diff --git a/example/numpy/simple.cpp b/example/numpy/simple.cpp index b1f2dc3f..ad598bde 100644 --- a/example/numpy/simple.cpp +++ b/example/numpy/simple.cpp @@ -3,11 +3,11 @@ // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) -#include +#include #include namespace p = boost::python; -namespace np = boost::numpy; +namespace np = boost::python::numpy; int main(int argc, char **argv) { diff --git a/example/numpy/ufunc.cpp b/example/numpy/ufunc.cpp index 66a691a6..5fb69204 100644 --- a/example/numpy/ufunc.cpp +++ b/example/numpy/ufunc.cpp @@ -11,11 +11,11 @@ * None of the methods like np::add, np::multiply etc are supported as yet */ -#include +#include #include namespace p = boost::python; -namespace np = boost::numpy; +namespace np = boost::python::numpy; // Create the structs necessary to implement the ufuncs diff --git a/example/numpy/wrap.cpp b/example/numpy/wrap.cpp index 5d2027eb..33f71c85 100644 --- a/example/numpy/wrap.cpp +++ b/example/numpy/wrap.cpp @@ -23,12 +23,12 @@ * Python. Again, see the Gaussian demo for an example. */ -#include +#include #include #include namespace p = boost::python; -namespace np = boost::numpy; +namespace np = boost::python::numpy; // This is roughly the most efficient way to write a C/C++ function that operates // on a 2-d NumPy array - operate directly on the array by incrementing a pointer diff --git a/example/project.zip b/example/project.zip deleted file mode 100644 index d863defd..00000000 Binary files a/example/project.zip and /dev/null differ diff --git a/example/quickstart/Jamroot b/example/quickstart/Jamfile similarity index 51% rename from example/quickstart/Jamroot rename to example/quickstart/Jamfile index 8425638c..6dd35130 100644 --- a/example/quickstart/Jamroot +++ b/example/quickstart/Jamfile @@ -1,23 +1,15 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) -# Specify the path to the Boost project. If you move this project, -# adjust the path to refer to the Boost root directory. -use-project boost - : ../../../.. ; - -# Set up the project-wide requirements that everything uses the -# boost_python library defined in the project whose global ID is -# /boost/python. -project boost-python-quickstart - : requirements /boost/python//boost_python - /boost//headers - : usage-requirements /boost//headers - ; - -# Make the definition of the python-extension rule available import python ; +import testing ; + +project quickstart + : requirements + . + ; # Declare a Python extension called hello. python-extension extending : extending.cpp ; @@ -25,8 +17,6 @@ python-extension extending : extending.cpp ; # Declare an executable called embedding that embeds Python exe embedding : embedding.cpp /python//python ; -import testing ; - # Declare a test of the extension module testing.make-test run-pyd : extending test_extending.py : : test_ext ; diff --git a/example/quickstart/boost-build.jam b/example/quickstart/boost-build.jam deleted file mode 100644 index cf910e79..00000000 --- a/example/quickstart/boost-build.jam +++ /dev/null @@ -1,7 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -# Edit this path to point at the tools/build/src subdirectory of your -# Boost installation. Absolute paths work, too. -boost-build ../../../../tools/build/src ; diff --git a/example/quickstart/script.py b/example/quickstart/script.py index 5a8faf79..c3e034ba 100644 --- a/example/quickstart/script.py +++ b/example/quickstart/script.py @@ -1,6 +1,7 @@ +#! /usr/bin/env python # Copyright Stefan Seefeld 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -print 'Hello World !' +print('Hello World !') number = 42 diff --git a/example/quickstart/test_extending.py b/example/quickstart/test_extending.py index 14616f7c..938c7b90 100644 --- a/example/quickstart/test_extending.py +++ b/example/quickstart/test_extending.py @@ -1,3 +1,4 @@ +#! /usr/bin/env python # Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) diff --git a/example/std_pair.cpp b/example/std_pair.cpp deleted file mode 100644 index edf98dd6..00000000 --- a/example/std_pair.cpp +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost -// Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include -#include -#include - -namespace { // Avoid cluttering the global namespace. - - // Converts a std::pair instance to a Python tuple. - template - struct std_pair_to_tuple - { - static PyObject* convert(std::pair const& p) - { - return boost::python::incref( - boost::python::make_tuple(p.first, p.second).ptr()); - } - static PyTypeObject const *get_pytype () {return &PyTuple_Type; } - }; - - // Helper for convenience. - template - struct std_pair_to_python_converter - { - std_pair_to_python_converter() - { - boost::python::to_python_converter< - std::pair, - std_pair_to_tuple, - true //std_pair_to_tuple has get_pytype - >(); - } - }; - - // Example function returning a std::pair. - std::pair - foo() { return std::pair(3, 5); } - -} // namespace anonymous - -BOOST_PYTHON_MODULE(std_pair_ext) -{ - using namespace boost::python; - std_pair_to_python_converter(); - def("foo", foo); -} diff --git a/example/test_getting_started1.py b/example/test_getting_started1.py deleted file mode 100644 index 32dc1f6e..00000000 --- a/example/test_getting_started1.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -r'''>>> import getting_started1 - >>> print getting_started1.greet() - hello, world - >>> number = 11 - >>> print number, '*', number, '=', getting_started1.square(number) - 11 * 11 = 121 -''' - -def run(args = None): - if args is not None: - import sys - sys.argv = args - import doctest, test_getting_started1 - return doctest.testmod(test_getting_started1) - -if __name__ == '__main__': - import sys - sys.exit(run()[0]) diff --git a/example/test_getting_started2.py b/example/test_getting_started2.py deleted file mode 100644 index ae86017b..00000000 --- a/example/test_getting_started2.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -r'''>>> from getting_started2 import * - >>> hi = hello('California') - >>> hi.greet() - 'Hello from California' - >>> invite(hi) - 'Hello from California! Please come soon!' - >>> hi.invite() - 'Hello from California! Please come soon!' - - >>> class wordy(hello): - ... def greet(self): - ... return hello.greet(self) + ', where the weather is fine' - ... - >>> hi2 = wordy('Florida') - >>> hi2.greet() - 'Hello from Florida, where the weather is fine' - >>> invite(hi2) - 'Hello from Florida! Please come soon!' -''' - -def run(args = None): - if args is not None: - import sys - sys.argv = args - import doctest, test_getting_started2 - return doctest.testmod(test_getting_started2) - -if __name__ == '__main__': - import sys - sys.exit(run()[0]) - diff --git a/example/test_std_pair.py b/example/test_std_pair.py deleted file mode 100644 index 64f239fe..00000000 --- a/example/test_std_pair.py +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright Ralf W. Grosse-Kunstleve 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -import std_pair_ext -assert std_pair_ext.foo() == (3, 5) -print "OK" diff --git a/example/tutorial/Jamfile b/example/tutorial/Jamfile new file mode 100644 index 00000000..a32272e7 --- /dev/null +++ b/example/tutorial/Jamfile @@ -0,0 +1,19 @@ +# Copyright Stefan Seefeld 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +import python ; + +project tutorial + : requirements + . + ; + +python-extension hello_ext : hello.cpp ; + +run-test hello : hello_ext hello.py ; + +alias test : hello ; +explicit test ; + diff --git a/example/tutorial/Jamroot b/example/tutorial/Jamroot deleted file mode 100644 index ac5e1f03..00000000 --- a/example/tutorial/Jamroot +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright David Abrahams 2006. Distributed under the Boost -# Software License, Version 1.0. (See accompanying -# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -import python ; - -if ! [ python.configured ] -{ - ECHO "notice: no Python configured in user-config.jam" ; - ECHO "notice: will use default configuration" ; - using python ; -} - -# Specify the path to the Boost project. If you move this project, -# adjust this path to refer to the Boost root directory. -use-project boost - : ../../../.. ; - -# Set up the project-wide requirements that everything uses the -# boost_python library from the project whose global ID is -# /boost/python. -project - : requirements /boost/python//boost_python - /boost//headers - : usage-requirements /boost//headers - ; - -# Declare the three extension modules. You can specify multiple -# source files after the colon separated by spaces. -python-extension hello_ext : hello.cpp ; - -# Put the extension and Boost.Python DLL in the current directory, so -# that running script by hand works. -install convenient_copy - : hello_ext - : on SHARED_LIB PYTHON_EXTENSION - . - ; - -# A little "rule" (function) to clean up the syntax of declaring tests -# of these extension modules. -local rule run-test ( test-name : sources + ) -{ - import testing ; - testing.make-test run-pyd : $(sources) : : $(test-name) ; -} - -# Declare test targets -run-test hello : hello_ext hello.py ; - - diff --git a/example/tutorial/hello.py b/example/tutorial/hello.py index d18b1c53..31f75565 100755 --- a/example/tutorial/hello.py +++ b/example/tutorial/hello.py @@ -1,7 +1,8 @@ +#! /usr/bin/env python # Copyright Joel de Guzman 2002-2007. Distributed under the Boost # Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt # or copy at http://www.boost.org/LICENSE_1_0.txt) # Hello World Example from the tutorial import hello_ext -print hello_ext.greet() +print(hello_ext.greet())