ape.parts.storage.csvstorage.CsvDictStorage

class ape.parts.storage.csvstorage.CsvDictStorage(headers, path=None, storage=None)

A storage that writes to csv files

CsvDictStorage constructor

Param:
  • path: path to folder to store output-file in
  • storage: file-like object to use instead of creating one from ‘path’
  • headers: list of column headers in order required
Raises:

ApeError if neither path nor storage given

__init__(headers, path=None, storage=None)

CsvDictStorage constructor

Param:
  • path: path to folder to store output-file in
  • storage: file-like object to use instead of creating one from ‘path’
  • headers: list of column headers in order required
Raises:

ApeError if neither path nor storage given

Methods

__init__(headers[, path, storage]) CsvDictStorage constructor
log_error(error[, message]) Logs the error in bold red
open(filename) Opens the filename as a DictWriter
writerow(rowdict) Writes the row to storage
writerows(rowdicts) Writes each dictionary in rowdicts to the csv

Attributes

logger
return:A logging object.
storage A file-storage created from the path (unless passed into constructor)
writer DictWriter instance (creates it if not set)
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(filename)

Opens the filename as a DictWriter

Param:
  • filename: the name of the file to open
Postcondition:

header written to file

Returns:

copy of self with open DictWriter as writer

writerow(rowdict)

Writes the row to storage

Param:
  • rowdict: dict whose keys match the headers
Raise:

ApeError if keys don’t match header or invalid data was passed in.

writerows(rowdicts)

Writes each dictionary in rowdicts to the csv

Param:
  • rowdicts: iterable collection of dictionaries
Raise:

ApeError (see writerow)

logger
Returns:A logging object.
storage

A file-storage created from the path (unless passed into constructor)

Returns:FileStorage
writer

DictWriter instance (creates it if not set)

Postcondition:if dictwriter created, header written to file
Raise:ApeError if not set and storage not writeable

Navigation