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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.5.tar.gz
Algorithm Hash digest
SHA256 449faee9635a433d7c911f6da43b4974a31d24d5e71fc61c7932669dcfb36a6f
MD5 bf3509200b03f8ce5ac6841874d02455
BLAKE2b-256 fa627446fc06b3a02e1a09ce1931826ef6ea14348cf6bc6f2e62c74566e542ba

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 28e14c138a3d1ff51f78b79daac5c01b5710e796a243c42ca27c67967fa19709
MD5 cdc86c0e69be618c654343698b2272bb
BLAKE2b-256 184d7ac7a99eb7e830324f18f5cb97d71202d592663df456b0a649095c696575

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