Compare two models#
Diff two model files so that the diff shows only what the models mean differently. A plain text diff also shows the order of declarations, spacing and the order of the terms in a sum. The canonical form removes those differences. Comparing two models lists what the form sorts and what it keeps.
- Write one model in the canonical form.
The form goes to stdout. -o canonical.yaml writes it to a file instead. A
file the language refuses prints its message on stderr and exits with
status 1.
- Diff two files once. Give each file to the command, and diff the two outputs:
- Make
git diffshow the canonical form. Tell git which files are models, and which command writes them out:
echo 'models/*.yaml diff=mathspec' >> .gitattributes
git config diff.mathspec.textconv "python -m mathspec canonical"
The files in the repository stay as you wrote them. Only the diff changes.
A commit that writes sum(dispatch * cost) as sum(cost * dispatch) shows
no difference. A commit that changes a coefficient shows one line:
objective:
sense: minimize
- expression: sum(cost * dispatch)
+ expression: sum((2 * cost) * dispatch)
Match only model files in .gitattributes. Git runs the command on every
file the pattern matches, and a YAML file that is not a model fails to load.
- Compare from Python where the comparison is one step of a longer
script.
to_yamlwrites the same text withcanonical=True: