File: pickletrouble.py

package info (click to toggle)
pytables 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,972 kB
  • ctags: 16,919
  • sloc: python: 59,339; ansic: 46,596; cpp: 1,463; sh: 476; makefile: 428
file content (28 lines) | stat: -rw-r--r-- 756 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import print_function
import tables


class MyClass(object):
    foo = 'bar'

# An object of my custom class.
myObject = MyClass()

h5f = tables.open_file('test.h5', 'w')
h5f.root._v_attrs.obj = myObject  # store the object
print(h5f.root._v_attrs.obj.foo)  # retrieve it
h5f.close()

# Delete class of stored object and reopen the file.
del MyClass, myObject

h5f = tables.open_file('test.h5', 'r')
print(h5f.root._v_attrs.obj.foo)
# Let us inspect the object to see what is happening.
print(repr(h5f.root._v_attrs.obj))
# Maybe unpickling the string will yield more information:
import pickle
pickle.loads(h5f.root._v_attrs.obj)
# So the problem was not in the stored object,
# but in the *environment* where it was restored.
h5f.close()