博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python使用pickle,json等序列化dict
阅读量:5367 次
发布时间:2019-06-15

本文共 2926 字,大约阅读时间需要 9 分钟。

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())

转载于:https://www.cnblogs.com/DjangoBlog/p/6679603.html

你可能感兴趣的文章
JS定义类或函数
查看>>
[ English ] Ping sb.
查看>>
牛客网暑期多校1
查看>>
Python 必选参数,默认参数,可变参数,关键字参数和命名关键字参数
查看>>
程序员的自我修养-链接、装载与库-2 静态链接的过程
查看>>
优秀作业 http://note.youdao.com/noteshare?id=c3ab264c5be57ca81f7f34f16075af09&sub=0C38120BA0C447B386...
查看>>
Yii2 数据操作DAO
查看>>
SQL Server中的事务与锁
查看>>
《AngularJS高级程序设计》学习笔记
查看>>
Current State of Industry Mining and Construction One
查看>>
scp 从远程服务器上一下载文件
查看>>
Java的post请求-----接口测试
查看>>
转:电容的ESR是什么意思
查看>>
杭电1061 Rightmost Digit
查看>>
mvc 5 的过滤器和webapi 过滤器 对应实现的action过滤器区别
查看>>
but was actually of type [com.sun.proxy.$Proxy33]
查看>>
搭建事务管理转账案例的环境(强调:简化开发,以后DAO可以继承JdbcDaoSupport类)...
查看>>
github 与git 使用 及配置
查看>>
IIS配置支持跨域请求
查看>>
文件大小限制
查看>>