[SciPy-user] Numeric Object Array Pickling

Greg Novak novak at ucolick.org
Tue Jul 13 14:04:25 CDT 2004


I would like a "ragged" array that I can pickle.  I need a list of lists
of numbers, and the "inner" arrays will all be different lengths that I
don't know in advance, which is why I want it to be ragged.  But I also
need to use the array slicing facilities of Numeric, which is why I want
it to be an array rather than a list.  It looks like the "PyObject" type
of Numeric array is what I want, but I can't pickle it.  The following
code gives a seg fault (on Red Hat Linux 9, Python 2.2, Scipy 0.3)

  arrays = [arange(i+1) for i in range(5)]
  aoa = array([None for i in range(5)], 'O')
  for i in range(5):
      aoa[i] = arrays[i]

  file=open("aoa.txt", "w")
  pickle.dump(aoa, file)
  file.close()

  ... Exit and restart python ...

  file=open("aoa.txt", "r")
  aoa = pickle.load(file)
  file.close()

I can imagine why this is happening... it seems that the array pickling 
function is just saving the memory addresses of the objects in the array, 
because if you don't exit Python this code works.  However... is there a 
way to do what I want?

It looks like I can make a list of arrays and pickle that.  Then, when I 
unpickle it I can convert it to a PyObject array and use Numeric's 
slicing.  But...  that's a little kludgey.  Is there a better way?  Or 
does this qualify as a bug?

Thanks,
Greg





More information about the SciPy-user mailing list