473,386 Members | 2,042 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,386 software developers and data experts.

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__(self):
return {'buffer': self.tostring()}
def __setstate__(self, dict):
self.fromstring(dict['buffer'])
Jul 18 '05 #1
3 1839
Nicolas Fleury wrote:
class array(arr.array):
def __getstate__(self):
return {'buffer': self.tostring()}
def __setstate__(self, dict):
self.fromstring(dict['buffer'])


I also added __getinitargs__(), but it is not called...
Jul 18 '05 #2
Nicolas Fleury wrote:
I also added __getinitargs__(), but it is not called...


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=None):
self.arr = arr.array(typecode, initializer)
def __getstate__(self):
return {'buffer': self.arr.tostring(),
'typecode': self.arr.typecode}
def __setstate__(self, dict):
self.arr = arr.array(dict['typecode'])
self.arr.fromstring(dict['buffer'])
Jul 18 '05 #3
Nicolas Fleury wrote:
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...


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

import array as arr
class array:
def __init__(self, typecode, initializer=None):
self.arr = arr.array(typecode, initializer)
def __getstate__(self):
return {'buffer': self.arr.tostring(),
'typecode': self.arr.typecode}
def __setstate__(self, dict):
self.arr = arr.array(dict['typecode'])
self.arr.fromstring(dict['buffer'])
def __getattr__(self, name):
if name == 'arr': return self.arr
return self.arr.__getattribute__(name)
def __setattr__(self, name, value):
if name == 'arr': self.__dict__['arr'] = value
else: self.arr.__setattr__(name, value)

Jul 18 '05 #4

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

Similar topics

1
by: Marc | last post by:
Hi all, After some research I've decided that my previous question (Confusing problem between Tkinter.Intvar...) was headed in the wrong direction. Partly because I think I have a greater...
1
by: Edward Loper | last post by:
I'm having trouble pickling subclasses of dict when they contain cycles. In particular: >>> import pickle >>> class D(dict): pass >>> d = D() >>> d = d # add a cycle. >>> print d {1: {...}}...
8
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...
1
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...
2
by: John Machin | last post by:
Googling for "pickle array" in comp.lang.python yields old messages that show a PickleError -- plus one message where Alex Martelli writes "I am but an egg" :O) Looks like arrays are NOW (2.4.1)...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...

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.