Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 19204
1
r"""
2
Load and save Sage objects with standardized names
3
==================================================
4
5
The ``saveable`` module defines the ``Savable`` class:
6
a mixin class with methods that load and save Sage objects with standardized names.
7
8
AUTHORS:
9
10
- Paul Leopardi (2016-08-04): initial version
11
- Paul Leopardi (2017-04-01): saveable.py based on persistent.py
12
13
"""
14
#*****************************************************************************
15
# Copyright (C) 2016-2018 Paul Leopardi [email protected]
16
#
17
# Distributed under the terms of the GNU General Public License (GPL)
18
# as published by the Free Software Foundation; either version 2 of
19
# the License, or (at your option) any later version.
20
# http://www.gnu.org/licenses/
21
#*****************************************************************************
22
23
from builtins import object
24
import os
25
import os.path
26
27
from sage.misc.persist import load, save
28
29
30
class Saveable(object):
31
r"""
32
A mixin class with methods that load and save objects with standardized names.
33
34
EXAMPLES:
35
36
::
37
38
sage: from boolean_cayley_graphs.saveable import Saveable
39
sage: class ListSaveable(list, Saveable):
40
....: def __init__(self, value):
41
....: list.__init__(self, value)
42
....:
43
sage: a = ListSaveable([1])
44
sage: a[0]
45
1
46
"""
47
48
49
@classmethod
50
def mangled_name(cls, name, dir=None):
51
r"""
52
Convert a name for an object into a standardized name.
53
54
INPUT:
55
56
- ``cls`` -- The current class.
57
- ``name`` -- The name for the object.
58
- ``dir`` -- (Optional, default=None)
59
The directory name to be used for the file name of the object.
60
The default value of None means the current directory.
61
62
OUTPUT:
63
64
A string containing the directory path and the standardized name.
65
66
EXAMPLES:
67
68
::
69
70
sage: from boolean_cayley_graphs.saveable import Saveable
71
sage: class ListSaveable(list, Saveable):
72
....: def __init__(self, value):
73
....: list.__init__(self, value)
74
....:
75
sage: ListSaveable.mangled_name('a')
76
'ListSaveable__a'
77
sage: ListSaveable.mangled_name('a', dir='b')
78
'b/ListSaveable__a'
79
"""
80
standardized_name = cls.__name__ + "__" + name
81
if dir == None:
82
return standardized_name
83
else:
84
return os.path.join(dir, standardized_name)
85
86
87
@classmethod
88
def load_mangled(cls, name, dir=None):
89
r"""
90
Load an object based on its standardized name.
91
92
INPUT:
93
94
- ``cls`` -- the class object.
95
- ``name`` -- string: the file name suffix (without ".obj")
96
part of the standardized name.
97
- ``dir`` -- string, optional. The directory where the object
98
was saved. Default is None, meaning the current directory.
99
100
OUTPUT:
101
102
The object that was saved in the file referred to by the
103
standardized name and the directory.
104
105
EXAMPLES:
106
107
::
108
109
sage: import os
110
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
111
sage: a = BFI([0,1,0,0])
112
sage: d = tmp_dir()
113
sage: a.save_mangled("a", dir=d)
114
sage: b = BFI.load_mangled("a", dir=d)
115
sage: a == b
116
True
117
sage: BFI.remove_mangled("a", dir=d)
118
sage: os.rmdir(d)
119
"""
120
return cls(load(cls.mangled_name(name, dir=dir)))
121
122
123
@classmethod
124
def remove_mangled(cls, name, dir=None):
125
r"""
126
Remove a saved object based on its standardized name.
127
128
INPUT:
129
130
- ``cls`` -- the class object.
131
- ``name`` -- string: the file name suffix (without ".obj")
132
part of the standardized name.
133
- ``dir`` -- string, optional. The directory where the object
134
was saved. Default is None, meaning the current directory.
135
136
OUTPUT:
137
138
None.
139
140
EFFECT:
141
142
The file containing the saved object is deleted.
143
144
EXAMPLES:
145
146
::
147
148
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
149
sage: a = BFI([0,1,0,0])
150
sage: d = tmp_dir()
151
sage: a.save_mangled("a", dir=d)
152
sage: file_name = BFI.mangled_name("a.sobj", dir=d)
153
sage: os.path.isfile(file_name)
154
True
155
sage: BFI.remove_mangled("a", dir=d)
156
sage: os.path.isfile(file_name)
157
False
158
sage: os.rmdir(d)
159
"""
160
file_name = cls.mangled_name(name + ".sobj", dir=dir)
161
if os.path.isfile(file_name):
162
os.remove(file_name)
163
164
165
def save_mangled(self, name, dir=None):
166
r"""
167
Save an object using its standardized name.
168
169
INPUT:
170
171
- ``self`` -- the current object.
172
- ``name`` -- string: the file name suffix (without ".obj")
173
part of the standardized name.
174
- ``dir`` -- string, optional. The directory where the object
175
is to be saved. Default is None, meaning the current directory.
176
177
OUTPUT:
178
179
None.
180
181
EFFECT:
182
183
A file is created and the object ``self`` is saved into the file.
184
185
EXAMPLES:
186
187
::
188
189
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
190
sage: a = BFI([0,1,0,0])
191
sage: d = tmp_dir()
192
sage: a.save_mangled("a", dir=d)
193
sage: file_name = BFI.mangled_name("a.sobj", dir=d)
194
sage: os.path.isfile(file_name)
195
True
196
sage: BFI.remove_mangled("a", dir=d)
197
sage: os.rmdir(d)
198
"""
199
save(self, self.__class__.mangled_name(
200
name,
201
dir=dir))
202
203