Skip to content

Instantly share code, notes, and snippets.

@joealcorn
Created February 16, 2017 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joealcorn/ac4f5576be1694ccdfd961f7c60be3d4 to your computer and use it in GitHub Desktop.
Save joealcorn/ac4f5576be1694ccdfd961f7c60be3d4 to your computer and use it in GitHub Desktop.
PicklableFile implementation
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