illegal-immigrant
illegal-immigrant

Reputation: 8234

storing 'object'

Does PyTables support storing Python objects? something like this :

dtype = np.dtype([('Name', '|S2'), ('objValue', object)])
data = np.zeros(3, dtype)
file.createArray(box3,'complicated',data)

I get error when trying to do this of course... How to properly store arrays of objects?Is it possible? Thanks

Upvotes: 1

Views: 1061

Answers (2)

FrancescAlted
FrancescAlted

Reputation: 351

You can save generic Python object with Pytables:

>>> dtype = np.dtype([('Name', '|S2'), ('objValue', object)])
>>> data = np.zeros(3, dtype)
>>> file = tables.openFile('/tmp/test.h5', 'w')
>>> myobjects = file.createVLArray(file.root, 'myobjects', tables.ObjectAtom())
>>> myobjects.append(data)
>>> myobjects[0]
array([('', 0), ('', 0), ('', 0)], 
      dtype=[('Name', '|S2'), ('objValue', '|O8')])

However, this will use pickle (cPickle in fact) behind the scenes, so you won't be able to access these objects from other languages (pickle is a serialization format only supported by Python itself).

Upvotes: 5

Jacob Oscarson
Jacob Oscarson

Reputation: 6393

Try the pickle module if you want to store complicated data somewhere it isn't supported by the library in question.

Upvotes: 1

Related Questions