-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: In-place Dictionary Mutations for JSON File Adapters #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,15 +46,22 @@ def save_items(self, items: list[LearningItem]) -> None: | |
|
|
||
| storage = self._load_storage() | ||
| raw_items = storage.get("items", []) | ||
| existing_items = [] | ||
| if isinstance(raw_items, list): | ||
| existing_items = [ | ||
| _item_from_dict(entry) for entry in raw_items if isinstance(entry, dict) | ||
| ] | ||
| by_id = {existing.id: existing for existing in existing_items} | ||
| for item in items: | ||
| by_id[item.id] = item | ||
| storage["items"] = [_item_to_dict(entry) for entry in by_id.values()] | ||
| if not isinstance(raw_items, list): | ||
| raw_items = [] | ||
|
|
||
| items_to_save = {str(item.id): _item_to_dict(item) for item in items} | ||
|
|
||
| updated_raw_items = [] | ||
| for entry in raw_items: | ||
| if isinstance(entry, dict): | ||
| entry_id = str(entry.get("id")) | ||
| if entry_id in items_to_save: | ||
| updated_raw_items.append(items_to_save.pop(entry_id)) | ||
| else: | ||
| updated_raw_items.append(entry) | ||
|
Comment on lines
+55
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of this loop has a bug that can lead to data loss. If for entry in raw_items:
if isinstance(entry, dict) and str(entry.get("id")) in items_to_save:
updated_raw_items.append(items_to_save.pop(str(entry.get("id"))))
else:
updated_raw_items.append(entry) |
||
|
|
||
| updated_raw_items.extend(items_to_save.values()) | ||
| storage["items"] = updated_raw_items | ||
| self._save_storage(storage) | ||
|
|
||
| def list_attempts(self) -> list[Attempt]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The date in this new entry's heading,
2025-03-22, is in the future. This is likely a typo and should probably be for the current year,2024, to maintain chronological order and avoid confusion.