Ticket #30402: test-save.sage

File test-save.sage, 1.3 KB (added by gh-penguian, 14 months ago)

Test script to run on Cocalc

Line 
1# Test save, load and explain_pickle
2
3from sage.misc.banner import version_dict
4
5def get_version():
6    v_dict = version_dict()
7    v_list = [v_dict['major'],v_dict['minor'],v_dict['tiny']]
8    return '.'.join([str(v) for v in v_list])
9
10def make_name(prefix,version):
11    return prefix+'.'+version+'.sobj'
12
13def test_load(sobj_name):
14    sobj_read = open(sobj_name,'rb').read()
15    sobj_explain = explain_pickle(sobj_read)
16    sobj_load = load(sobj_name)
17    return sobj_explain, sobj_load
18
19def test_load_bytes(sobj_name):
20    return load(sobj_name,encoding='bytes')
21
22def test_save(sobj,prefix,version):
23    sobj_name = make_name(prefix,version)
24    save(sobj,sobj_name)
25    sobj_explain, sobj_load = test_load(sobj_name)
26    return sobj_explain, sobj_load
27
28if __name__ == '__main__':
29    version = get_version()
30    print(version)
31
32    m = matrix([[-1,2,3],[4,5,7]])
33    d = {0:'Hello',1:m}
34
35    m_explain, m_load = test_save(m,'m',version)
36    print(m_explain)
37    print(m_load)
38    if version > '9':
39        m_name = make_name('m',version)
40        m_bytes = test_load_bytes(m_name)
41        print(m_bytes)
42
43    d_explain, d_load = test_save(d,'d',version)
44    print(d_explain)
45    print(d_load)
46    if version > '9':
47        d_name = make_name('d',version)
48        d_bytes = test_load_bytes(d_name)
49        print(d_bytes)