Skip to main content

Show difference of two strings in color

Project description

diffstrings

Test Publish to PyPI Publish to TestPyPI PyPi Version Supported Python versions License Code style: black Imports: isort

Show difference of strings in color. It works on both terminal and ipython.

It extends difflib.SequenceMatcher class and provides a direct function too.

Installation

pip install diffstrings

API

Example Result

Colored diff strings

Color explanation:

  • Green: to add
  • Yellow: to replace. (Remove yellow colored then add with green colored strings)
  • Red: to be removed

Example texts

# Truth text to compare with
truth = """Lorem ipsum dolor sit amet,consectetur adipiscing elit,  sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.f"""

# Supposedly wrong text to compare
predict = """Lorem furio dlfgor sit amet, consectetur apiscbgfing elit, sed do eiusmod tempor inciddfbunt ut labore et ddslore magna aliqua. Ut enim ad minim veniam, quis nostrud exercsfsation ullamco laboris assi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in volbcvbte velit esse cilldm doldsre eu fugiat nulla pafdgatur. Excepteur sint occaecat cupidatat non pident, sunt in culpa qui asafficia desert mollit anim id est lfdgarum."""

Using class

from diffstrings import SequenceMatcher

s = SequenceMatcher(None, predict, truth, autojunk=False)
print(s.diff_strings())
print(s.diff_strings(True))

Using function

from diffstrings import diff_strings

print(diff_strings(predict, truth))
print(diff_strings(predict, truth, show_change_on_seq2=True))

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

diffstrings-0.1.3.tar.gz (7.2 kB view hashes)

Uploaded Source

Built Distribution

diffstrings-0.1.3-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page