pyrit.memory.MemoryExporter#

class MemoryExporter[source]#

Bases: object

Handles the export of data to various formats, currently supporting only JSON format. This class utilizes the strategy design pattern to select the appropriate export format.

__init__()[source]#

Methods

__init__()

export_data(data, *[, file_path, export_type])

Exports the provided data to a file in the specified format.

export_to_csv(data[, file_path])

Exports the provided data to a CSV file at the specified file path.

export_to_json(data[, file_path])

Exports the provided data to a JSON file at the specified file path.

model_to_dict(model_instance)

Converts an SQLAlchemy model instance into a dictionary, serializing special data types such as UUID and datetime to string representations.

export_data(data: List[Base] | List[Dict], *, file_path: Path = None, export_type: str = 'json')[source]#

Exports the provided data to a file in the specified format.

Parameters:
  • data (Union[List[Base], List[Dict]]) – The data to be exported, typically a list of SQLAlchemy model instances or as a list of dictionaries.

  • file_path (str) – The full path, including the file name, where the data will be exported.

  • export_type (str, Optional) – The format for exporting data. Defaults to “json”.

Raises:

ValueError – If no file_path is provided or if the specified export format is not supported.

export_to_csv(data: List[Base] | List[Dict], file_path: Path = None) None[source]#

Exports the provided data to a CSV file at the specified file path. Each item in the data list, representing a row from the table, is converted to a dictionary before being written to the file.

Parameters:
  • data (Union[List[Base], List[Dict]]) – The data to be exported, as a list of SQLAlchemy model instances or as a list of dictionaries.

  • file_path (Path) – The full path, including the file name, where the data will be exported.

Raises:

ValueError – If no file_path is provided.

export_to_json(data: List[Base] | List[Dict], file_path: Path = None) None[source]#

Exports the provided data to a JSON file at the specified file path. Each item in the data list, representing a row from the table, is converted to a dictionary before being written to the file.

Parameters:
  • data (Union[List[Base], List[Dict]]) – The data to be exported, as a list of SQLAlchemy model instances or as a list of dictionaries.

  • file_path (Path) – The full path, including the file name, where the data will be exported.

Raises:

ValueError – If no file_path is provided.

model_to_dict(model_instance: Base)[source]#

Converts an SQLAlchemy model instance into a dictionary, serializing special data types such as UUID and datetime to string representations. This ensures compatibility with JSON and other serialization formats.

Parameters:

model_instance – An instance of an SQLAlchemy model.

Returns:

A dictionary representation of the model instance, with special types serialized.