moreorless: Python Diff Wrapper
Moreorless is a thin Python wrapper around `difflib.unified_diff` designed to simplify its usage and address common edge cases, such as the "No newline at eof" issue. It provides a more user-friendly interface for generating unified diffs, applying patches, and producing colorized diff output. The current version is 0.5.0, with an irregular release cadence based on contributions and feature enhancements.
Warnings
- gotcha While `moreorless` simplifies `difflib.unified_diff`, it is still a wrapper. For highly complex or performance-critical diffing scenarios, understanding the underlying `difflib` module might be beneficial.
- gotcha The `echo_color_unified_diff` function (and other color-related utilities) internally uses the `click` library for terminal output. While `click` is a required dependency, users unfamiliar with `click` might find the integration surprising or encounter issues if their terminal environment doesn't support ANSI escape codes.
- deprecated The `moreorless` library is relatively small with limited community contributions (few stars, forks). While actively maintained by the author, major feature additions or rapid bug fixes might not occur as quickly as with larger, more widely adopted libraries.
- breaking The `combined_diff` functionality was introduced in version `0.5.0`. Attempts to use `moreorless.combined.combined_diff` in versions prior to `0.5.0` will result in an `ImportError` or `AttributeError`.
Install
-
pip install moreorless
Imports
- unified_diff
from moreorless import unified_diff
- apply_single_file
from moreorless.patch import apply_single_file
- echo_color_unified_diff
from moreorless.click import echo_color_unified_diff
- combined_diff
from moreorless.combined import combined_diff
Quickstart
from moreorless import unified_diff
from moreorless.click import echo_color_unified_diff
old_text = """Line 1\nLine 2\nLine 3 - original\nLine 4\n"""
new_text = """Line 1\nLine 2 - modified\nLine 3 - changed\nLine 5 - new\n"""
# Get a raw unified diff string
diff_string = unified_diff(old_text, new_text, 'old_file.txt', 'new_file.txt')
print("\n--- Raw Unified Diff ---")
print(diff_string)
# Or print a colorized diff directly to stdout (requires 'click' package)
print("\n--- Colorized Diff (using click) ---")
echo_color_unified_diff(old_text, new_text, 'old_file.txt', 'new_file.txt')