unpickling Set as set

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amir  Michail

    #1

    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

  • Nick Vatamaniuc

    #2
    Re: unpickling Set as set


    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=s et(unpickled_se t)

    -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

    Comment

    • George Sakkis

      #3
      Re: unpickling Set as set

      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=s et(unpickled_se t)
      >
      -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

      Comment

      • Gabriel G

        #4
        Re: unpickling Set as set

        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

        Comment

        Working...