Last active
August 29, 2015 14:21
-
-
Save alfredplpl/328c6038c17dd063d296 to your computer and use it in GitHub Desktop.
自動で圧縮してくれるPickleです。今までのコードに「from cmpPickle import CompressedPickle as pickle」と最初に書くと、そのまま使えます。Auto compressing pickle.
This file contains 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
# -*- coding: utf-8 -*- | |
# This code is distributed under the 3-Clause BSD license (New BSD license). | |
# 日本の方へ | |
# 基本的に作者の名前を書いていただければ、商用利用も可能です。なお、保証はいたしかねます。 | |
# 参考URL: http://osdn.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license | |
# References: https://docs.python.org/3/library/gzip.html | |
# http://henrysmac.org/blog/2010/3/15/python-pickle-example-including-gzip-for-compression.html | |
__author__ = 'alfredplpl' | |
import cPickle as pickle | |
import gzip | |
import os | |
class CompressedPickle: | |
#simplified methods | |
@staticmethod | |
def loadFromPath(filePath): | |
with gzip.open(filePath,"rb") as f: | |
obj=pickle.load(f) | |
return obj | |
@staticmethod | |
def dumpToPath(obj,filePath): | |
with gzip.open(filePath,"wb") as f: | |
pickle.dump(obj,f,pickle.HIGHEST_PROTOCOL) | |
#compatible methods | |
@staticmethod | |
def load(file): | |
path=file.name | |
file.close() | |
print path | |
with gzip.open(path) as f: | |
obj=pickle.load(f) | |
return obj | |
@staticmethod | |
def dump(obj,file,protocol=0): | |
path=file.name | |
file.close() | |
print path | |
with gzip.open(path,"wb") as f: | |
pickle.dump(obj,f,protocol=protocol) | |
return None | |
#tools | |
@staticmethod | |
def convertPickleIntoCompressed(inputPath,outputPath): | |
with open(inputPath,"r") as f: | |
obj=pickle.load(f) | |
CompressedPickle.dumpFromPath(obj,outputPath) | |
# Copyright (c) 2015, alfredplpl | |
# All rights reserved. |
This file contains 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
# -*- coding: utf-8 -*- | |
# This code is distributed under the 3-Clause BSD license (New BSD license). | |
# 基本的に作者の名前を書いていただければ、商用利用も可能です。なお、保証はいたしかねます。 | |
# 参考URL: http://osdn.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license | |
import cPickle as pickle | |
import gzip | |
import os | |
#Sample code | |
if __name__ == "__main__": | |
data=range(100000) | |
print data[1:10] | |
#ordinary pickle | |
with open("./tmp.pkl","w") as f: | |
pickle.dump(data,f) | |
with open("./tmp.pkl") as f: | |
rdata=pickle.load(f) | |
print rdata[1:10] | |
###important### | |
from cmpPickle import CompressedPickle as pickle | |
#compatible type | |
with open("./tmp.pkl.gzip","w") as f: | |
pickle.dump(data,f) | |
with open("./tmp.pkl.gzip") as f: | |
rdata=pickle.load(f) | |
print rdata[1:10] | |
#simplified pickle | |
pickle.dumpToPath(data,"./tmp2.pkl.gzip") | |
rdata=pickle.loadFromPath("./tmp2.pkl.gzip") | |
print rdata[1:10] | |
__author__ = 'alfredplpl' | |
# Copyright (c) 2015, alfredplpl | |
# All rights reserved. | |
# References: https://docs.python.org/3/library/gzip.html | |
# http://henrysmac.org/blog/2010/3/15/python-pickle-example-including-gzip-for-compression.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment