# ocdiff
Fast and simple side-by-side diff library for Python - wraps [similar](https://crates.io/crates/similar), inspired by [icdiff](https://github.com/jeffkaufman/icdiff).
# Usage
```shell
pip install ocdiff
```
```python
ocdiff.html_diff(
a: str,
b: str,
context_lines: int | None = None,
max_total_width: int | None = None,
) -> str
```
```python
ocdiff.console_diff(
a: str,
b: str,
context_lines: int | None = None,
max_total_width: int | None = None,
) -> str
```
# Example Output
![Screenshot](screenshot-1.png)
# CLI usage
```shell
ocdiff a.txt b.txt
```
# Usage in `pytest` with [rich](https://github.com/Textualize/rich)
In your `conftest.py`, add:
```python
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
```
<br>
<hr>
<br>
# Install/Develop
```shell
uv pip install -e '.[dev]'
maturin develop
```
# Make release
- Add pypi token and user = `__token__` to settings (do this once).
- Upversion `pyproject.toml`.
```shell
git tag -a v0.0.x head -m v0.0.x
git push origin v0.0.x
```
Raw data
{
"_id": null,
"home_page": null,
"name": "ocdiff",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11",
"maintainer_email": "Oliver Russell <ojhrussell@gmail.com>",
"keywords": "diff",
"author": "Oliver Russell <ojhrussell@gmail.com>",
"author_email": "Oliver Russell <ojhrussell@gmail.com>",
"download_url": null,
"platform": null,
"description": "# ocdiff\n\nFast and simple side-by-side diff library for Python - wraps [similar](https://crates.io/crates/similar), inspired by [icdiff](https://github.com/jeffkaufman/icdiff).\n\n# Usage\n\n```shell\npip install ocdiff\n```\n\n```python\nocdiff.html_diff(\n a: str,\n b: str,\n context_lines: int | None = None,\n max_total_width: int | None = None,\n) -> str\n```\n\n```python\nocdiff.console_diff(\n a: str,\n b: str,\n context_lines: int | None = None,\n max_total_width: int | None = None,\n) -> str\n```\n\n# Example Output\n\n![Screenshot](screenshot-1.png)\n\n# CLI usage\n\n```shell\nocdiff a.txt b.txt\n```\n\n# Usage in `pytest` with [rich](https://github.com/Textualize/rich)\n\nIn your `conftest.py`, add:\n\n```python\nimport ocdiff\nimport ocdiff.helpers\nimport rich.console\n\ndef rich_repr(o: Any) -> str:\n string_io = io.StringIO()\n rich.console.Console(\n file=string_io,\n width=ocdiff.helpers.terminal_width() // 2 - 10,\n tab_size=4,\n no_color=True,\n highlight=False,\n log_time=False,\n log_path=False,\n ).print(o)\n string_io.seek(0)\n return string_io.getvalue()\n\n\ndef pytest_assertrepr_compare(config: Any, op: str, left: Any, right: Any) -> list[str] | None:\n very_verbose = config.option.verbose >= 2\n if not very_verbose:\n return None\n\n if op != \"==\":\n return None\n\n try:\n if abs(left + right) < 100:\n return None\n except TypeError:\n pass\n\n try:\n if isinstance(left, str) and isinstance(right, str):\n pretty_left = left\n pretty_right = right\n else:\n pretty_left = rich_repr(left)\n pretty_right = rich_repr(right)\n return ocdiff.console_diff(\n pretty_left,\n pretty_right,\n context_lines=10,\n max_total_width=ocdiff.helpers.terminal_width() - len(\"E \"),\n ).splitlines()\n except Exception:\n return None\n```\n\n<br>\n<hr>\n<br>\n\n# Install/Develop\n\n```shell\nuv pip install -e '.[dev]'\nmaturin develop\n```\n\n# Make release\n\n- Add pypi token and user = `__token__` to settings (do this once).\n- Upversion `pyproject.toml`.\n\n```shell\ngit tag -a v0.0.x head -m v0.0.x\ngit push origin v0.0.x\n```\n\n",
"bugtrack_url": null,
"license": null,
"summary": "difftastic Python wrapper",
"version": "0.0.26",
"project_urls": {
"documentation": "https://github.com/leontrolski/ocdiff",
"homepage": "https://github.com/leontrolski/ocdiff",
"repository": "https://github.com/leontrolski/ocdiff.git"
},
"split_keywords": [
"diff"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7df057fb68f77d94f8adf4b406e551cba04d336b1f4f78a027b58b42b88708ac",
"md5": "6931c8b6f4160d3a2338c9e94310b3c8",
"sha256": "7ee57fb28f8be765090cb65ff6cdb83cd1df71c850bea11955597b68fa151587"
},
"downloads": -1,
"filename": "ocdiff-0.0.26-cp311-cp311-macosx_10_12_x86_64.whl",
"has_sig": false,
"md5_digest": "6931c8b6f4160d3a2338c9e94310b3c8",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 246809,
"upload_time": "2024-04-15T09:16:22",
"upload_time_iso_8601": "2024-04-15T09:16:22.086837Z",
"url": "https://files.pythonhosted.org/packages/7d/f0/57fb68f77d94f8adf4b406e551cba04d336b1f4f78a027b58b42b88708ac/ocdiff-0.0.26-cp311-cp311-macosx_10_12_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "576e9c5188e031ff91f4eb5be7fb922ef19fa5512789a7d8ea01f040a495ae56",
"md5": "a23b6bc1f29ccc27813eea04be7a7c5e",
"sha256": "523565202e5881737906a45f68c365f7d9f9b5d4bf2d2f7dc6cd5d6a7da8b9a7"
},
"downloads": -1,
"filename": "ocdiff-0.0.26-cp311-cp311-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "a23b6bc1f29ccc27813eea04be7a7c5e",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 238241,
"upload_time": "2024-04-15T09:15:54",
"upload_time_iso_8601": "2024-04-15T09:15:54.928013Z",
"url": "https://files.pythonhosted.org/packages/57/6e/9c5188e031ff91f4eb5be7fb922ef19fa5512789a7d8ea01f040a495ae56/ocdiff-0.0.26-cp311-cp311-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "255af3780f3d402e6c950f2d1b8ae0092fd88e67f741562f590067e4765e0ff1",
"md5": "ec705e219789389f4cba510a15f380bf",
"sha256": "a1292b706a8c04e8b273ebd02f3d2e3e44e48aae4327fcaf7479767728da4443"
},
"downloads": -1,
"filename": "ocdiff-0.0.26-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "ec705e219789389f4cba510a15f380bf",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 308680,
"upload_time": "2024-04-15T09:17:41",
"upload_time_iso_8601": "2024-04-15T09:17:41.752297Z",
"url": "https://files.pythonhosted.org/packages/25/5a/f3780f3d402e6c950f2d1b8ae0092fd88e67f741562f590067e4765e0ff1/ocdiff-0.0.26-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bd2f3009025d98c4e69ff77dc923b06e22916b21d418100b1aa76bc5abc5aeb7",
"md5": "39cb320d6dfbba0ac8ce10dedd9ee5bd",
"sha256": "7d5652c7f824563b3d79f260debaffc3dce6e62276119ce95aa43a447b681a6f"
},
"downloads": -1,
"filename": "ocdiff-0.0.26-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl",
"has_sig": false,
"md5_digest": "39cb320d6dfbba0ac8ce10dedd9ee5bd",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 333696,
"upload_time": "2024-04-15T09:17:42",
"upload_time_iso_8601": "2024-04-15T09:17:42.781781Z",
"url": "https://files.pythonhosted.org/packages/bd/2f/3009025d98c4e69ff77dc923b06e22916b21d418100b1aa76bc5abc5aeb7/ocdiff-0.0.26-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-15 09:16:22",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "leontrolski",
"github_project": "ocdiff",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ocdiff"
}