Show character-level differences between two strings with colored terminal output
Project description
philiprehberger-diff-strings
Show character-level differences between two strings with colored terminal output.
Install
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))
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) |
Character-level diff as a string |
char_diff(a, b) |
Programmatic diff returning a list of Change objects |
word_diff(a, b, *, color=True) |
Word-level diff as a string |
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 |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_diff_strings-0.1.2.tar.gz.
File metadata
- Download URL: philiprehberger_diff_strings-0.1.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c7f3d201d557e9dfa979621df9e50f2a695db4831000cd3de25f9d4ecfb95e54
|
|
| MD5 |
bcedf21486ade35213a20f2aadf5e533
|
|
| BLAKE2b-256 |
08ebb2a332950bcb048ff36da19618e812c3311e14d4c1d0727036b952302804
|
File details
Details for the file philiprehberger_diff_strings-0.1.2-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_diff_strings-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35a84af230a7a51b9d7fe0d1fd016beed16d90e39ba7e9e857a2f4d49ff5c75f
|
|
| MD5 |
eb44c6544056c72959c7ad91774e8e0e
|
|
| BLAKE2b-256 |
bd3c1fd70d23c8c2c346c165b22e620320843e67c1d224afe34472b4d2c5a8ab
|