Python--ZODB

2022-04-18 00:00:00 数据库 专区 订阅 对象 存放

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

相关文章