Pickled my dictionary from ZODB but i got a less in size one?
Posted
by
Someone Someoneelse
on Stack Overflow
See other posts from Stack Overflow
or by Someone Someoneelse
Published on 2012-06-24T15:48:59Z
Indexed on
2012/06/30
3:16 UTC
Read the original article
Hit count: 164
I use ZODB and i want to copy my 'database_1.fs'
file to another 'database_2.fs'
,
so I opened the root dictionary of that 'database_1.fs'
and I (pickle.dump
) it in a text file.
Then I (pickle.load
) it in a dictionary-variable, in the end I update the root dictionary of the other 'database_2.fs'
with the dictionary-variable.
It works, but I wonder why the size of the 'database_1.fs'
not equal to the size of the other 'database_2.fs'
.
They are still copies of each other.
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()
then that's what i do:-
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
Note:
(1) there are persistent dictionaries and lists in the original database_1.fs
(2) both of them have the same length and the same indexes.
© Stack Overflow or respective owner