Skip to content

Commit

Permalink
Bump meta and pybind11 deps; improve parser::visitor bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
skystrife committed Mar 28, 2017
1 parent 4defd76 commit 14e01fd
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 10 deletions.
2 changes: 1 addition & 1 deletion deps/pybind11
Submodule pybind11 updated 65 files
+11 −5 .appveyor.yml
+3 −2 .readthedocs.yml
+60 −17 .travis.yml
+6 −18 CMakeLists.txt
+5 −4 README.md
+288 −28 docs/advanced/cast/eigen.rst
+6 −4 docs/advanced/cast/stl.rst
+13 −15 docs/advanced/classes.rst
+8 −3 docs/advanced/functions.rst
+14 −0 docs/advanced/misc.rst
+72 −0 docs/advanced/pycpp/numpy.rst
+90 −3 docs/changelog.rst
+6 −0 docs/classes.rst
+3 −3 docs/conf.py
+0 −10 docs/environment.yml
+1 −0 docs/requirements.txt
+18 −8 include/pybind11/attr.h
+43 −5 include/pybind11/cast.h
+5 −3 include/pybind11/chrono.h
+504 −0 include/pybind11/class_support.h
+78 −33 include/pybind11/common.h
+447 −99 include/pybind11/eigen.h
+5 −5 include/pybind11/functional.h
+293 −66 include/pybind11/numpy.h
+150 −398 include/pybind11/pybind11.h
+218 −44 include/pybind11/pytypes.h
+75 −29 include/pybind11/stl_bind.h
+1 −1 pybind11/_version.py
+1 −0 setup.py
+85 −11 tests/CMakeLists.txt
+3 −3 tests/conftest.py
+7 −0 tests/pytest.ini
+2 −2 tests/test_buffers.py
+33 −0 tests/test_callbacks.cpp
+6 −0 tests/test_callbacks.py
+6 −0 tests/test_chrono.cpp
+5 −1 tests/test_chrono.py
+4 −0 tests/test_cmake_build/installed_target/CMakeLists.txt
+9 −0 tests/test_docstring_options.cpp
+11 −1 tests/test_docstring_options.py
+232 −72 tests/test_eigen.cpp
+524 −43 tests/test_eigen.py
+9 −0 tests/test_enum.py
+6 −0 tests/test_inheritance.cpp
+5 −1 tests/test_inheritance.py
+15 −13 tests/test_kwargs_and_defaults.py
+10 −3 tests/test_methods_and_attributes.cpp
+46 −2 tests/test_methods_and_attributes.py
+8 −0 tests/test_modules.py
+85 −6 tests/test_multiple_inheritance.cpp
+49 −0 tests/test_multiple_inheritance.py
+114 −0 tests/test_numpy_array.cpp
+132 −27 tests/test_numpy_array.py
+18 −0 tests/test_numpy_dtypes.cpp
+22 −15 tests/test_numpy_dtypes.py
+17 −0 tests/test_numpy_vectorize.cpp
+89 −4 tests/test_numpy_vectorize.py
+25 −1 tests/test_python_types.cpp
+33 −1 tests/test_python_types.py
+80 −1 tests/test_sequences_and_iterators.cpp
+38 −3 tests/test_sequences_and_iterators.py
+6 −0 tests/test_smart_ptr.cpp
+9 −0 tests/test_smart_ptr.py
+30 −2 tests/test_stl_binders.cpp
+58 −0 tests/test_stl_binders.py
1 change: 1 addition & 0 deletions src/metapy_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <mutex>

#include "metapy_analyzers.h"
#include "metapy_identifiers.h"
#include "metapy_probe_map.h"

#include "cpptoml.h"
Expand Down
1 change: 1 addition & 0 deletions src/metapy_embeddings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "cpptoml.h"
#include "meta/embeddings/word_embeddings.h"
#include "metapy_embeddings.h"
#include "metapy_identifiers.h"

namespace py = pybind11;
using namespace meta;
Expand Down
1 change: 1 addition & 0 deletions src/metapy_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "metapy_identifiers.h"
#include "metapy_index.h"

#include "cpptoml.h"
Expand Down
38 changes: 32 additions & 6 deletions src/metapy_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "meta/parser/io/ptb_reader.h"

#include "metapy_identifiers.h"
#include "metapy_parser.h"

namespace py = pybind11;
Expand Down Expand Up @@ -143,6 +144,7 @@ void metapy_bind_parser(py::module& m)

py::class_<leaf_node, node>{m_parse, "LeafNode"}
.def(py::init<class_label, std::string>())
.def(py::init<const leaf_node&>())
.def("word", [](const leaf_node& ln) { return *ln.word(); });

py::class_<internal_node, node>{m_parse, "InternalNode"}
Expand Down Expand Up @@ -172,9 +174,28 @@ void metapy_bind_parser(py::module& m)
[](internal_node& n, const node* descendent) {
n.head_constituent(descendent);
})
.def("each_child", [](internal_node& n, std::function<void(node*)> fn) {
n.each_child(fn);
});
// weirdness: need to ensure that the child nodes passed down to
// the python lambda preserve the lifetime of the current internal
// node in case they are stored internally
.def("each_child",
[](internal_node& n, py::function fn) {
n.each_child([&](node* child) {
auto handle = py::cast(
*child, py::return_value_policy::reference_internal,
py::cast(n));
fn(handle);
});
})
.def("__getitem__",
[](internal_node& n, int64_t offset) {
uint64_t idx = offset >= 0 ? static_cast<uint64_t>(offset)
: n.num_children() + offset;
if (idx >= n.num_children())
throw py::index_error();
return n.child(idx);
},
py::keep_alive<0, 1>())
.def("__len__", &internal_node::num_children);

py::class_<parse_tree>{m_parse, "ParseTree"}
.def("__init__",
Expand All @@ -194,9 +215,14 @@ void metapy_bind_parser(py::module& m)
tree.pretty_print(ss);
return ss.str();
})
.def("visit", [](parse_tree& tree, parser::visitor<py::object>& vtor) {
return tree.visit(vtor);
});
// keep_alive here is to ensure that the visitor keeps the tree
// alive as long as it is still referenced, since it might hold an
// internal pointer into the tree
.def("visit",
[](parse_tree& tree, parser::visitor<py::object>& vtor) {
return tree.visit(vtor);
},
py::keep_alive<2, 1>());

py::implicitly_convertible<node, parse_tree>();

Expand Down
13 changes: 11 additions & 2 deletions src/metapy_sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "meta/sequence/perceptron.h"
#include "meta/sequence/sequence.h"

#include "metapy_identifiers.h"
#include "metapy_sequence.h"

namespace py = pybind11;
using namespace meta;

Expand Down Expand Up @@ -56,14 +59,20 @@ void metapy_bind_sequence(py::module& m)
.def("add_observation", &sequence::sequence::add_observation)
.def("add_symbol", &sequence::sequence::add_symbol)
.def("__getitem__",
[](sequence::sequence& seq, sequence::sequence::size_type idx) {
[](sequence::sequence& seq, int64_t offset) {
std::size_t idx = offset >= 0
? static_cast<std::size_t>(offset)
: seq.size() + offset;
if (idx >= seq.size())
throw py::index_error();
return seq[idx];
})
.def("__setitem__",
[](sequence::sequence& seq, sequence::sequence::size_type idx,
[](sequence::sequence& seq, int64_t offset,
sequence::observation obs) {
std::size_t idx = offset >= 0
? static_cast<std::size_t>(offset)
: seq.size() + offset;
if (idx >= seq.size())
throw py::index_error();
seq[idx] = std::move(obs);
Expand Down

0 comments on commit 14e01fd

Please sign in to comment.