Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Pickling a dictionary

Reply
Thread Tools

Pickling a dictionary

 
 
Devashish Tyagi
Guest
Posts: n/a
 
      11-07-2012
So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this

obj = InteractiveInterpreter()
local = obj.locals()
pickle.dump(local, open('obj.dump','rw'))

But I received an error say
TypeError: can't pickle ellipsis objects

From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour?
 
Reply With Quote
 
 
 
 
Ian Kelly
Guest
Posts: n/a
 
      11-07-2012
On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi
<(E-Mail Removed)> wrote:
> So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this
>
> obj = InteractiveInterpreter()
> local = obj.locals()
> pickle.dump(local, open('obj.dump','rw'))
>
> But I received an error say
> TypeError: can't pickle ellipsis objects
>
> From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour?


The contents of the dictionary need to be pickleable as well. You
probably have an ellipsis object in the dict somewhere.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      11-07-2012
Devashish Tyagi wrote:

> So I want to store the current state of a InteractiveInterpreter Object in
> database. In order to achieve this I tried this
>
> obj = InteractiveInterpreter()
> local = obj.locals()
> pickle.dump(local, open('obj.dump','rw'))


Assuming InteractiveInterpreter is imported from the code module the above
will fail with a TypeError. Please copy-and paste code snippets to avoid
guessing games.

> But I received an error say
> TypeError: can't pickle ellipsis objects
>
> From what I understand this shouldn't occur as local is a dictionary. Any
> particular reason for this behaviour?


For a dict to be pickled all its keys and values have to be pickled, too.

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      11-07-2012
On Wed, Nov 7, 2012 at 9:16 AM, Ian Kelly <(E-Mail Removed)> wrote:
> On Wed, Nov 7, 2012 at 9:07 AM, Devashish Tyagi
> <(E-Mail Removed)> wrote:
>> So I want to store the current state of a InteractiveInterpreter Object in database. In order to achieve this I tried this
>>
>> obj = InteractiveInterpreter()
>> local = obj.locals()
>> pickle.dump(local, open('obj.dump','rw'))
>>
>> But I received an error say
>> TypeError: can't pickle ellipsis objects
>>
>> From what I understand this shouldn't occur as local is a dictionary. Any particular reason for this behaviour?

>
> The contents of the dictionary need to be pickleable as well. You
> probably have an ellipsis object in the dict somewhere.


By the way, if you use Python 3 and pickle protocol 3, then Ellipsis
*is* pickleable:

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:4 [MSC v.1600
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> pickle.dumps(Ellipsis, protocol=pickle.HIGHEST_PROTOCOL)

b'\x80\x03cbuiltins\nEllipsis\nq\x00.'

Cheers,
Ian
 
Reply With Quote
 
Devashish Tyagi
Guest
Posts: n/a
 
      11-07-2012
On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten wrote:
> Devashish Tyagi wrote:
>
>
>
> > So I want to store the current state of a InteractiveInterpreter Object in

>
> > database. In order to achieve this I tried this

>
> >

>
> > obj = InteractiveInterpreter()

>
> > local = obj.locals()

>
> > pickle.dump(local, open('obj.dump','rw'))

>
>
>
> Assuming InteractiveInterpreter is imported from the code module the above
>
> will fail with a TypeError. Please copy-and paste code snippets to avoid
>
> guessing games.


Here is the code

from code import InteractiveInterpreter
import StringIO
import pickle

src = StringIO.StringIO()
inter = InteractiveInterpreter()
inter.runcode('a = 5')
local = inter.locals

pickle.dump(local,open('obj.dump','wb'))

Here is the error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle ellipsis objects


>
>
>
> > But I received an error say

>
> > TypeError: can't pickle ellipsis objects

>
> >

>
> > From what I understand this shouldn't occur as local is a dictionary. Any

>
> > particular reason for this behaviour?

>
>
>
> For a dict to be pickled all its keys and values have to be pickled, too.

 
Reply With Quote
 
Devashish Tyagi
Guest
Posts: n/a
 
      11-07-2012
On Wednesday, 7 November 2012 21:57:05 UTC+5:30, Peter Otten wrote:
> Devashish Tyagi wrote:
>
>
>
> > So I want to store the current state of a InteractiveInterpreter Object in

>
> > database. In order to achieve this I tried this

>
> >

>
> > obj = InteractiveInterpreter()

>
> > local = obj.locals()

>
> > pickle.dump(local, open('obj.dump','rw'))

>
>
>
> Assuming InteractiveInterpreter is imported from the code module the above
>
> will fail with a TypeError. Please copy-and paste code snippets to avoid
>
> guessing games.


Here is the code

from code import InteractiveInterpreter
import StringIO
import pickle

src = StringIO.StringIO()
inter = InteractiveInterpreter()
inter.runcode('a = 5')
local = inter.locals

pickle.dump(local,open('obj.dump','wb'))

Here is the error

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/pickle.py", line 1370, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.7/pickle.py", line 663, in _batch_setitems
save(v)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle ellipsis objects


>
>
>
> > But I received an error say

>
> > TypeError: can't pickle ellipsis objects

>
> >

>
> > From what I understand this shouldn't occur as local is a dictionary. Any

>
> > particular reason for this behaviour?

>
>
>
> For a dict to be pickled all its keys and values have to be pickled, too.

 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      11-07-2012
On Wed, Nov 7, 2012 at 10:40 AM, Devashish Tyagi
<(E-Mail Removed)> wrote:
> Here is the code
>
> from code import InteractiveInterpreter
> import StringIO
> import pickle
>
> src = StringIO.StringIO()
> inter = InteractiveInterpreter()
> inter.runcode('a = 5')
> local = inter.locals
>
> pickle.dump(local,open('obj.dump','wb'))


After calling runcode it seems that the __builtins__ dict containing
an 'Ellipsis' binding has been added to inter.locals. You probably
don't want to include the contents of __builtins__ in your pickle
anyway, so just delete it:

del local['__builtins__']
pickle.dump(local, ...)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance ordered dictionary vs normal dictionary Navkirat Singh Python 6 07-29-2010 10:18 AM
Re: Performance ordered dictionary vs normal dictionary Chris Rebert Python 0 07-29-2010 06:11 AM
creating a dictionary from a dictionary with regex james_027 Python 1 08-22-2007 07:39 AM
[DICTIONARY] - Copy dictionary entries to attributes Ilias Lazaridis Python 6 02-21-2006 11:27 AM
dictionary within dictionary Fox ASP General 5 03-13-2005 05:03 AM



Advertisments