Skip to main content

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

Project description

philiprehberger-diff-strings

Tests PyPI version License

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

Development

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

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.1.4.tar.gz (4.3 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.1.4-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9c8b4d6f0327517af3639f3395ea44b1722eb26602f06e96ca75839ad748e289
MD5 498ad3dae4fcbcbfcde19daa761ce6e1
BLAKE2b-256 489544bff06aba558f17f612cfab7b4426c7b4b9af1795332757958c8463fb74

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cdc884547e11117aec8b0fb9a3ff36510ea9ad8711b57a592f828e411ff1b1cf
MD5 33a76722517393f072905bd46848c4de
BLAKE2b-256 67dfd648fe092ebc279fcccb1a28ef2a4ad7fb8f7330f1cdb017b27aa7469cd1

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