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

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.2.tar.gz
Algorithm Hash digest
SHA256 c7f3d201d557e9dfa979621df9e50f2a695db4831000cd3de25f9d4ecfb95e54
MD5 bcedf21486ade35213a20f2aadf5e533
BLAKE2b-256 08ebb2a332950bcb048ff36da19618e812c3311e14d4c1d0727036b952302804

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_diff_strings-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 35a84af230a7a51b9d7fe0d1fd016beed16d90e39ba7e9e857a2f4d49ff5c75f
MD5 eb44c6544056c72959c7ad91774e8e0e
BLAKE2b-256 bd3c1fd70d23c8c2c346c165b22e620320843e67c1d224afe34472b4d2c5a8ab

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