绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Python--ZODB
2022-04-18 13:51:17

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

分享好友

分享这个小栈给你的朋友们,一起进步吧。

ZODB
创建时间:2022-04-18 13:36:36
ZODB
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 飘絮絮絮丶
    专家
戳我,来吐槽~