Core¶
The history model and the stores in trrack.
Stores¶
Store
¶
JsonFileStore(path)
¶
A Store that round-trips the payload through one JSON file.
Save to and load from the JSON file at path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
The path of the file. Missing parent folders are created on the first save. |
required |
to_state(payload)
¶
Get the plain tracked values out of a saved payload.
Use it in a store's save when the store keeps the values in its own
shape, such as one URL parameter per value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
dict
|
The payload passed to |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The tracked values, keyed by name. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the payload is a |
Examples:
def save(self, data: dict) -> None:
for name, value in to_state(data).items():
self._write_value(name, value)
from_state(state)
¶
Turn plain tracked values into a payload a store can return.
Use it in a store's load when the store keeps the values in its own
shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict
|
The tracked values, keyed by name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
The payload to return from |
Examples:
def load(self) -> dict | None:
state = self._read_values()
return from_state(state) if state else None
History¶
ProvenanceGraph(root_state, *, now, root_id=None)
¶
A branching graph of full-state snapshots.
Each commit adds a node under the current node. You pass every timestamp
as now, so the graph never reads the clock.
Create the graph with a single root holding root_state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_state
|
dict
|
The state of the root node. |
required |
now
|
str
|
The creation time of the root. Any string, such as an ISO 8601 time. |
required |
root_id
|
str
|
The id of the root. Defaults to a new random id. |
None
|
current
property
¶
The node the graph is currently positioned at.
is_at_latest
property
¶
Whether the current node is a leaf (nothing to redo).
is_at_root
property
¶
Whether the current node is the root (nothing to undo).
root
property
¶
The root node the graph started from.
commit(state, *, now, label=None, node_id=None)
¶
Append state as a child of the current node and move there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
dict
|
The state of the new node. |
required |
now
|
str
|
The creation time. Any string, such as an ISO 8601 time. |
required |
label
|
str
|
The label of the new node. Defaults to a list of the values that changed. |
None
|
node_id
|
str
|
The id of the new node. Defaults to a new random id. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Node |
Node
|
The new node. |
from_dict(data)
classmethod
¶
Rebuild a graph from to_dict() output.
go_to(node_id)
¶
Move to the node with id node_id.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_id
|
str
|
The id of the node to go to. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Node |
Node
|
The node that is now current. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no node has this id. |
redo()
¶
Move to the newest child of the current node, or None at a leaf.
relabel(text, node_id=None)
¶
Set the label of node_id (default: the current node) to text.
to_dict()
¶
Serialize the whole graph to plain JSON-compatible data.
undo()
¶
Move to the parent of the current node, or None at the root.
Node(id, parent, children, created_at, label, state)
dataclass
¶
A full-state snapshot and its position in the graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str
|
The unique id of the node. |
required |
parent
|
str | None
|
The id of the parent node, or |
required |
children
|
list[str]
|
The ids of the child nodes, oldest first. |
required |
created_at
|
str
|
The creation time. Any string, such as an ISO 8601 time. |
required |
label
|
str
|
The text that names the node. |
required |
state
|
dict
|
The tracked values at this node, keyed by name. |
required |