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 46 47 48 49 50 51 52 53 54 55 56
|
#! /usr/bin/env python
import openturns as ot
import openturns.experimental as otexp
import os
import inspect
ot.TESTPREAMBLE()
ot.Log.Show(ot.Log.NONE)
# find all instantiable classes
persistentClasses = {}
for mod in [ot, otexp]:
for name, obj in inspect.getmembers(mod):
if inspect.isclass(obj) and issubclass(obj, ot.PersistentObject):
persistentClasses[obj.__name__] = obj
# save / load
fileName = "studyStd.xml"
failed = []
for cname, class_ in persistentClasses.items():
study = ot.Study()
study.setStorageManager(ot.XMLStorageManager(fileName))
print(cname)
try:
instance = class_()
study.add(cname, instance)
study.save()
study = ot.Study()
study.setStorageManager(ot.XMLStorageManager(fileName))
study.load()
os.remove(fileName)
instance = class_()
study.fillObject(cname, instance)
print(cname, "OK")
except Exception as exc:
failed += [cname]
print("--", cname, exc)
print(f"==== {len(failed)} failures / {len(persistentClasses)} classes ====")
print(f"failed={failed}")
assert len(failed) < 43, f"{len(failed)} serialization failures: {failed}"
# non-ascii filename
fileName = "utf_é.xml"
study = ot.Study()
study.setStorageManager(ot.XMLStorageManager(fileName))
study.add("x", ot.Point([42.0]))
study.save()
study = ot.Study()
study.setStorageManager(ot.XMLStorageManager(fileName))
study.load()
x = ot.Point()
study.fillObject("x", x)
assert len(x) == 1 and x[0] == 42.0, "wrong x"
print(x)
os.remove(fileName)
|