Skip to main content

Python bindings for diffx - semantic diffing of JSON, YAML, TOML, XML, INI, and CSV files. Powered by Rust for blazing fast performance.

Project description

diffx

CI PyPI License: MIT

Python bindings for diffx - semantic diff for structured data (JSON, YAML, TOML, XML, INI, CSV). Powered by Rust via PyO3 for blazing fast performance.

Installation

pip install diffx

Supported Platforms

Platform Architecture
Linux x64 (glibc)
Linux x64 (musl/Alpine)
Linux ARM64
macOS x64 (Intel)
macOS ARM64 (Apple Silicon)
Windows x64

Usage

Basic Diff

import diffx

old = {"name": "Alice", "age": 30}
new = {"name": "Alice", "age": 31, "city": "Tokyo"}

results = diffx.diff(old, new)

for change in results:
    print(f"{change['type']}: {change['path']}")
    # Modified: age
    # Added: city

With Options

results = diffx.diff(data1, data2,
    epsilon=0.001,                      # Tolerance for float comparison
    array_id_key='id',                  # Match array elements by ID
    ignore_keys_regex='timestamp|updatedAt',  # Ignore keys matching regex
    path_filter='user',                 # Only show diffs in paths containing "user"
    ignore_case=True,                   # Ignore case differences
    ignore_whitespace=True,             # Ignore whitespace differences
)

Parsers

Parse various formats to Python objects:

import diffx

json_obj = diffx.parse_json('{"name": "Alice"}')
yaml_obj = diffx.parse_yaml('name: Alice\nage: 30')
toml_obj = diffx.parse_toml('name = "Alice"')
csv_list = diffx.parse_csv('name,age\nAlice,30')
ini_obj = diffx.parse_ini('[user]\nname = Alice')
xml_obj = diffx.parse_xml('<user><name>Alice</name></user>')

Format Output

import diffx

results = diffx.diff(old, new)
print(diffx.format_output(results, 'json'))   # JSON format
print(diffx.format_output(results, 'yaml'))   # YAML format
print(diffx.format_output(results, 'diffx'))  # diffx format

File Comparison

import diffx

# Compare files (auto-detects format from extension)
results = diffx.diff_files('old.json', 'new.json')
results = diffx.diff_files('config1.yaml', 'config2.yaml', epsilon=0.1)

# Compare strings
json1 = '{"name": "Alice", "age": 30}'
json2 = '{"name": "Alice", "age": 31}'
results = diffx.diff_strings(json1, json2, 'json')

API Reference

diff(old, new, **kwargs)

Compare two values and return differences.

Options (kwargs):

Option Type Description
epsilon float Tolerance for floating-point comparisons
array_id_key str Key to identify array elements
ignore_keys_regex str Regex pattern for keys to ignore
path_filter str Only show diffs in matching paths
output_format str Output format ("diffx", "json", "yaml")
ignore_whitespace bool Ignore whitespace differences
ignore_case bool Ignore case differences
brief_mode bool Report only whether objects differ
quiet_mode bool Suppress normal output

Returns: List of diff results:

# For Added/Removed:
{"type": "Added", "path": "key", "value": ...}
{"type": "Removed", "path": "key", "value": ...}

# For Modified/TypeChanged:
{"type": "Modified", "path": "key", "old_value": ..., "new_value": ...}
{"type": "TypeChanged", "path": "key", "old_value": ..., "new_value": ...}

Parsers

  • parse_json(content: str) -> Any
  • parse_yaml(content: str) -> Any
  • parse_toml(content: str) -> dict
  • parse_csv(content: str) -> list[dict]
  • parse_ini(content: str) -> dict
  • parse_xml(content: str) -> dict

Utility Functions

  • format_output(results: list, format: str) -> str - Format diff results as string
  • diff_files(file1: str, file2: str, **kwargs) -> list - Compare two files
  • diff_strings(str1: str, str2: str, format: str, **kwargs) -> list - Compare two strings

Exception

  • DiffError - Raised when diff operations fail

Development

# Setup
uv sync --all-extras

# Build
uv run maturin develop

# Test
uv run pytest -v

# Format & Lint
cargo fmt --check
cargo clippy

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

diffx_python-0.7.2-cp311-cp311-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.11Windows x86-64

diffx_python-0.7.2-cp311-cp311-musllinux_1_1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

diffx_python-0.7.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

diffx_python-0.7.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

diffx_python-0.7.2-cp311-cp311-macosx_11_0_arm64.whl (1.2 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

diffx_python-0.7.2-cp311-cp311-macosx_10_12_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.11macOS 10.12+ x86-64

File details

Details for the file diffx_python-0.7.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for diffx_python-0.7.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4b140fe13e1ae3f2b411d3306284a7c78eb1ca1aeb6741e7011ef041c0ace586
MD5 16481380100e258b39e4f3cf44d25b93
BLAKE2b-256 9533a65d64cbb4fe8e06448efebcc2707cf09596befee5d2fc2ae56ed087e3f6

See more details on using hashes here.

File details

Details for the file diffx_python-0.7.2-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for diffx_python-0.7.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 051ed95c3706ff712c62ab58005d60120ee4ed7a207ee213c5d1ce1f28e7895f
MD5 4f687e0c93a2f82156e230bd55d2f2ca
BLAKE2b-256 59d7c2d3b17cfca3f8baeee058c73c591319edd1cca4b718c2fb11d2ad7509e8

See more details on using hashes here.

File details

Details for the file diffx_python-0.7.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for diffx_python-0.7.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6ec00d54be726f559121abe17a391d213ec6bd4d2cb778a8f76e217cf13f44c4
MD5 52b55b36566d395d2eddcf5f2ba4a8b1
BLAKE2b-256 1a2ce5d325fb805d72faed16063024c36070bd03af3ba57974885a0623ebeefb

See more details on using hashes here.

File details

Details for the file diffx_python-0.7.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for diffx_python-0.7.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3f5af1a3bcd2d1eb4c109ff54ed7716634c19700d2b61dbebc3742b5b2885805
MD5 99e0c555d0a68a0c148916335f1a7cfc
BLAKE2b-256 9b5868283f899f87b4f32c5951636443991a108a570a326ca08685a0e64ca211

See more details on using hashes here.

File details

Details for the file diffx_python-0.7.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for diffx_python-0.7.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 02f2d52eb03615e469d6d6e37403781a9ba66cade5c451f8ad5349a4e89eb418
MD5 01cf1a2bcc8920bf695d0c3123b8d533
BLAKE2b-256 c42ec642c258b1435338a859e33058d6c0e11fb109a821ff820d1445e0833a61

See more details on using hashes here.

File details

Details for the file diffx_python-0.7.2-cp311-cp311-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for diffx_python-0.7.2-cp311-cp311-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 79e18e9f5a9038d10471acf7b90d2c69f893d4329f9df97f21456c12a027cbe6
MD5 5379feb1214dbd0c69459270ab1d1530
BLAKE2b-256 6be317c0faa35a00168b1e36e5f0c88d765276b2fb7bdd9c9a16917b96bb6aa8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page