Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
## 2024-05-24 - openpyxl read_only optimization
**Learning:** `openpyxl.load_workbook(..., read_only=True)` is significantly faster (1.75x) for parsing large files but requires explicit `wb.close()` (preferably in `try...finally`) as it keeps file handles open and `Workbook` objects may not support context managers in all versions.
**Action:** Use `read_only=True` for read-heavy Excel tasks and always ensure `close()` is called.

## 2024-05-25 - DocxXMLEditor ID Generation Optimization
**Learning:** Generating sequential IDs by scanning the full DOM on every insertion is O(N^2) for batch operations. Caching the next ID reduces this to O(N).
**Action:** When generating unique IDs in a document, initialize a counter by scanning once, then increment locally, while watching for manual insertions to keep the counter valid.
39 changes: 27 additions & 12 deletions skills/docx/scripts/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,26 @@ def __init__(
self.rsid = rsid
self.author = author
self.initials = initials
self._next_change_id = None

def _get_next_change_id(self):
"""Get the next available change ID by checking all tracked change elements."""
max_id = -1
for tag in ("w:ins", "w:del"):
elements = self.dom.getElementsByTagName(tag)
for elem in elements:
change_id = elem.getAttribute("w:id")
if change_id:
try:
max_id = max(max_id, int(change_id))
except ValueError:
pass
return max_id + 1
"""Get the next available change ID."""
if self._next_change_id is None:
max_id = -1
for tag in ("w:ins", "w:del"):
elements = self.dom.getElementsByTagName(tag)
for elem in elements:
change_id = elem.getAttribute("w:id")
if change_id:
try:
max_id = max(max_id, int(change_id))
except ValueError:
pass
self._next_change_id = max_id + 1

next_id = self._next_change_id
self._next_change_id += 1
return next_id

def _ensure_w16du_namespace(self):
"""Ensure w16du namespace is declared on the root element."""
Expand Down Expand Up @@ -168,6 +174,15 @@ def add_tracked_change_attrs(elem):
# Auto-assign w:id if not present
if not elem.hasAttribute("w:id"):
elem.setAttribute("w:id", str(self._get_next_change_id()))
elif self._next_change_id is not None:
# Update cache if we see a higher ID manually inserted
try:
current_id = int(elem.getAttribute("w:id"))
if current_id >= self._next_change_id:
self._next_change_id = current_id + 1
except ValueError:
pass

if not elem.hasAttribute("w:author"):
elem.setAttribute("w:author", self.author)
if not elem.hasAttribute("w:date"):
Expand Down