SPPAS 4.22

https://sppas.org/

Module sppas.src.resources

Class sppasDumpFile

Description

Class to manage dump files.

A dump file is a binary version of an ASCII file. Its size is greater than the original ASCII one but the time to load it is divided by two or three.

Constructor

Create a sppasDumpFile instance.

Parameters
  • filename: (str) Name of the ASCII file.
  • dump_extension: (str) Extension of the dump file.
View Source
def __init__(self, filename, dump_extension=''):
    """Create a sppasDumpFile instance.

    :param filename: (str) Name of the ASCII file.
    :param dump_extension: (str) Extension of the dump file.

    """
    self._dump_ext = sppasDumpFile.DUMP_FILENAME_EXT
    self._filename = filename
    self.set_dump_extension(dump_extension)

Public functions

set_filename

Fix the name of the ASCII file.

Parameters
  • filename: (str) Name of the ASCII file.
View Source
def set_filename(self, filename):
    """Fix the name of the ASCII file.

        :param filename: (str) Name of the ASCII file.

        """
    self._filename = filename

set_dump_extension

Fix the extension of the dump file.

Set to the default extension if the given extension is an empty string.

Parameters
  • extension: (str) Extension of the dump file (starting with or without the dot).
Raises

DumpExtensionError if extension of the dump file is the same as the ASCII file.

View Source
def set_dump_extension(self, extension=''):
    """Fix the extension of the dump file.

        Set to the default extension if the given extension is an empty
        string.

        :param extension: (str) Extension of the dump file         (starting with or without the dot).
        :raises: DumpExtensionError if extension of the dump file is         the same as the ASCII file.

        """
    if extension.startswith('.') is False:
        extension = '.' + extension
    if len(extension) == 1:
        extension = sppasDumpFile.DUMP_FILENAME_EXT
    file_name, file_ext = os.path.splitext(self._filename)
    if extension.lower() == file_ext.lower():
        raise DumpExtensionError(extension)
    self._dump_ext = extension

get_dump_extension

Return the extension of the dump version of filename.

View Source
def get_dump_extension(self):
    """Return the extension of the dump version of filename."""
    return self._dump_ext

get_dump_filename

Return the file name of the dump version of filename.

Returns
  • name of the dump file
View Source
def get_dump_filename(self):
    """Return the file name of the dump version of filename.

        :returns: name of the dump file

        """
    file_name, file_ext = os.path.splitext(self._filename)
    return file_name + self._dump_ext

has_dump

Test if a dump file exists for filename and if it is up-to-date.

Returns
  • (bool)
View Source
def has_dump(self):
    """Test if a dump file exists for filename and if it is up-to-date.

        :returns: (bool)

        """
    dump_filename = self.get_dump_filename()
    if os.path.isfile(dump_filename):
        tascii = os.path.getmtime(self._filename)
        tdump = os.path.getmtime(dump_filename)
        if tascii < tdump:
            return True
    return False

load_from_dump

Load the file from a dumped file.

Returns
  • loaded data or None
View Source
def load_from_dump(self):
    """Load the file from a dumped file.

        :returns: loaded data or None

        """
    if self.has_dump() is False:
        return None
    dump_filename = self.get_dump_filename()
    try:
        with codecs.open(dump_filename, 'rb') as f:
            data = pickle.load(f)
    except Exception as e:
        logging.info('Save a dumped data failed: {:s}'.format(str(e)))
        os.remove(dump_filename)
        return None
    return data

save_as_dump

Save the data as a dumped file.

Parameters
  • data: The data to save
Returns
  • (bool)
View Source
def save_as_dump(self, data):
    """Save the data as a dumped file.

        :param data: The data to save
        :returns: (bool)

        """
    dump_filename = self.get_dump_filename()
    try:
        with codecs.open(dump_filename, 'wb') as f:
            pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
    except Exception as e:
        logging.info('Save a dumped data failed: {:s}'.format(str(e)))
        return False
    return True