#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模块的更多相关文章

  1. python序列化: json & pickle & shelve 模块

    一.json & pickle & shelve 模块 json,用于字符串 和 python数据类型间进行转换pickle,用于python特有的类型 和 python的数据类型间进 ...

  2. python pickle 和 shelve模块

    pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧 1.pickle 写: 以写方式打开一个文件描述符,调用pickle.dump把对象写进去 dn = {'b ...

  3. 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时间 ...

  4. 小白的Python之路 day5 shelve模块讲解

    shelve模块讲解 一.概述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,有什么方法可以向dump多少次就dump多少次,并且load不会出 ...

  5. python之shelve模块详解

    一.定义 Shelve是对象持久化保存方法,将对象保存到文件里面,缺省(即默认)的数据存储文件是二进制的. 二.用途 可以作为一个简单的数据存储方案. 三.用法 使用时,只需要使用open函数获取一个 ...

  6. Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解

    把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...

  7. os常用模块,json,pickle,shelve模块,正则表达式(实现运算符分离),logging模块,配置模块,路径叠加,哈希算法

    一.os常用模块 显示当前工作目录 print(os.getcwd()) 返回上一层目录 os.chdir("..") 创建文件包 os.makedirs('python2/bin ...

  8. json,pickle,shelve模块,xml处理模块

    常用模块学习—序列化模块详解 什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. 为什么要序列化? 你打游戏过程 ...

  9. python的shelve模块

    shelve shelve是一额简单的数据存储方案,他只有一个函数就是open(),这个函数接收一个参数就是文件名,并且文件名必须是.bat类型的.然后返回一个shelf对象,你可以用他来存储东西,就 ...

随机推荐

  1. 线程池原理及创建(C++实现)

    http://www.cnblogs.com/lidabo/p/3328402.html 本文给出了一个通用的线程池框架,该框架将与线程执行相关的任务进行了高层次的抽象,使之与具体的执行任务无关.另外 ...

  2. 窗体移动API

    //窗体移动API [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport ...

  3. 6-JS函数(二)

    函数 函数的实参和形参 function temp (a,b){ console.log(a) }; // a , b为两个形参 var a1 = 2; var b1 = 3; temp(a1,b1) ...

  4. 【leetcode❤python】66. Plus One

    class Solution:      # @param digits, a list of integer digits      # @return a list of integer digi ...

  5. Create Timer Example To Show Image Presentation in Oracle Forms

    Suppose you want to change multiple images after a specified time in home screen of your oracle form ...

  6. Create Stacked Canvas to Scroll Horizontal Tabular Data Blocks In Oracle Forms

    In this tutorial you will learn to create horizontal scrollable tabular or detail data block by usin ...

  7. [51NOD1007] 正整数分组(DP,记忆化搜索)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1007 dp(id, s)表示第id个数之前,其中一个集合和为s ...

  8. jquery初涉,First Blood

    jquery可以帮助干的事情有: 遍历HTML文档 操作DOM 处理事件 执行动画 开发Ajax操作 优点就不在这儿扯蛋了~ 1.jquery环境配置 jquery不需要安装,只需要将下载的jquer ...

  9. ajax请求、servlet返回json数据

    ajax请求.servlet返回json数据 1.方式一 response.setcontenttype("text/html;charset=utf-8"); response. ...

  10. [SAP ABAP开发技术总结]SD销售订单定价过程

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...