473,757 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pickling a class with a __getattr__

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
>>import cPickle as pickle
class Dict(dict):
.... pass
....
>>>

friend = Dict(name='Zahi d', age=40)
friend
{'age': 40, 'name': 'Zahid'}
>>v=pickle.dump s(friend)
p=pickle.load s(v)
p
{'age': 40, 'name': 'Zahid'}

This is what happens when I'm trying to be clever:
>>import cPickle as pickle
class Dict(dict):
.... def __getattr__(sel f, key):
.... return self.__getitem_ _(key)
....
>>friend = Dict(name='Zahi d', age=40)
friend
{'age': 40, 'name': 'Zahid'}
>>friend.name
'Zahid'
>>v=pickle.dump s(friend)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/copy_reg.py", line 73, in _reduce_ex
getstate = self.__getstate __
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'
Why can't I pickle the slightly more featurefull class there called
'Dict'? I've got my reasons for not going for a simple type dict but
feel that that is irrelevant right now.

Apr 1 '07 #1
2 5496
Peter Bengtsson wrote:
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 is what happens when I'm trying to be clever:
>>>import cPickle as pickle
class Dict(dict):
... def __getattr__(sel f, key):
... return self.__getitem_ _(key)
...
>>>friend = Dict(name='Zahi d', age=40)
friend
{'age': 40, 'name': 'Zahid'}
>>>friend.nam e
'Zahid'
>>>v=pickle.dum ps(friend)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/copy_reg.py", line 73, in _reduce_ex
getstate = self.__getstate __
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'
Why can't I pickle the slightly more featurefull class there called
'Dict'?
Because you allow your __getattr__() implementation to raise the wrong kind
of exception.
>>from cPickle import dumps, loads
class Dict(dict):
.... def __getattr__(sel f, key):
.... try:
.... return self[key]
.... except KeyError:
.... raise AttributeError
....
>>friend = Dict(name="Zaph od", age=42)
v = dumps(friend)
p = loads(v)
p
{'age': 42, 'name': 'Zaphod'}

Peter
Apr 1 '07 #2
On Apr 1, 5:48 pm, Peter Otten <__pete...@web. dewrote:
Peter Bengtsson wrote:
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 is what happens when I'm trying to be clever:
>>import cPickle as pickle
class Dict(dict):
... def __getattr__(sel f, key):
... return self.__getitem_ _(key)
...
>>friend = Dict(name='Zahi d', age=40)
friend
{'age': 40, 'name': 'Zahid'}
>>friend.name
'Zahid'
>>v=pickle.dump s(friend)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.4/copy_reg.py", line 73, in _reduce_ex
getstate = self.__getstate __
File "<stdin>", line 3, in __getattr__
KeyError: '__getstate__'
Why can't I pickle the slightly more featurefull class there called
'Dict'?

Because you allow your __getattr__() implementation to raise the wrong kind
of exception.
>from cPickle import dumps, loads
class Dict(dict):

... def __getattr__(sel f, key):
... try:
... return self[key]
... except KeyError:
... raise AttributeError
...>>friend = Dict(name="Zaph od", age=42)
>v = dumps(friend)
p = loads(v)
p

{'age': 42, 'name': 'Zaphod'}

Peter
Thanks! That did the trick. I also noticed that I could define
__getstate__ and __setstate__ (needed for protocol 2) but your
solution works much better.

Apr 1 '07 #3

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

Similar topics

2
2039
by: Gabriel Genellina | last post by:
Hi In the following code sample, I have: - a Worker class, which could have a lot of methods and attributes. In particular, it has a 'bar' attribute. This class can be modified as needed. - a Base class (old-style) which defines a default class attribute 'bar' too. I can't modify the source code of this class. Note that Workes does not inherit from Base. - a Derived class which must inherit from Base and is a wrapper around Worker: it...
3
1860
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 doing it correctly. Any idea what I'm doing wrong? Thx and Regards, Nicolas import array as arr
20
1957
by: syd | last post by:
In my project, I've got dozens of similar classes with hundreds of description variables in each. In my illustrative example below, I have a Library class that contains a list of Nation classes. In one case, I might want a Library class with only Nations of with the continent variable "Europe", and so I'll do something like library.getContinent('Europe') which will return a Library() instance with only European nations. Similarly, I...
8
8121
by: Hans Georg Krauthaeuser | last post by:
Dear all, I have a long running application (electromagnetic compatibility measurements in mode-stirred chambers over GPIB) that use pickle (cPickle) to autosave a class instance with all the measured data from time to time. At the beginning, pickling is quite fast but when the data becomes more and more pickling slows down rapidly.
1
3261
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 pickle it? Thanks , Fedor
9
3601
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 def __getstate__(self): return self.A, self.B def __setstate__(self, tup):
0
1389
by: Irmen de Jong | last post by:
I'm having troubles pickling classes that extend Exception. Given the following source: class Foo(object): def __init__(self, m): self.m=m class Bar(Exception): def __init__(self, m):
0
265
by: Magnus Schuster | last post by:
With this explanation the behaviour is absolutely clear. Can I find some documentation anywhere containing more background information how magic functions are resolved? I haven't been successful with the Python Language Reference. I am totally clueless how to do this. Below you find my attempt. Neither __getattr__ nor __add__ of the metaclass 'meta' are ever called. May I ask you for some more guidance or corrections to the code below?
11
6257
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as the app is concerned). The problem is, If someone makes a typo, they may get an unexpected error due accidentally calling the original attribute instead of the wrapped version. Does anyone have a simple solution for this?
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9904
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.