ITPub博客

首页 > 数据库 > > Python--ZODB

Python--ZODB

作者:飘絮絮絮丶 时间:2022-04-18 13:51:17 0 删除 编辑

zodb是python自带的对象数据库,以kv的形式存放对象状态。可以存放简单类型与复杂类型。例子:

from persistent import Persistent
from ZODB import FileStorage, DB
import transaction
from elec_rate_server.data.measurepoint import MeasurePoint
from elec_rate_server.electools.meterpowertype import MeterPowerType

class DescHandler(object):
def __init__(self, info_path):
self.info_path = info_path

def zodb_init(self):
'''数据库初始化'''
self.storage = FileStorage.FileStorage(self.info_path)
self.db = DB(self.storage)
self.connection = self.db.open()
self.dbroot = self.connection.root()

def zodb_finit(self):
'''数据库关闭'''
self.connection.close()
self.db.close()
self.storage.close()

def save_desc_info(self, func_index, users, transgroups, relations):
'''存放对象'''
self.zodb_init()

result_mps = list()
for user in users:
for mp in user.get_measure_pos_list():
result_mps.append(mp)
'''对象可以是list'''
self.dbroot[func_index] = result_mps
transaction.commit()

self.zodb_finit()

def get_desc_info(self):
'''获取对象数据'''
self.zodb_init()

for key in self.dbroot.keys():
proc_results = self.dbroot[key]
if isinstance(proc_results, list):
print(str(key)+' 打印中间结果:')
for mp in proc_results:
if isinstance(mp, MeasurePoint):
print("group_no:", mp.group_no)
if isinstance(mp.get_write_power(), MeterPowerType):
print(" power:", mp.get_write_power().get_e())

self.zodb_finit()
————————————————
版权声明:本文为CSDN博主「Haidi_Elisa」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Haidi_Elisa/article/details/46519669

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946831/viewspace-3123603/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-09-09

  • 博文量
    2646
  • 访问量
    42236