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.
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.
Create a sppasDumpFile instance.
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)
Fix the name of the ASCII file.
def set_filename(self, filename):
"""Fix the name of the ASCII file.
:param filename: (str) Name of the ASCII file.
"""
self._filename = filename
Fix the extension of the dump file.
Set to the default extension if the given extension is an empty string.
DumpExtensionError if extension of the dump file is the same as the ASCII file.
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
Return the extension of the dump version of filename.
def get_dump_extension(self):
"""Return the extension of the dump version of filename."""
return self._dump_ext
Return the file name of the dump version of filename.
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
Test if a dump file exists for filename and if it is up-to-date.
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 the file from a dumped file.
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 the data as a dumped file.
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