shelve模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #coding:utf-8 __author__ = 'similarface' #email:similarface@outlook.com ''' shelve模块: 映射容器 存储对象,被存储的对象都会被序列化并且被写入文件 反序列化然后从文件获取任意对象 method:shelve.open() 'r' Open existing database for reading only (default) 'w' Open existing database for reading and writing 'c' Open database for reading and writing, creating it if it doesn’t exist 'n' Always create a new, empty database, open for reading and writing ''' import shelve import logging logging.basicConfig(level = logging.DEBUG) console = logging.StreamHandler() logging.getLogger('').addHandler(console) class Person: ''' 定义个简单的类 ''' def __init__( self ,name, * posts): self .name = name def as_dict( self ): return dict (name = self .name,underline = "=" * len ( self .name),) p1 = Person(name = 'Tom' ) #创建shelf对象 shelf = shelve. open ( 'person' ) #设置键 p1._id = 'person:1' #存储 shelf[p1._id] = p1 #获取 p = shelf[p1._id] logging.info(p) logging.info(p.name) logging.info(p._id) logging.info( list (shelf.keys())) shelf.close() #获取存储对象 shelf = shelve. open ( 'person' ) #获取指定的对象 根据类和属性来获取 results = (shelf[k] for k in shelf.keys() if k.startswith( 'person:' ) and shelf[k].name = = 'Tom' ) #logging.info(list(results)) for i in results: r0 = i logging.info(r0._id) logging.info(r0.name) ''' INFO:root:<__main__.Person instance at 0x00000000026DBAC8> <__main__.Person instance at 0x00000000026DBAC8> INFO:root:Tom Tom INFO:root:person:1 person:1 INFO:root:['person:1'] ['person:1'] INFO:root:person:1 person:1 INFO:root:Tom Tom ''' ''' 存在继承的对象的持久化 使用外键引用对象 ''' import datetime class Teacher: def __init__( self ,profession,name,desc,tags): self .profession = profession self .name = name self .desc = desc self .tags = tags def as_dict( self ): return dict ( profession = self .profession, name = self .name, underline = "-" * len ( self .name), desc = self .desc, tags = " " .join( self .tags) ) t1 = Teacher( 'math' , 'Lucy' , 'beauifu teacher' ,[ 'senior' ]) t2 = Teacher( 'english' , 'Tom' , 'Stronger' ,[ 'lower' ]) import shelve shelf = shelve. open ( 'person' ) owner = shelf[ 'person:1' ] t1._parent = owner._id t1._id = t1._parent + ':teacher:1' shelf[t1._id] = t1 t2._parent = owner._id t2._id = t2._parent + ':teachar:2' shelf[t2._id] = t2 logging.info( list (shelf.keys())) logging.info(t1._parent) logging.info(t2._id) ''' INFO:root:['person:1:teachar:2', 'person:1', 'person:1:teacher:1'] ['person:1:teachar:2', 'person:1', 'person:1:teacher:1'] INFO:root:person:1 person:1 INFO:root:person:1:teachar:2 person:1:teachar:2 ''' |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· ASP.NET Core - 日志记录系统(二)
· .NET 依赖注入中的 Captive Dependency
· .NET Core 对象分配(Alloc)底层原理浅谈
· 聊一聊 C#异步 任务延续的三种底层玩法
· 敏捷开发:如何高效开每日站会
· 终于决定:把自己家的能源管理系统开源了!
· C#实现 Winform 程序在系统托盘显示图标 & 开机自启动
· 了解 ASP.NET Core 中的中间件
· 实现windows下简单的自动化窗口管理
· 【C语言学习】——命令行编译运行 C 语言程序的完整流程