Commit 61352e50 authored by Wenzel Jakob's avatar Wenzel Jakob Committed by GitHub
Browse files

Merge pull request #289 from jagerman/example-renaming

Rename examples files, as per #288
parents fb6aed21 3e2e44f5
/*
example/example15.cpp -- pickle support
example/example-pickling.cpp -- pickle support
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -24,7 +24,7 @@ private:
int m_extra2 = 0;
};
void init_ex15(py::module &m) {
void init_ex_pickling(py::module &m) {
py::class_<Pickleable>(m, "Pickleable")
.def(py::init<std::string>())
.def("value", &Pickleable::value)
......
/*
example/example2.cpp2 -- singleton design pattern, static functions and
example/example-python-types.cpp2 -- singleton design pattern, static functions and
variables, passing and interacting with Python types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -16,13 +16,13 @@
# include <fcntl.h>
#endif
class Example2 {
class ExamplePythonTypes {
public:
static Example2 *new_instance() {
return new Example2();
static ExamplePythonTypes *new_instance() {
return new ExamplePythonTypes();
}
~Example2() {
std::cout << "Destructing Example2" << std::endl;
~ExamplePythonTypes() {
std::cout << "Destructing ExamplePythonTypes" << std::endl;
}
/* Create and return a Python dictionary */
......@@ -142,31 +142,31 @@ public:
static const int value2;
};
int Example2::value = 0;
const int Example2::value2 = 5;
int ExamplePythonTypes::value = 0;
const int ExamplePythonTypes::value2 = 5;
void init_ex2(py::module &m) {
void init_ex_python_types(py::module &m) {
/* No constructor is explicitly defined below. An exception is raised when
trying to construct it directly from Python */
py::class_<Example2>(m, "Example2", "Example 2 documentation")
.def("get_dict", &Example2::get_dict, "Return a Python dictionary")
.def("get_dict_2", &Example2::get_dict_2, "Return a C++ dictionary")
.def("get_list", &Example2::get_list, "Return a Python list")
.def("get_list_2", &Example2::get_list_2, "Return a C++ list")
.def("get_set", &Example2::get_set, "Return a Python set")
.def("get_set2", &Example2::get_set_2, "Return a C++ set")
.def("get_array", &Example2::get_array, "Return a C++ array")
.def("print_dict", &Example2::print_dict, "Print entries of a Python dictionary")
.def("print_dict_2", &Example2::print_dict_2, "Print entries of a C++ dictionary")
.def("print_set", &Example2::print_set, "Print entries of a Python set")
.def("print_set_2", &Example2::print_set_2, "Print entries of a C++ set")
.def("print_list", &Example2::print_list, "Print entries of a Python list")
.def("print_list_2", &Example2::print_list_2, "Print entries of a C++ list")
.def("print_array", &Example2::print_array, "Print entries of a C++ array")
.def("pair_passthrough", &Example2::pair_passthrough, "Return a pair in reversed order")
.def("tuple_passthrough", &Example2::tuple_passthrough, "Return a triple in reversed order")
.def("throw_exception", &Example2::throw_exception, "Throw an exception")
.def_static("new_instance", &Example2::new_instance, "Return an instance")
.def_readwrite_static("value", &Example2::value, "Static value member")
.def_readonly_static("value2", &Example2::value2, "Static value member (readonly)");
py::class_<ExamplePythonTypes>(m, "ExamplePythonTypes", "Example 2 documentation")
.def("get_dict", &ExamplePythonTypes::get_dict, "Return a Python dictionary")
.def("get_dict_2", &ExamplePythonTypes::get_dict_2, "Return a C++ dictionary")
.def("get_list", &ExamplePythonTypes::get_list, "Return a Python list")
.def("get_list_2", &ExamplePythonTypes::get_list_2, "Return a C++ list")
.def("get_set", &ExamplePythonTypes::get_set, "Return a Python set")
.def("get_set2", &ExamplePythonTypes::get_set_2, "Return a C++ set")
.def("get_array", &ExamplePythonTypes::get_array, "Return a C++ array")
.def("print_dict", &ExamplePythonTypes::print_dict, "Print entries of a Python dictionary")
.def("print_dict_2", &ExamplePythonTypes::print_dict_2, "Print entries of a C++ dictionary")
.def("print_set", &ExamplePythonTypes::print_set, "Print entries of a Python set")
.def("print_set_2", &ExamplePythonTypes::print_set_2, "Print entries of a C++ set")
.def("print_list", &ExamplePythonTypes::print_list, "Print entries of a Python list")
.def("print_list_2", &ExamplePythonTypes::print_list_2, "Print entries of a C++ list")
.def("print_array", &ExamplePythonTypes::print_array, "Print entries of a C++ array")
.def("pair_passthrough", &ExamplePythonTypes::pair_passthrough, "Return a pair in reversed order")
.def("tuple_passthrough", &ExamplePythonTypes::tuple_passthrough, "Return a triple in reversed order")
.def("throw_exception", &ExamplePythonTypes::throw_exception, "Throw an exception")
.def_static("new_instance", &ExamplePythonTypes::new_instance, "Return an instance")
.def_readwrite_static("value", &ExamplePythonTypes::value, "Static value member")
.def_readonly_static("value2", &ExamplePythonTypes::value2, "Static value member (readonly)");
}
......@@ -4,23 +4,23 @@ import sys, pydoc
sys.path.append('.')
import example
from example import Example2
from example import ExamplePythonTypes
Example2.value = 15
print(Example2.value)
print(Example2.value2)
ExamplePythonTypes.value = 15
print(ExamplePythonTypes.value)
print(ExamplePythonTypes.value2)
try:
Example2()
ExamplePythonTypes()
except Exception as e:
print(e)
try:
Example2.value2 = 15
ExamplePythonTypes.value2 = 15
except Exception as e:
print(e)
instance = Example2.new_instance()
instance = ExamplePythonTypes.new_instance()
dict_result = instance.get_dict()
dict_result['key2'] = 'value2'
......@@ -58,10 +58,10 @@ except Exception as e:
print(instance.pair_passthrough((True, "test")))
print(instance.tuple_passthrough((True, "test", 5)))
print(pydoc.render_doc(Example2, "Help on %s"))
print(pydoc.render_doc(ExamplePythonTypes, "Help on %s"))
print("__name__(example) = %s" % example.__name__)
print("__name__(example.Example2) = %s" % Example2.__name__)
print("__module__(example.Example2) = %s" % Example2.__module__)
print("__name__(example.Example2.get_set) = %s" % Example2.get_set.__name__)
print("__module__(example.Example2.get_set) = %s" % Example2.get_set.__module__)
print("__name__(example.ExamplePythonTypes) = %s" % ExamplePythonTypes.__name__)
print("__module__(example.ExamplePythonTypes) = %s" % ExamplePythonTypes.__module__)
print("__name__(example.ExamplePythonTypes.get_set) = %s" % ExamplePythonTypes.get_set.__name__)
print("__module__(example.ExamplePythonTypes.get_set) = %s" % ExamplePythonTypes.get_set.__module__)
15
5
example.Example2: No constructor defined!
example.ExamplePythonTypes: No constructor defined!
can't set attribute
key: key2, value=value2
key: key, value=value
......@@ -23,9 +23,9 @@ array item 1: array entry 2
This exception was intentionally thrown.
(u'test', True)
(5L, u'test', True)
Help on class Example2 in module example
Help on class ExamplePythonTypes in module example
class EExxaammppllee22(__builtin__.object)
class EExxaammpplleePPyytthhoonnTTyyppeess(__builtin__.object)
| Example 2 documentation
|
| Methods defined here:
......@@ -34,104 +34,104 @@ class EExxaammppllee22(__builtin__.object)
| x.__init__(...) initializes x; see help(type(x)) for signature
|
| ggeett__aarrrraayy(...)
| Signature : (example.Example2) -> list<unicode>[2]
| Signature : (example.ExamplePythonTypes) -> list<unicode>[2]
|
| Return a C++ array
|
| ggeett__ddiicctt(...)
| Signature : (example.Example2) -> dict
| Signature : (example.ExamplePythonTypes) -> dict
|
| Return a Python dictionary
|
| ggeett__ddiicctt__22(...)
| Signature : (example.Example2) -> dict<unicode, unicode>
| Signature : (example.ExamplePythonTypes) -> dict<unicode, unicode>
|
| Return a C++ dictionary
|
| ggeett__lliisstt(...)
| Signature : (example.Example2) -> list
| Signature : (example.ExamplePythonTypes) -> list
|
| Return a Python list
|
| ggeett__lliisstt__22(...)
| Signature : (example.Example2) -> list<unicode>
| Signature : (example.ExamplePythonTypes) -> list<unicode>
|
| Return a C++ list
|
| ggeett__sseett(...)
| Signature : (example.Example2) -> set
| Signature : (example.ExamplePythonTypes) -> set
|
| Return a Python set
|
| ggeett__sseett22(...)
| Signature : (example.Example2) -> set
| Signature : (example.ExamplePythonTypes) -> set
|
| Return a C++ set
|
| ppaaiirr__ppaasssstthhrroouugghh(...)
| Signature : (example.Example2, (bool, unicode)) -> (unicode, bool)
| Signature : (example.ExamplePythonTypes, (bool, unicode)) -> (unicode, bool)
|
| Return a pair in reversed order
|
| pprriinntt__aarrrraayy(...)
| Signature : (example.Example2, list<unicode>[2]) -> NoneType
| Signature : (example.ExamplePythonTypes, list<unicode>[2]) -> NoneType
|
| Print entries of a C++ array
|
| pprriinntt__ddiicctt(...)
| Signature : (example.Example2, dict) -> NoneType
| Signature : (example.ExamplePythonTypes, dict) -> NoneType
|
| Print entries of a Python dictionary
|
| pprriinntt__ddiicctt__22(...)
| Signature : (example.Example2, dict<unicode, unicode>) -> NoneType
| Signature : (example.ExamplePythonTypes, dict<unicode, unicode>) -> NoneType
|
| Print entries of a C++ dictionary
|
| pprriinntt__lliisstt(...)
| Signature : (example.Example2, list) -> NoneType
| Signature : (example.ExamplePythonTypes, list) -> NoneType
|
| Print entries of a Python list
|
| pprriinntt__lliisstt__22(...)
| Signature : (example.Example2, list<unicode>) -> NoneType
| Signature : (example.ExamplePythonTypes, list<unicode>) -> NoneType
|
| Print entries of a C++ list
|
| pprriinntt__sseett(...)
| Signature : (example.Example2, set) -> NoneType
| Signature : (example.ExamplePythonTypes, set) -> NoneType
|
| Print entries of a Python set
|
| pprriinntt__sseett__22(...)
| Signature : (example.Example2, set<unicode>) -> NoneType
| Signature : (example.ExamplePythonTypes, set<unicode>) -> NoneType
|
| Print entries of a C++ set
|
| tthhrrooww__eexxcceeppttiioonn(...)
| Signature : (example.Example2) -> NoneType
| Signature : (example.ExamplePythonTypes) -> NoneType
|
| Throw an exception
|
| ttuuppllee__ppaasssstthhrroouugghh(...)
| Signature : (example.Example2, (bool, unicode, int)) -> (int, unicode, bool)
| Signature : (example.ExamplePythonTypes, (bool, unicode, int)) -> (int, unicode, bool)
|
| Return a triple in reversed order
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| ____nneeww____ = <built-in method __new__ of example.Example2__Meta object>
| ____nneeww____ = <built-in method __new__ of example.ExamplePythonTypes__Meta object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
| nneeww__iinnssttaannccee = <built-in method new_instance of PyCapsule object>
| Signature : () -> example.Example2
| Signature : () -> example.ExamplePythonTypes
|
| Return an instance
__name__(example) = example
__name__(example.Example2) = Example2
__module__(example.Example2) = example
__name__(example.Example2.get_set) = get_set
__module__(example.Example2.get_set) = example
Destructing Example2
__name__(example.ExamplePythonTypes) = ExamplePythonTypes
__module__(example.ExamplePythonTypes) = example
__name__(example.ExamplePythonTypes.get_set) = get_set
__module__(example.ExamplePythonTypes.get_set) = example
Destructing ExamplePythonTypes
/*
example/example6.cpp -- supporting Pythons' sequence protocol, iterators,
example/example-sequences-and-iterators.cpp -- supporting Pythons' sequence protocol, iterators,
etc.
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -109,7 +109,7 @@ private:
float *m_data;
};
void init_ex6(py::module &m) {
void init_ex_sequences_and_iterators(py::module &m) {
py::class_<Sequence> seq(m, "Sequence");
seq.def(py::init<size_t>())
......
/*
example/example8.cpp -- binding classes with custom reference counting,
example/example-smart-ptr.cpp -- binding classes with custom reference counting,
implicit conversions between types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -105,7 +105,7 @@ void print_myobject3_2(std::shared_ptr<MyObject3> obj) { std::cout << obj->toStr
void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { std::cout << obj->toString() << std::endl; }
void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { std::cout << (*obj)->toString() << std::endl; }
void init_ex8(py::module &m) {
void init_ex_smart_ptr(py::module &m) {
py::class_<Object, ref<Object>> obj(m, "Object");
obj.def("getRefCount", &Object::getRefCount);
......
/*
example/example17.cpp -- Usage of stl_binders functions
example/example-stl-binder-vector.cpp -- Usage of stl_binders functions
Copyright (c) 2016 Sergey Lyskov
......@@ -24,7 +24,7 @@ std::ostream & operator<<(std::ostream &s, El const&v) {
return s;
}
void init_ex17(py::module &m) {
void init_ex_stl_binder_vector(py::module &m) {
pybind11::class_<El>(m, "El")
.def(pybind11::init<int>());
......
/*
example/example12.cpp -- overriding virtual functions from Python
example/example-virtual-functions.cpp -- overriding virtual functions from Python
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -11,18 +11,18 @@
#include <pybind11/functional.h>
/* This is an example class that we'll want to be able to extend from Python */
class Example12 {
class ExampleVirt {
public:
Example12(int state) : state(state) {
cout << "Constructing Example12.." << endl;
ExampleVirt(int state) : state(state) {
cout << "Constructing ExampleVirt.." << endl;
}
~Example12() {
cout << "Destructing Example12.." << endl;
~ExampleVirt() {
cout << "Destructing ExampleVirt.." << endl;
}
virtual int run(int value) {
std::cout << "Original implementation of Example12::run(state=" << state
std::cout << "Original implementation of ExampleVirt::run(state=" << state
<< ", value=" << value << ")" << std::endl;
return state + value;
}
......@@ -34,24 +34,24 @@ private:
};
/* This is a wrapper class that must be generated */
class PyExample12 : public Example12 {
class PyExampleVirt : public ExampleVirt {
public:
using Example12::Example12; /* Inherit constructors */
using ExampleVirt::ExampleVirt; /* Inherit constructors */
virtual int run(int value) {
/* Generate wrapping code that enables native function overloading */
PYBIND11_OVERLOAD(
int, /* Return type */
Example12, /* Parent class */
run, /* Name of function */
value /* Argument(s) */
int, /* Return type */
ExampleVirt, /* Parent class */
run, /* Name of function */
value /* Argument(s) */
);
}
virtual bool run_bool() {
PYBIND11_OVERLOAD_PURE(
bool, /* Return type */
Example12, /* Parent class */
ExampleVirt, /* Parent class */
run_bool, /* Name of function */
/* This function has no arguments. The trailing comma
in the previous line is needed for some compilers */
......@@ -61,7 +61,7 @@ public:
virtual void pure_virtual() {
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
Example12, /* Parent class */
ExampleVirt, /* Parent class */
pure_virtual, /* Name of function */
/* This function has no arguments. The trailing comma
in the previous line is needed for some compilers */
......@@ -69,30 +69,30 @@ public:
}
};
int runExample12(Example12 *ex, int value) {
int runExampleVirt(ExampleVirt *ex, int value) {
return ex->run(value);
}
bool runExample12Bool(Example12* ex) {
bool runExampleVirtBool(ExampleVirt* ex) {
return ex->run_bool();
}
void runExample12Virtual(Example12 *ex) {
void runExampleVirtVirtual(ExampleVirt *ex) {
ex->pure_virtual();
}
void init_ex12(py::module &m) {
/* Important: indicate the trampoline class PyExample12 using the third
void init_ex_virtual_functions(py::module &m) {
/* Important: indicate the trampoline class PyExampleVirt using the third
argument to py::class_. The second argument with the unique pointer
is simply the default holder type used by pybind11. */
py::class_<Example12, std::unique_ptr<Example12>, PyExample12>(m, "Example12")
py::class_<ExampleVirt, std::unique_ptr<ExampleVirt>, PyExampleVirt>(m, "ExampleVirt")
.def(py::init<int>())
/* Reference original class in function definitions */
.def("run", &Example12::run)
.def("run_bool", &Example12::run_bool)
.def("pure_virtual", &Example12::pure_virtual);
.def("run", &ExampleVirt::run)
.def("run_bool", &ExampleVirt::run_bool)
.def("pure_virtual", &ExampleVirt::pure_virtual);
m.def("runExample12", &runExample12);
m.def("runExample12Bool", &runExample12Bool);
m.def("runExample12Virtual", &runExample12Virtual);
m.def("runExampleVirt", &runExampleVirt);
m.def("runExampleVirtBool", &runExampleVirtBool);
m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
}
......@@ -3,34 +3,34 @@ from __future__ import print_function
import sys
sys.path.append('.')
from example import Example12, runExample12, runExample12Virtual, runExample12Bool
from example import ExampleVirt, runExampleVirt, runExampleVirtVirtual, runExampleVirtBool
class ExtendedExample12(Example12):
class ExtendedExampleVirt(ExampleVirt):
def __init__(self, state):
super(ExtendedExample12, self).__init__(state + 1)
super(ExtendedExampleVirt, self).__init__(state + 1)
self.data = "Hello world"
def run(self, value):
print('ExtendedExample12::run(%i), calling parent..' % value)
return super(ExtendedExample12, self).run(value + 1)
print('ExtendedExampleVirt::run(%i), calling parent..' % value)
return super(ExtendedExampleVirt, self).run(value + 1)
def run_bool(self):
print('ExtendedExample12::run_bool()')
print('ExtendedExampleVirt::run_bool()')
return False
def pure_virtual(self):
print('ExtendedExample12::pure_virtual(): %s' % self.data)
print('ExtendedExampleVirt::pure_virtual(): %s' % self.data)
ex12 = Example12(10)
print(runExample12(ex12, 20))
ex12 = ExampleVirt(10)
print(runExampleVirt(ex12, 20))
try:
runExample12Virtual(ex12)
runExampleVirtVirtual(ex12)
except Exception as e:
print("Caught expected exception: " + str(e))
ex12p = ExtendedExample12(10)
print(runExample12(ex12p, 20))
print(runExample12Bool(ex12p))
runExample12Virtual(ex12p)
ex12p = ExtendedExampleVirt(10)
print(runExampleVirt(ex12p, 20))
print(runExampleVirtBool(ex12p))
runExampleVirtVirtual(ex12p)
Constructing ExampleVirt..
Original implementation of ExampleVirt::run(state=10, value=20)
30
Caught expected exception: Tried to call pure virtual function "ExampleVirt::pure_virtual"
Constructing ExampleVirt..
ExtendedExampleVirt::run(20), calling parent..
Original implementation of ExampleVirt::run(state=11, value=21)
32
ExtendedExampleVirt::run_bool()
False
ExtendedExampleVirt::pure_virtual(): Hello world
Destructing ExampleVirt..
Destructing ExampleVirt..
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment