Guest User

Untitled

a guest
Feb 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <boost/python.hpp>
  2. #include <boost/python/module.hpp>
  3. #include <boost/python/def.hpp>
  4. #include <boost/python/class.hpp>
  5. #include <boost/python/tuple.hpp>
  6. #include <boost/python/extract.hpp>
  7. #include "class_name.hpp"
  8.  
  9. struct class_name_suite : boost::python::pickle_suite
  10. {
  11. //static
  12. //boost::python::tuple
  13. //getinitargs(const ipr::pcap_input& w)
  14. //{
  15. // return boost::python::make_tuple(w.id);
  16. //}
  17.  
  18.  
  19. static
  20. boost::python::tuple getstate(boost::python::object w_obj)
  21. {
  22. class_name const& w = boost::python::extract<class_name const&>(w_obj)();
  23. return boost::python::make_tuple(
  24. w_obj.attr("__dict__") //If the python object has other attributes, they will be stored in the dict
  25. , w.id
  26. , w.status);
  27. }
  28. static
  29. void
  30. setstate(boost::python::object w_obj, boost::python::tuple state)
  31. {
  32. using namespace boost::python;
  33. class_name& w = extract<class_name&>(w_obj)();
  34. // restore the object's __dict__
  35. dict d = extract<dict>(w_obj.attr("__dict__"))();
  36. d.update(state[0]);
  37. w.id = extract<std::string>(state[1]);
  38. w.status = extract<std::string>(state[2]);
  39. }
  40. static bool getstate_manages_dict() { return true; }
  41. };
  42.  
  43. //exporting the c++ class
  44. #include <boost/python.hpp>
  45.  
  46. class_<class_name, boost::noncopyable>("class_name")
  47. .enable_pickling()
  48. .def_pickle(class_name_suite())
  49. .def("get_tag", &class_name::get_tag)
  50.  
  51. //sample test
  52. @nose.tools.with_setup(setUp)
  53. def test_persist_class_name(self):
  54. from my_classes import class_name
  55. input = class_name.create()
  56. input.id = "Test_Source"
  57. input.status = 'Completed'
  58.  
  59. import pickle
  60. pickled1 = pickle.dumps(input, 0)
  61. pickled2 = pickle.dumps(input, 1)
  62. pickled3 = pickle.dumps(input, 2)
  63. unpickled = pickle.loads(pickled)
  64. self.assertEqual(unpickled.id, input.id)
  65. self.assertEqual(unpickled.status, input.status)
Add Comment
Please, Sign In to add comment