Created
February 16, 2017 17:19
-
-
Save joealcorn/ac4f5576be1694ccdfd961f7c60be3d4 to your computer and use it in GitHub Desktop.
PicklableFile implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from StringIO import StringIO | |
class UnpickledStringIO(StringIO): | |
def __setstate__(self, state): | |
self.__dict__.update(state) | |
def unpickle_factory(*a, **kw): | |
return UnpickledStringIO(*a, **kw) | |
class PicklableFile(object): | |
''' | |
Wrapper around files that allows them to be pickled. | |
When unpickled, you'll have an UnpickledStringIO | |
instance, which is like a regular StringIO obj | |
except it'll have a name property | |
Be aware of large files! | |
''' | |
def __init__(self, file): | |
self.file = file | |
def __reduce__(self): | |
self.file.seek(0) | |
data = self.file.read() | |
return (unpickle_factory, (data,), { | |
'name': self.file.name | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment