ape.parts.storage.filestorage.FileStorage

class ape.parts.storage.filestorage.FileStorage(path=None, timestamp='%Y_%m_%d_%I:%M:%S_%p', name=None, overwrite=False, mode='w')

A class to store data to a file

FileStorage constructor

Param:
  • path: path to prepend to all files (default is current directory)
  • timestamp: strftime format to timestamp file-names
  • name: Filename to use
  • overwrite: If true, clobber existing file with same name
  • mode: file mode (e.g. ‘a’ for append)
__init__(path=None, timestamp='%Y_%m_%d_%I:%M:%S_%p', name=None, overwrite=False, mode='w')

FileStorage constructor

Param:
  • path: path to prepend to all files (default is current directory)
  • timestamp: strftime format to timestamp file-names
  • name: Filename to use
  • overwrite: If true, clobber existing file with same name
  • mode: file mode (e.g. ‘a’ for append)

Methods

__init__([path, timestamp, name, overwrite, ...]) FileStorage constructor
log_error(error[, message]) Logs the error in bold red

Attributes

logger
return:A logging object.
path The path to prepend to files (cwd if not set by client)
close()

Closes self.file if it exists, sets self.closed to True

log_error(error, message='')

Logs the error in bold red

Param:
  • error: error type (prefix in red and bold)
  • message: descriptive message (red but not bold)
open(name, overwrite=False, mode='w', return_copy=True)

Opens a file for writing

Param:
  • name: a basename (no path) for the file
  • overwrite: If True, clobber existing files with the same name
  • mode: file-mode (e.g. ‘w’ or ‘a’)
  • return_copy: If True, return a copy of self, otherwise return self
Returns:

copy of self with file as open file and closed set to False

safe_name(name, overwrite=False)

Adds a timestamp if formatted for it, increments if already exists

Param:
  • name: name for file (without path added)
  • overwrite: if True, don’t mangle the name
Returns:

unique name with full path

write(text, exceptions=(<type 'exceptions.AttributeError'>, <type 'exceptions.ValueError'>))

Writes the text to the file

Param:
  • text: text to write to the file
  • exceptions: exceptions to catch if the file is closed
Raise:

ApeError if one of the exceptions is raised

writeline(text)

Adds newline to end of text and writes it to the file

writelines(texts, exceptions=(<type 'exceptions.AttributeError'>, <type 'exceptions.ValueError'>))

Writes the lines to the file

Param:
  • texts: collection of strings
  • exceptions: exceptions to catch if the file is closed
logger
Returns:A logging object.
path

The path to prepend to files (cwd if not set by client)

writeable

checks if the file is open for writing

Navigation