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

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.3.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.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.3.tar.gz
Algorithm Hash digest
SHA256 9c3c23c62e6854f81f6e26f68021ddb93d3dcf2225dfccf34797d3127f1d775c
MD5 bec20df0a83086c7c381a78e974d9030
BLAKE2b-256 8a10722201a76c6388ca8b2e69d8e3c57c735af088ec3cac94f308650500a68a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ae91a588283988e773897625b5d324d176a7f2a4d975284f723d888e9405c38b
MD5 b26f3572dc91f40dd3a95bd8d6bdbc5a
BLAKE2b-256 5560fda20e7cf61c9b8e99ec10e76228db8d511134c6f962bfb202571cf2aa8d

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