Save an object to the specified file in a backward compatible way for Pybind objects. See: http://pybind11.readthedocs.io/en/stable/advanced/classes.html#pickling-support and https://github.com/pybind/pybind11/issues/271
(obj_to_pickle, file_name)
| 2 | import sys |
| 3 | |
| 4 | def save_pickled_compatible(obj_to_pickle, file_name): |
| 5 | ''' |
| 6 | Save an object to the specified file in a backward compatible |
| 7 | way for Pybind objects. See: |
| 8 | http://pybind11.readthedocs.io/en/stable/advanced/classes.html#pickling-support |
| 9 | and https://github.com/pybind/pybind11/issues/271 |
| 10 | ''' |
| 11 | try: |
| 12 | import cPickle as pickle # Use cPickle on Python 2.7 |
| 13 | except ImportError: |
| 14 | import pickle |
| 15 | data = pickle.dumps(obj_to_pickle, 2) |
| 16 | with open(file_name, "wb") as handle: |
| 17 | handle.write(data) |
| 18 | |
| 19 | def load_pickled_compatible(file_name): |
| 20 | ''' |