-
Notifications
You must be signed in to change notification settings - Fork 0
Preserve typed ObjectState history across restarts #5
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
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Typed ObjectState history document persistence.""" | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass | ||
| from enum import Enum | ||
| from pathlib import Path | ||
|
|
||
| from objectstate import ObjectState, ObjectStateRegistry | ||
|
|
||
|
|
||
| class HistoryMode(Enum): | ||
| """Representative nominal configuration value.""" | ||
|
|
||
| FAST = "fast" | ||
|
|
||
|
|
||
| def identity(value): | ||
| """Representative importable callable configuration value.""" | ||
|
|
||
| return value | ||
|
|
||
|
|
||
| @dataclass | ||
| class HistoryConfig: | ||
| output_path: Path | ||
| mode: HistoryMode | ||
| transform: Callable | ||
|
|
||
|
|
||
| def _reset_registry_history() -> None: | ||
| ObjectStateRegistry._snapshots.clear() | ||
|
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. This helper leaves the Severity: medium 🤖 Was this useful? React with 👍 or 👎 |
||
| ObjectStateRegistry._timelines.clear() | ||
| ObjectStateRegistry._current_timeline = "main" | ||
| ObjectStateRegistry._current_head = None | ||
|
|
||
|
|
||
| def test_history_file_round_trips_typed_values(tmp_path: Path) -> None: | ||
| state = ObjectState( | ||
| HistoryConfig( | ||
| output_path=tmp_path / "results", | ||
| mode=HistoryMode.FAST, | ||
| transform=identity, | ||
| ), | ||
| scope_id="typed_history", | ||
| ) | ||
| ObjectStateRegistry.register(state, _skip_snapshot=True) | ||
| ObjectStateRegistry.record_snapshot("typed values", scope_id=state.scope_id) | ||
| history_path = tmp_path / "history.objectstate" | ||
|
|
||
| ObjectStateRegistry.save_history_to_file(str(history_path)) | ||
| _reset_registry_history() | ||
| ObjectStateRegistry.load_history_from_file(str(history_path)) | ||
|
|
||
| snapshot = ObjectStateRegistry.get_branch_history()[-1] | ||
| parameters = snapshot.all_states[state.scope_id].parameters | ||
| assert parameters["output_path"] == tmp_path / "results" | ||
| assert type(parameters["output_path"]) is type(tmp_path) | ||
| assert parameters["mode"] is HistoryMode.FAST | ||
| assert parameters["transform"] is identity | ||
| assert history_path.stat().st_size > 0 | ||
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.
Deep Code Review Agent🐛
The README example now points users at
history.objectstate, butdocs/undo_redo.rststill documentssave_history_to_file()/load_history_from_file()as JSON persistence. That generated documentation will describe the removed JSON contract after this PR, which can mislead users into treating the new dill file as portable JSON.Severity: low
🤖 Was this useful? React with 👍 or 👎