forked from anselale/Dignity
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheckpoint_manager.py
More file actions
36 lines (32 loc) · 1.43 KB
/
checkpoint_manager.py
File metadata and controls
36 lines (32 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import json
class CheckpointManager:
def __init__(self, checkpoint_file):
self.checkpoint_file = checkpoint_file
def load(self):
"""Load the checkpoint file and return a set of processed items."""
if os.path.exists(self.checkpoint_file):
try:
with open(self.checkpoint_file, "r", encoding="utf-8") as f:
data = json.load(f)
if isinstance(data, dict):
# data is a dictionary with keys as processed items
return set(data.keys())
elif isinstance(data, list):
# fallback if stored as a list
return set(data)
else:
return set()
except Exception as e:
print(f"Error loading checkpoint file {self.checkpoint_file}: {e}")
return set()
return set()
def save(self, processed_set):
"""Save the set of processed items as a dictionary with keys in alphabetical order."""
data = {item: True for item in processed_set}
with open(self.checkpoint_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, sort_keys=True)
def mark_processed(self, item, processed_set):
"""Add an item to the processed set and save it immediately."""
processed_set.add(item)
self.save(processed_set)