File: t_Object_pickle.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (45 lines) | stat: -rwxr-xr-x 971 bytes parent folder | download
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#! /usr/bin/env python

import openturns as ot
import pickle
from io import BytesIO

obj_list = []
obj_list.append(ot.Point([1.6, -8.7]))
obj_list.append(ot.Sample([[4.6, -3.7], [8.4, 6.3]]))
obj_list.append(ot.Description(["x", "y", "z"]))
obj_list.append(ot.Indices([1, 2, 4]))
obj_list.append(ot.Matrix([[1, 2], [3, 4]]))
obj_list.append(ot.SymbolicFunction(["x1", "x2"], ["y1=x1+x2"]))

src = BytesIO()

for obj in obj_list:
    pickle.dump(obj, src)

src.seek(0)

for obj in obj_list:
    obj2 = pickle.load(src)
    print(("object: " + str(obj)))
    print(("same: " + str(obj2 == obj) + "\n"))


# make sure we can serialize a derivated class
class MySample(ot.Sample):
    def __init__(self, sample):
        super().__init__(sample)

    def myFunction(self):
        print("test")


dist = ot.Normal([0, 0], [1, 1])
sample = dist.getSample(20)
mySample = MySample(sample)

src = BytesIO()
pickle.dump(mySample, src)

src.seek(0)
mySample_pkl = pickle.load(src)