473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Pickling object inherited from dict

Hi!

If I pickle a class which inherits from dict,
I don't get the same data again, if I unpickle
it.

def test_pickle():
home="..."
server=WorkflowServer(home)
server.save()
print server.users.items() # 1
unpickle=load(home)
print unpickle.users.items() #2

def load(home):
file=os.path.join(home, "data.pickle")
fd=open(file)
server=pickle.load(fd)
fd.close()
assert(server.__class__==WorkflowServer)
return server

WorkflowServer:
def save(self):
file=os.path.join(self.home, "data.pickle")
fd=open(file, "w")
pickle.dump(self, fd)
fd.close()

class User(dict):
....

class UserManagemente(dict):
.... # server.users

If I run test_pickle() I get this result: (See #1 and #2)
[('EGSADMIN', {}), ('TEST', {})]
[('EGSADMIN', {}), ('TEST', (<class '__main__.UserManagement'>,
<type 'dict'>, {'EGSADMIN': {}, 'TEST': {}}))]

Why are the two lines different? Sorry, I was not able
to make a smaller working example today.

thomas
Python 2.2.2
Jul 18 '05 #1
1 1984
Am Mon, 27 Oct 2003 17:19:08 +0100 schrieb Thomas Guettler:
Hi!

If I pickle a class which inherits from dict,
I don't get the same data again, if I unpickle
it.


Here is a executable example:
import pickle

class MyDictContainer(dict):
def __init__(self):
dict.__init__(self)
print "MyDictContainer.__init__"

class MyDict(dict):
def __init__(self, root, name):
dict.__init__(self)
self.root=root
self.name=name

container=MyDictContainer()
for name in ["one", "two", "three"]:
mydict=MyDict(container, name)
container[name]=mydict

print container
file="data.pickle"
fd=open(file, "w")
pickle.dump(container, fd)
fd.close()

fd=open(file)
unpickle=pickle.load(fd)
fd.close()
print unpickle
Output:

===> python test-pickle.py
MyDictContainer.__init__
{'three': {}, 'two': {}, 'one': {}}
{'one': {},
'three': (<class '__main__.MyDictContainer'>,
<type 'dict'>, {'three': {},
'two': (<class '__main__.MyDictContainer'>, <type 'dict'>,
{'three': {}, 'two': {}, 'one': {}}), 'one': {}}), 'two':
(<class '__main__.MyDictContainer'>, <type 'dict'>,
{'three': {}, 'two': {}, 'one': {}})}

After unpickling the object is not the same anymore.
Any hints?

thomas

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Guettler | last post by:
Hi! After unpickling the objects are not the same any more. Is this a bug or feature? import pickle class MyDictContainer(dict): def __init__(self): dict.__init__(self)
3
by: Nicolas Fleury | last post by:
Hi, Does anyone know if arrays would be picklable in python 2.4? Until then, I tried to derive from array.array and add __setstate__ and __getstate__ with the following code, but it seems I'm not...
1
by: Edward Loper | last post by:
I'm having trouble pickling subclasses of dict when they contain cycles. In particular: >>> import pickle >>> class D(dict): pass >>> d = D() >>> d = d # add a cycle. >>> print d {1: {...}}...
1
by: fedor | last post by:
Hi all, happy new year, I was trying to pickle a instance of a subclass of a tuple when I ran into a problem. Pickling doesn't work with HIGHEST_PROTOCOL. How should I rewrite my class so I can...
2
by: Kirk Strauser | last post by:
I have a module that defines a Search class and a SearchResult class. I use these classes by writing other modules that subclass both of them as needed to interface with particular search engines....
1
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to...
9
by: Alex | last post by:
I have a serious problem and I hope there is some solution. It is easier to illustrate with a simple code: >>> class Parent(object): __slots__= def __init__(self, a, b): self.A=a; self.B=b...
2
by: Peter Bengtsson | last post by:
Hi, I'm trying to pickle an object instance of a class that is like a dict but with a __getattr__ and I'm getting pickling errors. This works but is not good enough. $ python2.4 .... pass...
1
by: Brian Blais | last post by:
Hello, I have a somewhat nested dict that I want to pickle, but it (sometimes) contains some no-no's (specifically, in this case, functions). I know I can't pickle them, but I would like to be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.