python使用pickle,json等序列化dict,picklejson,PersistentDi
python使用pickle,json等序列化dict,picklejson,PersistentDi
PersistentDict从dict继承,实现了持久化。初始化时自动加载文件到内存,关闭时将dict内容序列化到文件,支持csv,json,pickle格式。
import pickle, json, csv, os, shutilclass PersistentDict(dict): ''' Persistent dictionary with an API compatible with shelve and anydbm. The dict is kept in memory, so the dictionary operations run as fast as a regular dictionary. Write to disk is delayed until close or sync (similar to gdbm's fast mode). Input file format is automatically discovered. Output file format is selectable between pickle, json, and csv. All three serialization formats are backed by fast C implementations. ''' def __init__(self, filename, flag='c', mode=None, format='pickle', *args, **kwds): self.flag = flag # r=readonly, c=create, or n=new self.mode = mode # None or an octal triple like 0644 self.format = format # 'csv', 'json', or 'pickle' self.filename = filename if flag != 'n' and os.access(filename, os.R_OK): fileobj = open(filename, 'rb' if format=='pickle' else 'r') with fileobj: self.load(fileobj) dict.__init__(self, *args, **kwds) def sync(self): 'Write dict to disk' if self.flag == 'r': return filename = self.filename tempname = filename + '.tmp' fileobj = open(tempname, 'wb' if self.format=='pickle' else 'w') try: self.dump(fileobj) except Exception: os.remove(tempname) raise finally: fileobj.close() shutil.move(tempname, self.filename) # atomic commit if self.mode is not None: os.chmod(self.filename, self.mode) def close(self): self.sync() def __enter__(self): return self def __exit__(self, *exc_info): self.close() def dump(self, fileobj): if self.format == 'csv': csv.writer(fileobj).writerows(self.items()) elif self.format == 'json': json.dump(self, fileobj, separators=(',', ':')) elif self.format == 'pickle': pickle.dump(dict(self), fileobj, 2) else: raise NotImplementedError('Unknown format: ' + repr(self.format)) def load(self, fileobj): # try formats from most restrictive to least restrictive for loader in (pickle.load, json.load, csv.reader): fileobj.seek(0) try: return self.update(loader(fileobj)) except Exception: pass raise ValueError('File not in a supported format')if __name__ == '__main__': import random # Make and use a persistent dictionary with PersistentDict('/tmp/demo.json', 'c', format='json') as d: print(d, 'start') d['abc'] = '123' d['rand'] = random.randrange(10000) print(d, 'updated') # Show what the file looks like on disk with open('/tmp/demo.json', 'rb') as f: print(f.read())
相关内容
- Python删除指定目录下的过期文件,,import osimp
- python使用atexit在程序退出时清理资源,pythonatexit,当进程
- python读取windows系统信息,python读取系统信息,需要使用
- python在windows下创建隐藏窗口的子进程,python窗口,impo
- python logging.basicConfig 封装,,第一次使用python的
- Python实现简单的缓存和缓存decorator,pythondecorator,一个简
- python解决八皇后算法,python皇后算法,python解决经典算法
- python下载Gmail邮箱中的所有邮件,pythongmail,#!/usr/bin/p
- python管理windows的环境变量,python环境变量,下面的代码
- python webpy中显示进程中的所有类型对象占用的内存大小
评论关闭