Skip to main content

Show character-level differences between two strings with colored terminal output

Project description

philiprehberger-diff-strings

Tests PyPI version Last updated

Show character-level differences between two strings with colored terminal output.

Installation

pip install philiprehberger-diff-strings

Usage

from philiprehberger_diff_strings import diff

print(diff("hello world", "hello there"))

Character-level diff

from philiprehberger_diff_strings import diff

# Colored output (default)
print(diff("the cat sat", "the bat sat"))

# Plain text output
print(diff("the cat sat", "the bat sat", color=False))

Programmatic diff

from philiprehberger_diff_strings import char_diff

changes = char_diff("abc", "adc")
# [Match(text='a'), Replace(old='b', new='d'), Match(text='c')]

Word-level diff

from philiprehberger_diff_strings import word_diff

print(word_diff("the quick brown fox", "the slow brown dog", color=False))

Line-level diff

from philiprehberger_diff_strings import line_diff

a = "line one\nline two\nline three"
b = "line one\nline 2\nline three"
print(line_diff(a, b, color=False))
# Shows unified-diff style output with +/- markers

# Control context lines shown around changes
print(line_diff(a, b, context=1))

Diff summary

from philiprehberger_diff_strings import diff_summary

summary = diff_summary("hello world", "hello there")
print(summary.additions)   # number of insertions
print(summary.deletions)   # number of deletions
print(summary.changes)     # number of replacements
print(summary.similarity)  # similarity ratio (0.0 to 1.0)

HTML diff

from philiprehberger_diff_strings import html_diff

result = html_diff("the cat", "the bat")
# 'the <del>c</del><ins>b</ins>at'

Custom colors

from philiprehberger_diff_strings import diff, word_diff

# Use custom ANSI colors (red, green, yellow, blue, magenta, cyan)
print(diff("abc", "adc", insert_color="cyan", delete_color="magenta", replace_color="blue"))
print(word_diff("old text", "new text", insert_color="blue", replace_color="cyan"))

Patch (reconstruct from changes)

from philiprehberger_diff_strings import char_diff, patch

changes = char_diff("hello world", "hello there")
result = patch("hello world", changes)
assert result == "hello there"

Similarity ratio

from philiprehberger_diff_strings import similarity

score = similarity("hello", "hallo")  # 0.8

Test assertions

from philiprehberger_diff_strings import assert_strings_equal

assert_strings_equal("expected output", "expected output")  # passes
assert_strings_equal("expected", "actual")  # raises AssertionError with diff

API

Function Description
diff(a, b, *, color=True, insert_color="green", delete_color="red", replace_color="yellow") Character-level diff as a string
char_diff(a, b) Programmatic diff returning a list of Change objects
word_diff(a, b, *, color=True, insert_color="green", delete_color="red", replace_color="yellow") Word-level diff as a string
line_diff(a, b, *, color=True, context=3) Unified-style line-level diff
diff_summary(a, b) Summary with addition/deletion/change counts and similarity
html_diff(a, b) Character-level diff as HTML with <ins>/<del> tags
patch(original, changes) Reconstruct the modified string from a change list
similarity(a, b) Similarity ratio between 0.0 and 1.0
assert_strings_equal(expected, actual, *, msg="") Assert equal or raise with diff

Change types

Type Fields Description
Match(text) text: str Unchanged text
Insert(text) text: str Text added in the second string
Delete(text) text: str Text removed from the first string
Replace(old, new) old: str, new: str Text that changed between strings

DiffSummary

Field Type Description
additions int Number of insertion operations
deletions int Number of deletion operations
changes int Number of replacement operations
similarity float Similarity ratio between 0.0 and 1.0

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

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 Distribution

philiprehberger_diff_strings-0.2.3.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_diff_strings-0.2.3-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_diff_strings-0.2.3.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.2.3.tar.gz
Algorithm Hash digest
SHA256 8eff586969c6434023ce4dab9adc23a8ae3719c0bd6f8835e871c40edeca8e18
MD5 78b19dbf1817dd43b6cb78319d95a22f
BLAKE2b-256 7d1b775076df3c778a4c4306032f5383f9e26271661fdf2cea53c81f9f768931

See more details on using hashes here.

File details

Details for the file philiprehberger_diff_strings-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4209d02b6150b6702c1b974ca37733a67083a67467490936b6542732badf2886
MD5 97c61c507ad5b6445f2bc5eca381ce07
BLAKE2b-256 c1c115988efede090f62e9d7a78b2cc9bc28ab71b19ca36ffb11d32e51690726

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