DiffFinder
Check a Python function against a reference solution and see exactly how they differ.
Give DiffFinder your function and a known-good one. It runs both on the same input and tells you whether they behave the same, and if not, what is different: the result, an exception, or a change to the input.
pip install tracecomparekit
Motivation
When a program gives the wrong answer, a beginner often cannot tell what is wrong, only that something is. But frequently a correct solution already exists (the lecturer's, a textbook's, a classmate's). Then the useful question is not "what does my code do?" but "how does my code's behaviour differ from the right one?"
What makes it different
- You supply a reference function, not hand-written expected values. There is nothing to calculate by hand, and the reference can be written in any style.
- It compares behaviour, not source code. A loop and a formula that give the same answers are reported as the same. A text diff of the two would be useless.
- It catches what a printed return value hides: a function that crashes where the reference does not, or one that changes its input list in place when the reference leaves it alone.
- No dependencies, so it installs next to anything, including on a shared notebook server.
Quick start
from difffinder import compare
def count_to(n): # your version
total = 0
for i in range(n):
total += i
return total
def count_to_ref(n): # the reference
total = 0
for i in range(n + 1):
total += i
return total
compare(count_to, count_to_ref, args=(4,))
DIFFERENT: count_to and count_to_ref behave differently on (n=4).
result: yours returned 6, the reference returned 10.
Different algorithms with the same behaviour are reported as the same:
def formula(n):
return n * (n + 1) // 2
compare(count_to_ref, formula, args=(4,))
SAME: count_to_ref and formula behave the same on (n=4).
Crashes and changes to the input are shown too:
def sort_in_place(items):
items.sort()
return items
def sorted_copy(items):
return sorted(items)
compare(sort_in_place, sorted_copy, args=([3, 1, 2],))
DIFFERENT: sort_in_place and sorted_copy behave differently on (items=[3, 1, 2]).
argument 'items' afterwards: yours [1, 2, 3], the reference [3, 1, 2].
def first_item(items):
return items[0]
def first_or_none(items):
return items[0] if items else None
compare(first_item, first_or_none, args=([],))
DIFFERENT: first_item and first_or_none behave differently on (items=[]).
result: yours raised IndexError: list index out of range, the reference returned None.
Usage
compare(candidate, reference, args=(), kwargs=None)
| Part | Meaning |
|---|---|
candidate |
your function (the one you want to check) |
reference |
the known-good function |
args |
a tuple of positional arguments, for example (4,) or ([3, 1, 2], 5) (note the comma in a one-item tuple) |
kwargs |
a dict of keyword arguments, for example {"reverse": True} |
It returns a Comparison with .same (True or False), .differences (a list), and .candidate /
.reference, which hold what each function did. run(func, args) runs a single function and records the same
information.
When are two functions "the same"?
On the input you give, they must have:
- the same return value (compared with
==), or both raise the same kind of exception, and - the same final state of every argument they received.
Each function runs on its own deep copy of your arguments, so your data is never changed.
Limitations
- It checks one input per call. A function can match on your input and still be wrong on another, so try several (such as an empty list, zero, or a negative number).
- The functions must finish. It cannot yet stop an endless loop.
- Anything a function does besides returning or changing its arguments (printing, files, network) happens once per function.
- Parameters are matched by position, so the two functions may use different parameter names.
Trace a difference
compare_traces() records changes to local variables and shows where the two runs first
diverge. The report displays as text in the terminal and as a table in Jupyter.
from difffinder import compare_traces, find_difference
report = compare_traces(count_to, count_to_ref, args=(4,), probe=False)
print(report)
first = find_difference(count_to, count_to_ref, [0, 1, 4])
print(first.args) # (1,)
By default, compare_traces() also runs the functions on variations of the positional inputs
and tries to shrink a failing example. Use probe=False to run each function once.
Pass watch=["total"] to compare a particular variable. Offline hints appear in the report.
For an optional model-generated hint, pass ai=your_callable. The callable receives a prompt
containing the functions' source, input, and comparison report.
Sorting visualizer
With Tkinter installed, run python -m difffinder.sorting_visualizer. Choose two sorting
functions, enter a list, and click Compare & Record to step through their changes side by side.
For a terminal example, run python -m difffinder.demo --console.
Development
git clone https://github.com/clemonl/DiffFinder
cd difffinder
pip install -e ".[dev]"
pytest
Requirements are listed in SPEC.md and the division of work in TEAM.md. Released under the MIT license.
Metadata
Release files for tracecomparekit 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tracecomparekit-0.1.0.tar.gz | 40.1 kB | Without provenance | Details for tracecomparekit-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tracecomparekit-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for tracecomparekit-0.1.0-py3-none-any.whl |
Total release size: 79.2 kB
Release files / tracecomparekit-0.1.0.tar.gz
| Download URL | tracecomparekit-0.1.0.tar.gz |
|---|---|
| Size | 40.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
703593133d6e86b655b023fe31d2e07b8a0f66dc1e7f77d2b7926c763aec5327
|
|
BLAKE2b-256 checksum How to use checksums |
14d98dfc4b2450f3ca9418f1181af1ac04564ad75f12d7d713a75cd20579aa27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / tracecomparekit-0.1.0-py3-none-any.whl
| Download URL | tracecomparekit-0.1.0-py3-none-any.whl |
|---|---|
| Size | 39.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a34d8d1db29763304a10d1922ab5f81be7e469c7fb77e3fd71181c5e53dde348
|
|
BLAKE2b-256 checksum How to use checksums |
39248e31afc7d31bc1746c9e1cd73f9c571cc8e76d223ce0fd556ca7f3cc355d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Security for tracecomparekit 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.