[python-win32] Sparse files and Pickle

Paul Moore p.f.moore at gmail.com
Mon Apr 4 20:05:50 CEST 2005


On Apr 4, 2005 5:29 PM, Tim Roberts <timr at probo.com> wrote:
> If I needed to refer to an 89MB disk file in an
> object, I would replace the data with the file name before pickling.  I
> thought pickle recognized a magic method name so the object could "help"
> put itself into a picklable state, but I don't see it now.

You're probably thinking of __setstate__ and __getstate__. Something
like this (untested) should work:

    def __getstate__(self):
        state = {}
        for key in self.__dict__:
            # don't pickle "data"
            if key != 'data':
                state[key] = self.__dict__[key]

    def __setstate__(self, state):
        self.__dict__.update(state)
        # get "data" back from the filesystem
        self.data = open(self.file).read()

There is also a __reduce__ method, and copy_reg.pickle.

Paul.


More information about the Python-win32 mailing list