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

unpickling Set as set

Hi,

Is there an easy way to unpickle the older Set as the newer set behind
the scenes when using shelve?

Amir

Nov 8 '06 #1
3 1066

The two are not of the same type:

---------------------------------
In : import sets
In : s1=sets.Set([1,2,3])

In : s2=set([1,2,3])

In: type(s1)
Out: <class 'sets.Set'>

In : type(s2)
Out: <type 'set'>

In : s1==s2
Out: False # oops!

In: s2==set(s1)
Out: True # aha!
----------------------------------

You'll have to just cast:
unpickled_set=set(unpickled_set)

-Nick V.
Amir Michail wrote:
Hi,

Is there an easy way to unpickle the older Set as the newer set behind
the scenes when using shelve?

Amir
Nov 8 '06 #2
Nick Vatamaniuc wrote:
The two are not of the same type:

---------------------------------
In : import sets
In : s1=sets.Set([1,2,3])

In : s2=set([1,2,3])

In: type(s1)
Out: <class 'sets.Set'>

In : type(s2)
Out: <type 'set'>

In : s1==s2
Out: False # oops!

In: s2==set(s1)
Out: True # aha!
----------------------------------

You'll have to just cast:
unpickled_set=set(unpickled_set)

-Nick V.

Or you may have this done automatically by hacking the Set class:

from sets import Set
import cPickle as pickle

Set.__reduce__ = lambda self: (set, (self._data,))

s = Set([1,2,3])
x = pickle.dumps(s)
print pickle.loads(x)
This doesn't work though if you have already pickled the Set before
replacing its __reduce__, so it may not necessarily be what you want.
If there is a way around it, I'd like to know it.

George

Nov 8 '06 #3
At Wednesday 8/11/2006 05:26, George Sakkis wrote:
>Or you may have this done automatically by hacking the Set class:

from sets import Set
import cPickle as pickle

Set.__reduce__ = lambda self: (set, (self._data,))

s = Set([1,2,3])
x = pickle.dumps(s)
print pickle.loads(x)
This doesn't work though if you have already pickled the Set before
replacing its __reduce__, so it may not necessarily be what you want.
If there is a way around it, I'd like to know it.
Perhaps registering a suitable reduce function in the copy_reg module.
If the sets were pickled alone, and it's not too much trouble, using:
a_set = set(a_set) just after unpickling may be enough.
And if they were instance attributes, __setstate__ on the class can
do the conversion.
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 8 '06 #4

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

Similar topics

1
by: Bob | last post by:
I've read over section 3.14.5.2 of the doc about a zillion times, and it still makes absolutely no sense to me. Can someone please explain it? What I'd like to do is, basically, have the object...
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...
3
by: sushant.sirsikar | last post by:
Hi, I am new in Python World.I want to know what is mean by ``pickling'' and ``unpickling'' ? And how can we used it?Please Give Me some links of Picking Examples. Thanks Sushant
2
by: Boris Borcic | last post by:
Assuming that the items of my_stream share no content (they are dumps of db cursor fetches), is there a simple way to do the equivalent of def pickles(my_stream) : from cPickle import...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.