Pickling array.array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Fleury

    Pickling array.array

    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

    class array(arr.array ):
    def __getstate__(se lf):
    return {'buffer': self.tostring() }
    def __setstate__(se lf, dict):
    self.fromstring (dict['buffer'])
  • Nicolas Fleury

    #2
    Re: Pickling array.array

    Nicolas Fleury wrote:[color=blue]
    > class array(arr.array ):
    > def __getstate__(se lf):
    > return {'buffer': self.tostring() }
    > def __setstate__(se lf, dict):
    > self.fromstring (dict['buffer'])[/color]

    I also added __getinitargs__ (), but it is not called...

    Comment

    • Nicolas Fleury

      #3
      Re: Pickling array.array

      Nicolas Fleury wrote:[color=blue]
      > I also added __getinitargs__ (), but it is not called...[/color]

      For those interested in the same problem, the only solution I've found
      is to encapsulate the array instead of derive from it. It sucks a bit,
      since all methods need to be implemented to redirect to array.array, but
      at least it's working. I'm still wondering how to do it with derivation...

      Thx and regards,
      Nicolas

      import array as arr
      class array:
      def __init__(self, typecode, initializer=Non e):
      self.arr = arr.array(typec ode, initializer)
      def __getstate__(se lf):
      return {'buffer': self.arr.tostri ng(),
      'typecode': self.arr.typeco de}
      def __setstate__(se lf, dict):
      self.arr = arr.array(dict['typecode'])
      self.arr.fromst ring(dict['buffer'])

      Comment

      • Nicolas Fleury

        #4
        Re: Pickling array.array

        Nicolas Fleury wrote:[color=blue]
        > For those interested in the same problem, the only solution I've found
        > is to encapsulate the array instead of derive from it. It sucks a bit,
        > since all methods need to be implemented to redirect to array.array, but
        > at least it's working. I'm still wondering how to do it with derivation...[/color]

        Here's a complete solution (if anyone knows a simpler solution...):

        import array as arr
        class array:
        def __init__(self, typecode, initializer=Non e):
        self.arr = arr.array(typec ode, initializer)
        def __getstate__(se lf):
        return {'buffer': self.arr.tostri ng(),
        'typecode': self.arr.typeco de}
        def __setstate__(se lf, dict):
        self.arr = arr.array(dict['typecode'])
        self.arr.fromst ring(dict['buffer'])
        def __getattr__(sel f, name):
        if name == 'arr': return self.arr
        return self.arr.__geta ttribute__(name )
        def __setattr__(sel f, name, value):
        if name == 'arr': self.__dict__['arr'] = value
        else: self.arr.__seta ttr__(name, value)

        Comment

        Working...