Skip to main content

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'

Markdown diff

For PR comments, issue bodies, or chat messages — render the diff using Markdown formatting (**bold** insertions, ~~strikethrough~~ deletions):

from philiprehberger_diff_strings import markdown_diff

print(markdown_diff("the cat sat", "the bat sat"))
# 'the ~~c~~**b**at sat'

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
markdown_diff(a, b) Character-level diff as Markdown (**bold** for inserts, ~~strikethrough~~ for deletes)
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

Release files for philiprehberger-diff-strings 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-diff-strings 0.3.0
File Size Uploaded
philiprehberger_diff_strings-0.3.0.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-diff-strings 0.3.0
File Interpreter ABI Platform
philiprehberger_diff_strings-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / philiprehberger_diff_strings-0.3.0.tar.gz

Download URL philiprehberger_diff_strings-0.3.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c5ebf7e72371364b04315bf0b896d5cc94b0b80fbeac9c1a1cf1d345251112e3
BLAKE2b-256 checksum
How to use checksums
dff3b10764ae943b30039ee54f0e09f1068c43ea29eaa1d0e4617edad1fa0772
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_diff_strings-0.3.0-py3-none-any.whl

Download URL philiprehberger_diff_strings-0.3.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
918d6a904f4f95efb39b7e8d15d3c1cc60463ef7407f653bfc707eb2406fe7ff
BLAKE2b-256 checksum
How to use checksums
61c2964bed16f20353257c7bbef2acdd48dee6c801137d52e47ad487bf4c8978
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page