Skip to main content

difftastic Python wrapper

Project description

ocdiff

Fast and simple side-by-side diff library for Python - wraps similar, inspired by icdiff.

Usage

pip install ocdiff
ocdiff.html_diff(
    a: str,
    b: str,
    context_lines: int | None = None,
    max_total_width: int | None = None,
) -> str
ocdiff.console_diff(
    a: str,
    b: str,
    context_lines: int | None = None,
    max_total_width: int | None = None,
) -> str

Example Output

Screenshot

CLI usage

ocdiff a.txt b.txt

Usage in pytest with rich

In your conftest.py, add:

import ocdiff
import ocdiff.helpers
import rich.console

def rich_repr(o: Any) -> str:
    string_io = io.StringIO()
    rich.console.Console(
        file=string_io,
        width=ocdiff.helpers.terminal_width() // 2 - 10,
        tab_size=4,
        no_color=True,
        highlight=False,
        log_time=False,
        log_path=False,
    ).print(o)
    string_io.seek(0)
    return string_io.getvalue()


def pytest_assertrepr_compare(config: Any, op: str, left: Any, right: Any) -> list[str] | None:
    very_verbose = config.option.verbose >= 2
    if not very_verbose:
        return None

    if op != "==":
        return None

    try:
        if abs(left + right) < 100:
            return None
    except TypeError:
        pass

    try:
        if isinstance(left, str) and isinstance(right, str):
            pretty_left = left
            pretty_right = right
        else:
            pretty_left = rich_repr(left)
            pretty_right = rich_repr(right)
        return ocdiff.console_diff(
            pretty_left,
            pretty_right,
            context_lines=10,
            max_total_width=ocdiff.helpers.terminal_width() - len("E     "),
        ).splitlines()
    except Exception:
        return None



Install/Develop

uv pip install -e '.[dev]'
maturin develop

Make release

  • Add pypi token and user = __token__ to settings (do this once).
  • Upversion pyproject.toml.
export VERSION=$(python -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])'); git tag -a v$VERSION head -m v$VERSION && git push origin v$VERSION

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

ocdiff-0.0.31-cp312-cp312-macosx_11_0_arm64.whl (238.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

ocdiff-0.0.31-cp312-cp312-macosx_10_13_x86_64.whl (246.9 kB view details)

Uploaded CPython 3.12 macOS 10.13+ x86-64

ocdiff-0.0.31-cp311-cp311-macosx_11_0_arm64.whl (238.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

ocdiff-0.0.31-cp311-cp311-macosx_10_9_x86_64.whl (246.9 kB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

File details

Details for the file ocdiff-0.0.31-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ocdiff-0.0.31-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 15200ed0fff6d51defdc61061fc344d0415b928c4474fd49a280729e31001575
MD5 6b2b29f899ac972d0b6fc86c788992f1
BLAKE2b-256 70ea23c2048c80470ba6bc9380dacc1b3ddbbb5e632f88dff76d9d3489a32cdd

See more details on using hashes here.

File details

Details for the file ocdiff-0.0.31-cp312-cp312-macosx_10_13_x86_64.whl.

File metadata

File hashes

Hashes for ocdiff-0.0.31-cp312-cp312-macosx_10_13_x86_64.whl
Algorithm Hash digest
SHA256 dc047832b74f5bdca192b5a59b23e672b645962680c1940ba19effd64b5f0df0
MD5 6ac832b2a1b4819f8fd7a1a45d888bda
BLAKE2b-256 2e4644c8c5bac35192d133c62444a2c831f6bb376c9e215ce7428ae260e22786

See more details on using hashes here.

File details

Details for the file ocdiff-0.0.31-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ocdiff-0.0.31-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0294c3e2b48d0bd421cbceae27aab028f348c76f3015afa1e987124b9873045a
MD5 2204e83d2f2e8e3ef5ebb9f0beadfeae
BLAKE2b-256 baa18736d7cae9610a6be8c7ed6ad80ed5036e3b9b03239d783814bbeae3d8bf

See more details on using hashes here.

File details

Details for the file ocdiff-0.0.31-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for ocdiff-0.0.31-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 117397e3a60130048ef06fd7f52f0b1a32493433c39c49e4dca52a79b2595d82
MD5 8cb49465afd4d7913ee58b4d394984a2
BLAKE2b-256 88cf7939ba2a25f2db89e2cfc203d9d2e10dfd4034a45b9aba5349b656737f19

See more details on using hashes here.

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