Created
March 21, 2012 17:36
-
-
Save tomthorogood/2149971 to your computer and use it in GitHub Desktop.
Instantiating and calling Python classes from using Boost.Python within C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // I had the worst time in the world trying to determine how to do this. | |
| // I did not find the answer in one concise resource. I figured I'd make this available | |
| // to anyone else having the same trouble. | |
| #include <usr/include/boost/python.hpp> // use your path here | |
| #include <iostream> | |
| #include <string> | |
| namespace py = boost::python; | |
| int main() | |
| { | |
| Py_Initialize(); | |
| py::object | |
| mRedis, // the Redis module | |
| iRedis, // the Redis instance | |
| vRedis, // the redis return value; | |
| std::string cString; // to hold our string that we extract from redis... | |
| // The following two lines are the equivalent of: | |
| // import redis | |
| // iRedis = redis.Redis() | |
| mRedis = py::import("redis"); | |
| iRedis = mRedis.attr("Redis")(); | |
| // Then, we're ready to roll! Let's set and get values with redis: | |
| iRedis.attr("set")("my.namespace.key", "Hello, Redis & C++ & Python!"); | |
| vRedis = iRedis.attr("get")("my.namespace.key"); | |
| cString = py::extract<std::string>(vRedis); | |
| std::cout << cString << std::endl; //displays "Hello, Redis & C++ & Python!"); | |
| return 0; | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The reason why I chose the Redis interface is because the c++ redis interface is underutilized, under documented, and not updated.The original problem I was trying to solve was to do explicitly this!