def writeStr(self, fmt=None, **keywords): ''' Convert the object to a pickled/jsonpickled string and return the string ''' fmt = self.parseWriteFmt(fmt) storage = self.packStream(self.stream) if fmt == 'pickle': out = pickle.dumps(storage, protocol=-1) elif fmt == 'jsonpickle': out = jsonpickle.encode(storage, **keywords) else: raise FreezeThawException('bad StreamFreezer format: %s' % fmt) #self.teardownStream(self.stream) return out
def writeStr(self, fmt=None, **keywords): ''' Convert the object to a pickled/jsonpickled string and return the string ''' fmt = self.parseWriteFmt(fmt) storage = self.packStream(self.stream) if fmt == 'pickle': out = pickle.dumps(storage, protocol=-1) elif fmt == 'jsonpickle': out = jsonpickle.encode(storage, **keywords) else: raise FreezeThawException('bad StreamFreezer format: %s' % fmt) #self.teardownStream(self.stream) return out
def write(self, fmt='pickle', fp=None, zipType=None, **keywords): ''' For a supplied Stream, write a serialized version to disk in either 'pickle' or 'jsonpickle' format and return the filepath to the file. jsonpickle is the better format for transporting from one computer to another, but slower and may have some bugs. If zipType == 'zlib' then zlib compression is done after serializing. No other compression types are currently supported. ''' if zipType not in (None, 'zlib'): raise FreezeThawException('Cannot zip files except zlib...') fmt = self.parseWriteFmt(fmt) if fp is None: directory = environLocal.getRootTempDir() if fmt.startswith('json'): fp = self.getJsonFp(directory) else: fp = self.getPickleFp(directory) else: if not isinstance(fp, pathlib.Path): fp = pathlib.Path(fp) if not fp.is_absolute(): # assume its a complete path fp = environLocal.getRootTempDir() / fp storage = self.packStream(self.stream) environLocal.printDebug(['writing fp', str(fp)]) if fmt == 'pickle': # a negative protocol value will get the highest protocol; # this is generally desirable # packStream() returns a storage dictionary pickleString = pickle.dumps(storage, protocol=pickle.HIGHEST_PROTOCOL) if zipType == 'zlib': pickleString = zlib.compress(pickleString) if isinstance(fp, pathlib.Path): fp = str(fp) with open(fp, 'wb') as f: # binary f.write(pickleString) elif fmt == 'jsonpickle': data = jsonpickle.encode(storage, **keywords) if zipType == 'zlib': data = zlib.compress(data) if isinstance(fp, pathlib.Path): fp = str(fp) with open(fp, 'w') as f: f.write(data) else: raise FreezeThawException('bad StreamFreezer format: %s' % fmt) ## must restore the passed-in Stream #self.teardownStream(self.stream) return fp
def write(self, fmt='pickle', fp=None, zipType=None, **keywords): ''' For a supplied Stream, write a serialized version to disk in either 'pickle' or 'jsonpickle' format and return the filepath to the file. jsonpickle is the better format for transporting from one computer to another, but slower and may have some bugs. If zipType == 'zlib' then zlib compression is done after serializing. No other compression types are currently supported. ''' if zipType not in (None, 'zlib'): raise FreezeThawException('Cannot zip files except zlib...') fmt = self.parseWriteFmt(fmt) if fp is None: directory = environLocal.getRootTempDir() if fmt.startswith('json'): fp = self.getJsonFp(directory) else: fp = self.getPickleFp(directory) else: if not isinstance(fp, pathlib.Path): fp = pathlib.Path(fp) if not fp.is_absolute(): # assume its a complete path fp = environLocal.getRootTempDir() / fp storage = self.packStream(self.stream) environLocal.printDebug(['writing fp', str(fp)]) if fmt == 'pickle': # a negative protocol value will get the highest protocol; # this is generally desirable # packStream() returns a storage dictionary pickleString = pickle.dumps(storage, protocol=pickle.HIGHEST_PROTOCOL) if zipType == 'zlib': pickleString = zlib.compress(pickleString) if isinstance(fp, pathlib.Path): fp = str(fp) with open(fp, 'wb') as f: # binary f.write(pickleString) elif fmt == 'jsonpickle': data = jsonpickle.encode(storage, **keywords) if zipType == 'zlib': data = zlib.compress(data) if isinstance(fp, pathlib.Path): fp = str(fp) with open(fp, 'w') as f: f.write(data) else: raise FreezeThawException('bad StreamFreezer format: %s' % fmt) ## must restore the passed-in Stream #self.teardownStream(self.stream) return fp