The question is not too clear; what it looks like:
- you have a third-party module that has cool classes
- these classes may contain links to files, which makes the classes themselves unusable because open files are not matched.
Essentially, you want to make open files picklable. You can do this quite easily, with a few caveats. Here's an incomplete but functional pattern:
import pickle
class PicklableFile(object):
def __init__(self, fileobj):
self.fileobj = fileobj
def __getattr__(self, key):
return getattr(self.fileobj, key)
def __getstate__(self):
ret = self.__dict__.copy()
ret['_file_name'] = self.fileobj.name
ret['_file_mode'] = self.fileobj.mode
ret['_file_pos'] = self.fileobj.tell()
del ret['fileobj']
return ret
def __setstate__(self, dict):
self.fileobj = open(dict['_file_name'], dict['_file_mode'])
self.fileobj.seek(dict['_file_pos'])
del dict['_file_name']
del dict['_file_mode']
del dict['_file_pos']
self.__dict__.update(dict)
f = PicklableFile(open("/tmp/blah"))
print f.readline()
data = pickle.dumps(f)
f2 = pickle.loads(data)
print f2.read()
Cautions and notes, some obvious, some less:
- ,
open. - , gzip.GzipFile, , . , file. - , .
- , .
- ('w +'), , ; , , . - , - .
- , IOError; , , .
- Python 2 Python 3 ;
file Python 3. Python 2 , file.
; , , , . , .