Re: [pygccxml-development] serialization support?
Brought to you by:
mbaas,
roman_yakovenko
From: Roman Y. <rom...@gm...> - 2006-09-13 11:06:38
|
On 9/13/06, Neal Becker <ndb...@gm...> wrote: > The technique I have used is not really elegant, but works. First serialize > with boost::serializatoin into a string, then pickle the string: > > struct mt_pickle_suite : python::pickle_suite { > > static python::object getstate (const rng_t& rng) { > std::ostringstream os; > boost::archive::binary_oarchive oa(os); > oa << rng; > return python::str (os.str()); > } > > static void > setstate(rng_t& rng, python::object entries) { > python::str s = python::extract<python::str> (entries)(); > std::string st = python::extract<std::string> (s)(); > std::istringstream is (st); > > boost::archive::binary_iarchive ia (is); > ia >> rng; > } > }; > > [...] > class_<rng_t> ("rng", "Mersenne Twister", python::init<rng_t::result_type>( > (python::arg ("seed")=4357), > "__init__(seed=4357)\n" > "Construct with optional seed\n" > "@param seed: initial seed\n" > )) > .def_pickle(mt_pickle_suite()) > .def ("getstate", &mt_pickle_suite::getstate) > .def ("setstate", &mt_pickle_suite::setstate) > [...] > Few comments: 1. This implementation will surprize any Python user. You don't preserve references between objects. 2. I think you can make this code to be template on Pickled class. 3. I think this code belongs to goodies. Do you want to work a little bit on it and contribute? ( test, few lines of documentation) from goodies import pickle pickle( module builder, list of classes ) Something like this. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ |