From 8ae3dda6a6c89758f90ae957aa9280554c174f32 Mon Sep 17 00:00:00 2001 From: Ellon Mendes Date: Mon, 23 Nov 2015 20:56:51 +0100 Subject: [PATCH] Add helper functions to better handle gtsam.Symbol on python --- python/handwritten/inference/Symbol.cpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/python/handwritten/inference/Symbol.cpp b/python/handwritten/inference/Symbol.cpp index ace26c67d..9fc5b74e7 100644 --- a/python/handwritten/inference/Symbol.cpp +++ b/python/handwritten/inference/Symbol.cpp @@ -20,6 +20,8 @@ #define NO_IMPORT_ARRAY #include +#include // for stringstream + #include "gtsam/inference/Symbol.h" using namespace boost::python; @@ -43,6 +45,20 @@ std::string selfToString(const Symbol & self) return (std::string)self; } +// Helper function to convert a Symbol to int using int() cast in python +size_t selfToKey(const Symbol & self) +{ + return self.key(); +} + +// Helper function to recover symbol's unsigned char as string +std::string chrFromSelf(const Symbol & self) +{ + std::stringstream ss; + ss << self.chr(); + return ss.str(); +} + void exportSymbol(){ class_ >("Symbol") @@ -53,7 +69,6 @@ class_ >("Symbol") .def("print", &Symbol::print, print_overloads(args("s"))) .def("equals", &Symbol::equals, equals_overloads(args("q","tol"))) .def("key", &Symbol::key) - .def("chr", &Symbol::chr) .def("index", &Symbol::index) .def(self < self) .def(self == self) @@ -61,6 +76,8 @@ class_ >("Symbol") .def(self != self) .def(self != other()) .def("__repr__", &selfToString) + .def("__int__", &selfToKey) + .def("chr", &chrFromSelf) ; } \ No newline at end of file