Skip to content

Releases

v0.4.0: a persistent store

Choose a tag to compare

@zalmane zalmane released this 29 Nov 12:30
81a4d8f
  • 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 a Layout interface that another arrangement can fill in (it uses orjson, now a dependency, for fast loads; pip install --no-deps pytracemodel restrictedpython runs without it, with the same results). See the saving guide.

v0.3.0

Choose a tag to compare

@zalmane zalmane released this 29 Nov 12:31
79f32b3
  • Yes/no items: add_item(..., dtype=bool) (or "bool") holds True or False. set_value rejects anything
    else, so a flag can no longer be 2; a formula or check uses it as a plain bool (if x["firm"],
    expect=lambda x: x["firm"]); tables show yes and no; it is saved as true and false. When the source does
    not say, enter tm.NIL, which is not False. Before, a flag had to be an int item of 0 and 1. Closes #43.

  • model.cells() reads many cells at once: model.cells(use="offices") returns a Table with 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 is missing and does not raise, so one gap no longer
    stops a read of the rest. Before, reading across items took one get or entry per 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 into section as text. It is copied, saved and reloaded as it was. The
    Excel Values sheet gets a column per extra key. Breaking: document, url and section are 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 a tm.Label, a str that also knows its .description, its read-only
    .attrs and its .dimension; dim.labels lists them. It compares, hashes and prints as its code, so it goes wherever a
    label string does, and dim.coords stays plain text.

  • Dimension description and attrs: a dimension has its own description (what people call it, "Unit type"; the name
    stays what formulas use) and attrs (free-form facts about it), like an item and a label:
    add_dimension(name, coords, description=..., attrs=...) and update_dimension(name, description=..., attrs=...).
    Saved with the model and versioned (dimension.update). Breaking: dim.description(label) and dim.attrs(label)
    are gone; use dim[label].description and dim[label].attrs.

  • Review: an agent drafts on a branch (a repo opened with self_approve=False); a person
    reads it with repo.review(branch) (each change with its author, and the
    calculated cells it moves), changes it on the branch, rules with repo.accept / reject /
    comment on 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) and tm.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.at takes a branch name.

  • Versions: tm.Repo.open(path, author) keeps a model under version control in an
    append-only log. repo.model is 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) and repo.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 a tm.Op
    of plain data, which tm.ops.replay applies to another model through the same verbs and
    tm.ops.invert undoes.

  • Verbs that undo: remove_item, set_formula, update_item, remove_check,
    clear_value and remove_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, .str gives
    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
    DefinitionError instead of being False or a TypeError.

  • to_excel nests 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, in and overlap use the weeks.

v0.2.0: lambda formulas, tracing, versions and review

Choose a tag to compare

@zalmane zalmane released this 07 Nov 13:39
187ea53

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 pytracemodel

Highlights

  • 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(...) and model.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 reads repo.review(branch), accepts or rejects each change, and repo.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 .str and .dt methods.
  • Labels are objects. dim["Q1"].description and dim["Q1"].attrs. Dimensions have their own description and attrs.
  • Undo verbs. remove_item, set_formula, update_item, remove_check, clear_value and remove_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"] raises DefinitionError. Write formula=lambda x: x["revenue"] * 0.6.
  • Checks are lambdas of the run x. Use and, or, not in place of &, |, ~.
  • dim.description(label) and dim.attrs(label) are replaced by dim[label].description and dim[label].attrs.
  • A source no longer requires document, url or section.
  • 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.