Repository navigation
Releases
v0.4.0: a persistent store
- A persistent store:
tracemodel.store.open_store(path, write=True)keeps named models in one file. A write recalculates only the cells it reaches, so a page of a large model is a seek, and each write is also logged so the file can be rebuilt. The cells sit behind aLayoutinterface that another arrangement can fill in (it uses orjson, now a dependency, for fast loads;pip install --no-deps pytracemodel restrictedpythonruns without it, with the same results). See the saving guide.
v0.3.0
-
Yes/no items:
add_item(..., dtype=bool)(or"bool") holdsTrueorFalse.set_valuerejects anything
else, so a flag can no longer be2; a formula or check uses it as a plainbool(if x["firm"],
expect=lambda x: x["firm"]); tables showyesandno; it is saved astrueandfalse. When the source does
not say, entertm.NIL, which is notFalse. Before, a flag had to be anintitem of0and1. Closes #43. -
model.cells()reads many cells at once:model.cells(use="offices")returns aTablewith one row per cell
(item,coords,value,kind,source) for every item that has that dim;model.cells(["fee", "rate"])reads
named items in full, scalars included. A cell with no value ismissingand does not raise, so one gap no longer
stops a read of the rest. Before, reading across items took onegetorentryper item. -
A source is free plain data, and optional:
set_value(..., source=None)takes a dict with any keys
(page,bbox,sha256, ...) whose values are text, numbers, true/false, none, lists and dicts of those, so
structured facts no longer need packing intosectionas text. It is copied, saved and reloaded as it was. The
Excel Values sheet gets a column per extra key. Breaking:document,urlandsectionare no longer
required and unknown keys are no longer rejected; a source that isn't plain data (a date object, say) raises
InvalidValue. A schema for sources and a policy on them are on the backlog. -
Labels are objects:
dim["PA"]returns atm.Label, astrthat also knows its.description, its read-only
.attrsand its.dimension;dim.labelslists them. It compares, hashes and prints as its code, so it goes wherever a
label string does, anddim.coordsstays plain text. -
Dimension description and attrs: a dimension has its own
description(what people call it, "Unit type"; the name
stays what formulas use) andattrs(free-form facts about it), like an item and a label:
add_dimension(name, coords, description=..., attrs=...)andupdate_dimension(name, description=..., attrs=...).
Saved with the model and versioned (dimension.update). Breaking:dim.description(label)anddim.attrs(label)
are gone; usedim[label].descriptionanddim[label].attrs. -
Review: an agent drafts on a branch (a repo opened with
self_approve=False); a person
reads it withrepo.review(branch)(each change with its author, and the
calculated cells it moves), changes it on the branch, rules withrepo.accept/reject/
commenton a cell, a slice, an item, a commit or all of it, and lands what was accepted with
repo.approve(branch), which re-runs the checks. Nobody approves their own changes;
repo.approved_by(...)says who let a value in. -
Diff:
repo.diff(a, b, calculated=True)andtm.diff(model_a, model_b)list the
definitions and entered values that differ, and the calculated cells that moved, each naming
the differing inputs and formulas it rests on. -
Branches:
repo.new_branch,checkout,branches,drop_branch, and
repo.rebase(branch, onto), which lands a branch's changes on another as one commit, checked
by the model's verbs, or refuses naming every object both sides changed differently
(repo.conflicts). History stays a line.repo.attakes a branch name. -
Versions:
tm.Repo.open(path, author)keeps a model under version control in an
append-only log.repo.modelis the branch's model;repo.commit(message)writes its pending
changes as one commit,repo.discard()drops them.repo.log(),repo.at(commit)(the model
at a commit, read-only),repo.history(item, **coords)andrepo.export(...)read it back.
The log's records are checksummed and hash-chained; a torn last line and a second writer are
caught. -
Recording changes:
with model.recording() as ops:records every change as atm.Op
of plain data, whichtm.ops.replayapplies to another model through the same verbs and
tm.ops.invertundoes. -
Verbs that undo:
remove_item,set_formula,update_item,remove_check,
clear_valueandremove_coords. Each refuses, with nothing changed, while a formula or a
check still reads what it would remove; in a transaction that is checked at commit. -
Text items:
add_item(..., dtype=str)(or"str"/"string") holds sourced text, such
as a contract's status or vendor. Text compares and joins in formulas and checks,.strgives
pandas-named methods (contains,lower,len, ...), empty text is rejected in favour of
tm.NIL, and Excel writes it as text. Comparing text with a number now raises
DefinitionErrorinstead of beingFalseor aTypeError. -
to_excelnests every dim down in the first column, as a pivot table's compact form. -
52/53-week fiscal years:
tm.period_range(..., freq="Y-SEP-SAT-LAST")(Apple's, ending on
the last Saturday of September) or"Y-JAN-SAT-NEAREST"(retail), as pandas'FY5253, and
13-week quarters,"Q-SEP-SAT-LAST", with the 53rd week in the first quarter or the one named
("Q-JAN-SAT-NEAREST-4").start_time,end_time,days,inandoverlapuse the weeks.
v0.2.0: lambda formulas, tracing, versions and review
tracemodel builds financial models in Python where every number has a fixed address, a source you can open, and rules that check it.
pip install pytracemodelHighlights
- One kind of formula. Every formula and check is a lambda of
x. tracemodel checks it against an allow-list and compiles it with RestrictedPython. It saves the lambda as source text, so a model file runs no other code. - Tracing.
model.precedents(...)andmodel.dependents(...)trace a cell, as Excel's Trace Precedents and Trace Dependents do.report.explain()lists each cell a check read. - Versions, branches and review.
tm.Repo.open(path, author)keeps a model in an append-only, hash-chained log. An agent drafts on a branch. A person readsrepo.review(branch), accepts or rejects each change, andrepo.approve(branch)lands only what was accepted.repo.diff(...)shows the calculated cells that moved. - Sources are plain data.
set_value(..., source={...})takes any keys (document,page,bbox,sha256, ...). The Excel Values sheet gets a column for each key. - Text and dates. Items can hold sourced text (
dtype=str) and dates, with pandas-named.strand.dtmethods. - Labels are objects.
dim["Q1"].descriptionanddim["Q1"].attrs. Dimensions have their own description and attrs. - Undo verbs.
remove_item,set_formula,update_item,remove_check,clear_valueandremove_coords. Each one refuses while a formula or a check still reads what it would remove.
Breaking changes from 0.1
- Arithmetic on
model["item"]raisesDefinitionError. Writeformula=lambda x: x["revenue"] * 0.6. - Checks are lambdas of the run
x. Useand,or,notin place of&,|,~. dim.description(label)anddim.attrs(label)are replaced bydim[label].descriptionanddim[label].attrs.- A source no longer requires
document,urlorsection. - Definition files are
tracemodel/2. Files from 0.1 still load.
See CHANGELOG.md for the full list.
Try it: apple.ipynb, a model of Apple's FY2024–FY2025 10-K.