把我的字典从ZODB弄来,但我有一本小一点的?

2024-05-06 22:46:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用ZODB,我想把我的'database_1.fs'文件复制到另一个'database_2.fs', 所以我打开了'database_1.fs'的根字典,我(pickle.dump)把它放在一个文本文件中。你知道吗

然后我(pickle.load)把它放在一个dictionary变量中,最后用dictionary变量更新另一个'database_2.fs'的根字典。你知道吗

它是有效的,但我想知道为什么'database_1.fs'的大小不等于另一个'database_2.fs'的大小。你知道吗

它们仍然是彼此的复制品。你知道吗

def openstorage(store):             #opens the database
    data={}
    data['file']=filestorage
    data['db']=DB(data['file'])
    data['conn']=data['db'].open()
    data['root']=data['conn'].root()
    return data

def getroot(dicty):
    return dicty['root']

def closestorage(dicty):              #close the database after Saving
    transaction.commit()
    dicty['file'].close()
    dicty['db'].close()
    dicty['conn'].close()
    transaction.get().abort()

那我就是这么做的:-

import pickle

loc1='G:\\database_1.fs'
op1=openstorage(loc1)
root1=getroot(op1)

loc2='G:database_2.fs'
op2=openstorage(loc2)
root2=getroot(op2)

>>> len(root1)
215
>>> len(root2)
0

pickle.dump( root1, open( "save.txt", "wb" ))
item=pickle.load( open( "save.txt", "rb" ) )          #now item is a dictionary

root2.update(item)

closestorage(op1)
closestorage(op2)

#after I open both of the databases
#I get the same keys in both databases
#But `database_2.fs`  is smaller that `database_2.fs` in size I mean.

>>> len(root2)==len(root1)==215      #they have the same keys 
True

注:

(1)原始database_1.fs中有持久的字典和列表

(2)两者长度相同,指标相同。你知道吗


Tags: theclosedatadictionarylen字典defopen
1条回答
网友
1楼 · 发布于 2024-05-06 22:46:00

在google之后,我发现由ZODB生成的任何data.fs实际上都存储了一些关于对象的旧副本的信息,允许ZODB提供对象撤消功能和多版本并发控制。 因此,为了解决这种行为,实际上可以使用pack方法。 打包存储意味着删除未使用的对象。你知道吗

无论如何谢谢你

相关问题 更多 >