473,395 Members | 1,637 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,395 software developers and data experts.

pickle an instance of a custom class

3
I want to save the content of a custom made OrderedDict. (I got the OrderedDict from http://aspn.activestate.com/ASPN/Python)
The pickle modul seemed to be the solution for the job.
At first I tried to pickle the "nacked" OrderedDict derived from dict, but the pickle routine saves only a empty dict. A secont try to "replace" the __dict__ with the __getstate__ and __setstate__ methods also faild.

How can I save the content (the items of the derived dict and the _keys list) of the OrderedDict via the pickle modul?

here is the code:

Expand|Select|Wrap|Line Numbers
  1. class OrderedDict(dict):
  2.     """This dictionary class extends UserDict to record the order in which items are 
  3.     added. Calling keys(), values(), items(), etc. will return results in this order."""
  4.  
  5.     def __init__(self, d = None):
  6.         self._keys = []
  7.         if d == None: d = {}
  8.         self.update(d)
  9.  
  10.     def __setitem__(self, key, item):
  11.         dict.__setitem__(self,key, item)
  12.         if key not in self._keys: self._keys.append(key)
  13.  
  14.     def items(self):
  15.         return zip(self._keys, self.values())
  16.  
  17.     def keys(self):
  18.         return self._keys[:]
  19.  
  20.     def update(self, d):
  21.         dict.update(self, d)
  22.         for key in d.keys():
  23.             if key not in self._keys: self._keys.append(key)
  24.  
  25.     def __getstate__(self):
  26.         """Return state values to be pickled."""
  27.         return (dict(self.items()), self._keys)
  28.  
  29.     def __setstate__(self, state):
  30.         """Restore state from the unpickled state values."""
  31.         self.update(state[0])
  32.         self._keys = state[1]
  33.  
  34. if __name__ == "__main__":
  35.     import pickle
  36.     o = OrderedDict()
  37.     o['a'] = [0,1]
  38.     o['b'] = [2,3]
  39.     p = pickle.dumps(o)
  40.     u = pickle.loads(p)
  41.     print o # output should be {'b': [2, 3], 'a': [0, 1]}
  42.     print u # output should be {'b': [2, 3], 'a': [0, 1]}
  43.  
  44. # Output:
  45. #{'b': [2, 3], 'a': [0, 1]}
  46. #{}                                      
  47.  
Mar 10 '08 #1
4 7281
bvdet
2,851 Expert Mod 2GB
I want to save the content of a custom made OrderedDict. (I got the OrderedDict from http://aspn.activestate.com/ASPN/Python)
The pickle modul seemed to be the solution for the job.
At first I tried to pickle the "nacked" OrderedDict derived from dict, but the pickle routine saves only a empty dict. A secont try to "replace" the __dict__ with the __getstate__ and __setstate__ methods also faild.

How can I save the content (the items of the derived dict and the _keys list) of the OrderedDict via the pickle modul?
The dictionary elements will display in the correct order if you implement a __repr__() method. You will also need to override the dict.values() method so the values will be paired with the correct keys. Object pickling and unpickling should work without __getstate__() and __setstate__() methods.
Expand|Select|Wrap|Line Numbers
  1. class OrderedDict(dict):
  2.  
  3.     def __init__(self, d = None):
  4.         self._keys = []
  5.         if d == None: d = {}
  6.         self.update(d)
  7.  
  8.     def __setitem__(self, key, item):
  9.         dict.__setitem__(self,key, item)
  10.         if key not in self._keys: self._keys.append(key)
  11.  
  12.     def items(self):
  13.         return zip(self._keys, self.values())
  14.  
  15.     def keys(self):
  16.         return self._keys[:]
  17.  
  18.     def itervalues(self):
  19.         for k in self._keys:
  20.             yield self[k]
  21.  
  22.     def values(self):
  23.         return list(self.itervalues())    
  24.  
  25.     def update(self, d):
  26.         dict.update(self, d)
  27.         for key in d.keys():
  28.             if key not in self._keys: self._keys.append(key)
  29.  
  30.     def __repr__(self):
  31.         return '{%s}' % ', '.join([': '.join([repr(k),str(v)]) \
  32.                                    for k,v in zip(self.keys(), self.values())])
  33.  
  34. if __name__ == "__main__":
  35.     dd = OrderedDict()
  36.     dd['z'] = [0,1]
  37.     dd['b'] = [2,3]
  38.     dd['x'] = [21,22]
  39.     dd['y'] = [45,46]
  40.     dd['a'] = [9,10]
  41.     p = pickle.dumps(dd)
  42.     u = pickle.loads(p)
  43.     print dd
  44.     print u
Output:

>>> {'z': [0, 1], 'b': [2, 3], 'x': [21, 22], 'y': [45, 46], 'a': [9, 10]}
{'z': [0, 1], 'b': [2, 3], 'x': [21, 22], 'y': [45, 46], 'a': [9, 10]}
>>>
Mar 11 '08 #2
PBlitz
3
Thank you for the help. Unfortunately my output with your code is:
{'z': [0, 1], 'b': [2, 3], 'x': [21, 22], 'y': [45, 46], 'a': [9, 10]}
{}

the pickle p contains:
c__main__
OrderedDict
p0
(tp1
Rp2
.

This means the pickle dumps no dict values nor _keys list values.
Maybe the reason is my configuration. I use jython 2.2.1 on a windows machine.
Is there a implementation difference between python and jython?
Mar 11 '08 #3
bvdet
2,851 Expert Mod 2GB
Thank you for the help. Unfortunately my output with your code is:
{'z': [0, 1], 'b': [2, 3], 'x': [21, 22], 'y': [45, 46], 'a': [9, 10]}
{}

the pickle p contains:
c__main__
OrderedDict
p0
(tp1
Rp2
.

This means the pickle dumps no dict values nor _keys list values.
Maybe the reason is my configuration. I use jython 2.2.1 on a windows machine.
Is there a implementation difference between python and jython?
I am using Python 2.3 on Windows XP. I don't know the differences. I have pickled class instances successfully many times. Your pickle module may be broken.
Mar 11 '08 #4
PBlitz
3
Here a more precise description of the issue:
Pickling of a class derived from dict creates an empty pickle in jython
2.2.1. In contrast Python 2.5.2 delivers the expected result.
So I assume a bug in the jython pickle module. Does anyone know if a developer release of jython already solves the problem? Or how to patch the module?

# code describing the issue:

Expand|Select|Wrap|Line Numbers
  1. class MyDict(dict):
  2.     def __init__(self, d = None):
  3.         if d == None: d = {}
  4.         dict.__init__(self, d)
  5.  
  6. if __name__ == "__main__":
  7.     import pickle
  8.     md = MyDict({'b': [2, 3], 'z': [0, 1]})
  9.     p = pickle.dumps(md)
  10.     rd = pickle.loads(p)
  11.     print "-> pickle p:\n", p
  12.     print "-> MyDict md:\n", md
  13.     print "-> reconstructed MyDict rd:\n", rd
  14.  
# jython 2.2.1 output with empty pickle p:

-> pickle p:
c__main__
MyDict
p0
(tp1
Rp2
.
-> MyDict md:
{'b': [2, 3], 'z': [0, 1]}
-> reconstructed MyDict rd:
{}

# in contrast python 2.5.2 output with filled pickle p:

-> pickle p:
ccopy_reg
_reconstructor
p0
(c__main__
MyDict
p1
c__builtin__
dict
p2
(dp3
S'b'
p4
(lp5
I2
aI3
asS'z'
p6
(lp7
I0
aI1
astp8
Rp9
.
-> MyDict md:
{'b': [2, 3], 'z': [0, 1]}
-> reconstructed MyDict rd:
{'b': [2, 3], 'z': [0, 1]}
Mar 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Naresh Agarwal | last post by:
Hi I've written a Custom class loader, which reads the bytecode from a database and returns the Class object. For example, bytecode of a class "Test" is stored in the database. Through custom...
0
by: bandzuch martin | last post by:
hello! is there any way how to return custom type for specific class using typeof operator? //my custom type definition class CustomType : System.Type { ... implementation ...
3
by: Eric Workman | last post by:
If I have a class that holds two string values, so for instance. Public Class myClass{ private string s1; private string s2; public myClass(str1, str2){ s1 = str1; s2 = str2;
2
by: raymi | last post by:
Hi, I have a custom class that gets its properties dynamically from a database at runtime. Therefore I implemented the ICustomTypeDescriptor in this class and created a a custom...
7
by: Joe Rigley | last post by:
Hi, I have a custom class with a public method. I want to perform a repose.redirect if an error occurs in the public method GetUserRoles. Unfortunately, Visual Studio 2003 is throwing an error...
3
by: Steve Franks | last post by:
Is there a way I can extend the HttpContext or one of its subclasses to include a property that exposes a custom class of mine to all ASP.NET pages? More specifically, I'd like to use a...
6
by: Shimon Sim | last post by:
Hi I am working on application that need to hold custom user information - Last and first name, email, some other domain related information. I used to create Base class for all my pages. The base...
14
by: Scott M. | last post by:
How do I add information to a custom class's properties and methods so that when an instance of the class is made the VS.NET intelliSense will show the information in a tooltip as the members are...
4
by: Pieter | last post by:
Hi, Is it possible to use a Custom Class/Object as a DataSource/DataSet for a report? I have now for instance my object MyCompany, and it woudl be nice to just use a MyCompany-instance as the...
3
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.