Module: vemomoto_core.tools.saveobject#

This module contains methods and classes to save and load objects.

The module contains a method save_object() that saves any object without overwriting existing identical sections. Furthermore, there is a class SeparatelySaveable that can be used as the base class for all objects for which some attributes shall be saved in separate files. Attributes that are instances of SeparatelySaveable also will be saved separately automatically. Further attributes that shall be saved separately can be specified via SeparatelySaveable.set_save_separately().

Attributes that are saved separately are placed in a folder next to the file to which the original object is saved. These attributes will be saved again only if they have been accessed after they have been saved initially. When the object is loaded, the separate attributes will not be loaded until they are accessed.

Usage#

Saving an object without overwriting similar parts works for all objects:

save_object(myObject)

Saving attributes separately:

# defining classes
class MyClass1(SeparatelySaveable):
    def __init__(self, value):
        super().__init__()
        self.attribute1 = value

        # specify that self.attribute shall be
        # saved separately
        self.set_save_separately('attribute')

class MyClass2(SeparatelySaveable):
    def __init__(self, value):
        super().__init__()

        # attributes that are instances of
        # SeparatelySaveable will always be saved separately
        self.attribute2 = MyClass1(value)


# creating objects
myObject1 = MyClass1()
myObject2 = MyClass2()

# Saves myObject1 to fileName1.ext and
# myObject1.attribute1 to fileName1.arx/attribute1.ext
myObject1.save_object("fileName1", ".ext", ".arx")

# Saves myObject2 to fileName2.ext and
# myObject2.attribute2 to fileName2.arx/attribute2.ext and
# myObject2.attribute2.attribute1 to fileName2.arx/attribute2.arx/attribute1.ext
myObject1.save_object("fileName2", ".ext", ".arx")

# load myObject2; myObject2.attribute2 will remain unloaded
loadedObject = load_object("fileName2.ext")

# myObject2.attribute1 will be loaded; myObject2.attribute2.attribute1
# will remain unloaded
loadedObject.attribute2

# Saves loadedObject to fileName2.ext and
# loadedObject.attribute2 to fileName2.arx/attribute2.ext
# loadedObject.attribute2.attribute1 will remain untouched
loadedObject.save_object("fileName2", ".ext", ".arx")

Data:

BLOCKSIZE

Size of read/write blocks when files are saved

DEFAULT_EXTENSION

File name extension used if no extension is specified

DEFAULT_FOLDER_EXTENSION

Folder name extension used if no extension is specified

Classes:

SeparatelySaveable([extension, folderExtension])

Functions:

load_object(fileName)

Load an object.

save_object(obj, fileName[, compare])

Save an object.

BLOCKSIZE = 1048576#

Size of read/write blocks when files are saved

DEFAULT_EXTENSION = ''#

File name extension used if no extension is specified

DEFAULT_FOLDER_EXTENSION = '.arx'#

Folder name extension used if no extension is specified

class SeparatelySaveable(extension='', folderExtension='.arx')[source]#

Bases: object

Methods:

del_save_separately(*name)

hasattr(name)

load_all()

save_object(fileName[, extension, ...])

set_save_separately(*name)

del_save_separately(*name)[source]#
hasattr(name)[source]#
load_all()[source]#
save_object(fileName, extension=None, folderExtension=None, overwriteChildExtension=False)[source]#
set_save_separately(*name)[source]#
load_object(fileName)[source]#

Load an object.

Parameters

fileName (str) – Path to the file

save_object(obj, fileName, compare=True)[source]#

Save an object.

If the object has been saved at the same file earlier, only the parts are overwritten that have changed. Note that an additional attribute at the beginning of the file will ‘shift’ all data, making it necessary to rewrite the entire file.

Parameters
  • obj (object) – Object to be saved

  • fileName (str) – Path of the file to which the object shall be saved

  • compare (bool) – Whether only changed parts shall be overwitten. A value of True will be beneficial for large files if no/few changes have been made. A value of False will be faster for small and strongly changed files.