shelve模块
#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
'''
shelve模块的更多相关文章
- python序列化: json & pickle & shelve 模块
一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...
- python pickle 和 shelve模块
pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...
- s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译
时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...
- 小白的Python之路 day5 shelve模块讲解
shelve模块讲解 一.概述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,有什么方法可以向dump多少次就dump多少次,并且load不会出 ...
- python之shelve模块详解
一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...
- Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解
把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...
- os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法
一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...
- json,pickle,shelve模块,xml处理模块
常用模块学习—序列化模块详解 什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. 为什么要序列化? 你打游戏过程 ...
- python的shelve模块
shelve shelve是一额简单的数据存储方案,他只有一个函数就是open(),这个函数接收一个参数就是文件名,并且文件名必须是.bat类型的.然后返回一个shelf对象,你可以用他来存储东西,就 ...
随机推荐
- git: fatal unable to auto-detect email address
参考:http://stackoverflow.com/questions/25671785/git-fatal-unable-to-auto-detect-email-address 正确使用命令: ...
- Tomcat优化总结
一.内存溢出问题 Linux设置启动脚本 [root@LAMP ~]# vi /usr/local/tomcat/bin/catalina.sh #__________________________ ...
- IOSView显示特性设置
一.主要用途 弹出模态ViewController是IOS变成中很有用的一个技术,UIKit提供的一些专门用于模态显示的ViewController,如UIImagePickerController等 ...
- c++学习笔记2--constexpr,类型别名,auto
constexpr:修饰常量表达式,约定修饰对象不仅为常量,而且在编译阶段就能得到值. 与const不同的是,constexpr约束的是当事人,就是说,constexpr int *a=1;这样的语句 ...
- Fragment 与 Fragment 相互传值
方法: 1 接口回调 2 用一个单例类 3 bundle 4 获取fragment实例 , setter getter 传值
- System.MissingMethodException: 找不到方法:
This is a problem which can occur when there is an old version of a DLL still lingering somewhere ar ...
- CUBRID学习笔记 24 数据类型1
---恢复内容开始--- 一 数字类型 注意小数的四舍五入问题 1数字型 Type Bytes Mix Max Exact/approx. SHORTSMALLINT 2 -32,768 32,76 ...
- iOS适配(Masonry)
1.各屏幕大小 设备 尺寸 像素 点 iPhone \ iPhone 3G \ iPhone 3GS 3.5 inch 320 x 480 320 x 480 iPhone 4 \ iPhone 4S ...
- Hibernate之N+1问题
什么是hibernate的N+1问题?先了解这样一个描述: 多个学生可以对应一个老师,所以student(n)---teacher(1).Stu类中有一个属性teacher.在hibernate配置文 ...
- hdu 5925 Coconuts 离散化+dfs
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...