Skip to content

See a colored highlight of the difference between two Python objects

License

Notifications You must be signed in to change notification settings

Viktor-Bubanja/Pretty-Good-Diff

Repository files navigation

Pretty Good Diff

This tool allows developers to compare two objects and see a highlight of the differences between them. It works with strings and dictionaries. This project was made to help me fix unit tests with failing assertions quicker, especially when the objects are huge or very nested and complicated. I simply call show_diff on the two objects before an assert statement (note: if you're running this within Pytest, you need to pass a -s flag to your Pytest command to disable output capturing and see the output of print statements).

How to use

To install, simply run pip install pretty_good_diff in your terminal.

Once installed, you can import the show_diff method like from pretty_good_diff import show_diff.

Then, call show_diff(first_object, second_object) to see a colored diff of the input objects. first_object and second_object need to have the same type and be either str or dict objects.

Before: alt text

After: alt text

Solution

To calculate the difference between two dictionaries, a straightforward algorithm is used which recursively compares nested dictionaries. For key-value pairs where the values are strings, the string-difference algorithm is utilised. The string-difference algorithm implemented here leverages dynamic programming to identify the largest set of common substrings between two strings, prioritising longer sequences over multiple shorter ones. With a time complexity of O(N x M), where N and M are the lengths of the two input strings, the algorithm remains performant even for larger inputs.

About

See a colored highlight of the difference between two Python objects

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages