Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions graphgen/models/reader/pickle_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pickle
from typing import Any, Dict, List

from graphgen.bases.base_reader import BaseReader


class PickleReader(BaseReader):
"""
Read pickle files, requiring the top-level object to be List[Dict[str, Any]].
"""

def read(self, file_path: str) -> List[Dict[str, Any]]:
with open(file_path, "rb") as f:
data = pickle.load(f)

if not isinstance(data, list):
raise ValueError("Pickle file must contain a list of documents.")

for doc in data:
if not isinstance(doc, dict):
raise ValueError("Every item in the list must be a dict.")
if doc.get("type") == "text" and self.text_column not in doc:
raise ValueError(f"Missing '{self.text_column}' in document: {doc}")

return self.filter(data)