Load and save Sage objects with standardized names

The saveable module defines the Savable class: a mixin class with methods that load and save Sage objects with standardized names.

AUTHORS:

  • Paul Leopardi (2016-08-04): initial version

  • Paul Leopardi (2017-04-01): saveable.py based on persistent.py

class boolean_cayley_graphs.saveable.Saveable[source]

Bases: object

A mixin class with methods that load and save objects with standardized names.

EXAMPLES:

sage: from boolean_cayley_graphs.saveable import Saveable
sage: class ListSaveable(list, Saveable):
....:     def __init__(self, value):
....:         list.__init__(self, value)
....:
sage: a = ListSaveable([1])
sage: a[0]
1
classmethod load_mangled(name, dir=None)[source]

Load an object based on its standardized name.

INPUT:

  • cls – the class object.

  • name – string: the file name suffix (without “.obj”) part of the standardized name.

  • dir – string, optional. The directory where the object was saved. Default is None, meaning the current directory.

OUTPUT:

The object that was saved in the file referred to by the standardized name and the directory.

EXAMPLES:

sage: import os
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
sage: a = BFI([0,1,0,0])
sage: d = tmp_dir()
sage: a.save_mangled("a", dir=d)
sage: b = BFI.load_mangled("a", dir=d)
sage: a == b
True
sage: BFI.remove_mangled("a", dir=d)
sage: os.rmdir(d)
classmethod mangled_name(name, dir=None)[source]

Convert a name for an object into a standardized name.

INPUT:

  • cls – The current class.

  • name – The name for the object.

  • dir – (Optional, default=None)

    The directory name to be used for the file name of the object. The default value of None means the current directory.

OUTPUT:

A string containing the directory path and the standardized name.

EXAMPLES:

sage: from boolean_cayley_graphs.saveable import Saveable
sage: class ListSaveable(list, Saveable):
....:     def __init__(self, value):
....:         list.__init__(self, value)
....:
sage: ListSaveable.mangled_name('a')
'ListSaveable__a'
sage: ListSaveable.mangled_name('a', dir='b')
'b/ListSaveable__a'
classmethod remove_mangled(name, dir=None)[source]

Remove a saved object based on its standardized name.

INPUT:

  • cls – the class object.

  • name – string: the file name suffix (without “.obj”) part of the standardized name.

  • dir – string, optional. The directory where the object was saved. Default is None, meaning the current directory.

OUTPUT:

None.

EFFECT:

The file containing the saved object is deleted.

EXAMPLES:

sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
sage: a = BFI([0,1,0,0])
sage: d = tmp_dir()
sage: a.save_mangled("a", dir=d)
sage: file_name = BFI.mangled_name("a.sobj", dir=d)
sage: os.path.isfile(file_name)
True
sage: BFI.remove_mangled("a", dir=d)
sage: os.path.isfile(file_name)
False
sage: os.rmdir(d)
save_mangled(name, dir=None)[source]

Save an object using its standardized name.

INPUT:

  • self – the current object.

  • name – string: the file name suffix (without “.obj”) part of the standardized name.

  • dir – string, optional. The directory where the object is to be saved. Default is None, meaning the current directory.

OUTPUT:

None.

EFFECT:

A file is created and the object self is saved into the file.

EXAMPLES:

sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved as BFI
sage: a = BFI([0,1,0,0])
sage: d = tmp_dir()
sage: a.save_mangled("a", dir=d)
sage: file_name = BFI.mangled_name("a.sobj", dir=d)
sage: os.path.isfile(file_name)
True
sage: BFI.remove_mangled("a", dir=d)
sage: os.rmdir(d)