Daff: Data Diff and Patch Tables

1.4.2 · active · verified Sun Mar 29

daff (data diff) is a Python library for comparing tables, producing a summary of their differences, and applying such summaries as patch files. It is optimized for comparing tables that share a common origin, effectively tracking changes between versions of the 'same' table. The library is actively maintained with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to compare two Python list-of-lists tables using `daff`, generate a diff, and render it as an HTML string for visualization. It uses `PythonTableView` to adapt standard Python data structures for `daff`'s comparison algorithms.

import daff

data1 = [
    ['Country', 'Capital'],
    ['Ireland', 'Dublin'],
    ['France', 'Paris'],
    ['Spain', 'Barcelona']
]

data2 = [
    ['Country', 'Code', 'Capital'],
    ['Ireland', 'ie', 'Dublin'],
    ['France', 'fr', 'Paris'],
    ['Spain', 'es', 'Madrid'],
    ['Germany', 'de', 'Berlin']
]

# Create table views for the data
table1 = daff.PythonTableView(data1)
table2 = daff.PythonTableView(data2)

# Compare tables to get alignment information
alignment = daff.Coopy.compareTables(table1, table2).align()

# Prepare an empty table to hold the diff results
data_diff = []
table_diff = daff.PythonTableView(data_diff)
flags = daff.CompareFlags()
highlighter = daff.TableDiff(alignment, flags)
highlighter.hilite(table_diff)

# Render the diff to HTML for visualization
diff_render = daff.DiffRender()
diff_render.usePrettyArrows(False) # Optional: control arrow style
diff_render.render(table_diff)
table_diff_html = diff_render.html()

print("Original Table 1:", data1)
print("Modified Table 2:", data2)
print("\nHTML Diff:\n", table_diff_html)

view raw JSON →