Pickle data with a persistent_id to a binary object (dumps and loads)
Question: A first question I asked was [_how to load a pickle object and resolve certain
references_](http://stackoverflow.com/questions/37026745/how-to-load-a-pickle-
object-and-resolve-certain-references). A next problem I'm facing is that I
cannot call
[`dumps`](https://docs.python.org/3/library/pickle.html#pickle.dumps) or
[`loads`](https://docs.python.org/3/library/pickle.html#pickle.loads) objects
to a binary object.

Below is an implementation of the `ContextAwarePickler` and the
`ContextAwareUnpickler`. How can I use these to convert an object to and back
from its binary representation? As far as I know this only works for files.

    
    
    import pickle
    
    class ContextAwarePickler(pickle.Pickler):
        def persistent_id(self, obj):
            # if this is a context, return the key
            if isinstance(obj, Context):
                return ("Context", context.key)
    
            # pickle as usual
            return None
    
    
    class ContextAwareUnpickler(pickle.Unpickler):
        def recover_context(self, key_id):
            ...
    
        def persistent_load(self, pid):
            type_tag, key_id = pid
            if type_tag == "Context":
                return self.recover_context(key_id)
            else:
                raise pickle.UnpicklingError("unsupported persistent object")
    


Answer: Your solution is similar to the one in `dill` (I'm the author) -- but not as
robust.

<https://github.com/uqfoundation/dill/blob/cccbea9b715e16b742288e1e5a21a687a4d4081b/dill/temp.py#L169>
(code snipped reproduced below)

    
    
    def loadIO(buffer, **kwds):
        """load an object that was stored with dill.temp.dumpIO
    
        buffer: buffer object
    
        >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
        >>> dill.temp.loadIO(dumpfile)
        [1, 2, 3, 4, 5]
        """
        import dill as pickle
        if PY3:
            from io import BytesIO as StringIO
        else:
            from StringIO import StringIO
        value = getattr(buffer, 'getvalue', buffer) # value or buffer.getvalue
        if value != buffer: value = value() # buffer.getvalue()
        return pickle.load(StringIO(value))
    
    def dumpIO(object, **kwds):
        """dill.dump of object to a buffer.
    Loads with "dill.temp.loadIO".  Returns the buffer object.
    
        >>> dumpfile = dill.temp.dumpIO([1, 2, 3, 4, 5])
        >>> dill.temp.loadIO(dumpfile)
        [1, 2, 3, 4, 5]
        """
        import dill as pickle
        if PY3:
            from io import BytesIO as StringIO
        else:
            from StringIO import StringIO
        file = StringIO()
        pickle.dump(object, file)
        file.flush()
        return file
    

Note that you may want to be careful about things like to `flush` the buffer
on `dump`, as `dill` does.

