# 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
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
```
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\nexport 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\n```\n\n",
"bugtrack_url": null,
"license": null,
"summary": "difftastic Python wrapper",
"version": "0.0.33",
"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": "d4f3a6c3bf14c2e7228d279c474cad5357fc8999e7746800833c53300a51c8db",
"md5": "5b46c2a63feda93704f5d11503f72725",
"sha256": "80449d6c26df6049a272bbd2f2f7f996c427b042c278649f750a875764b3fbce"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp311-cp311-macosx_10_9_x86_64.whl",
"has_sig": false,
"md5_digest": "5b46c2a63feda93704f5d11503f72725",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 277239,
"upload_time": "2024-10-08T11:26:51",
"upload_time_iso_8601": "2024-10-08T11:26:51.028833Z",
"url": "https://files.pythonhosted.org/packages/d4/f3/a6c3bf14c2e7228d279c474cad5357fc8999e7746800833c53300a51c8db/ocdiff-0.0.33-cp311-cp311-macosx_10_9_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3402e1421824f9468bc8aa169df6fd30b0b56063759bc53de3f213815f203e6f",
"md5": "ebe78f9be295dd74d5a1d402ae07d8db",
"sha256": "441a4e82c1085c9a82f0b76d11dd7d9f9dc5c823d1881fcf01011c98a562f120"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp311-cp311-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "ebe78f9be295dd74d5a1d402ae07d8db",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 266238,
"upload_time": "2024-10-08T11:26:51",
"upload_time_iso_8601": "2024-10-08T11:26:51.584395Z",
"url": "https://files.pythonhosted.org/packages/34/02/e1421824f9468bc8aa169df6fd30b0b56063759bc53de3f213815f203e6f/ocdiff-0.0.33-cp311-cp311-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "14fc3ee1ba953149a68a77df6603660dd9eef82946968d75eebbbf1b91254833",
"md5": "dd82574dd0e89928d0742dc2bb4c17db",
"sha256": "8d3e13f062aa7d07f7804fbe0bb3fb1147263ba6127e5c92986e46a6357a6079"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
"has_sig": false,
"md5_digest": "dd82574dd0e89928d0742dc2bb4c17db",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 307816,
"upload_time": "2024-10-08T11:32:21",
"upload_time_iso_8601": "2024-10-08T11:32:21.492153Z",
"url": "https://files.pythonhosted.org/packages/14/fc/3ee1ba953149a68a77df6603660dd9eef82946968d75eebbbf1b91254833/ocdiff-0.0.33-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5deb78b30ccad4a8141f085f5fe8a69344892f90fbc6374badb45006e49dacc8",
"md5": "42372562ba07c6a003fcb63302e9c92d",
"sha256": "dc36534101f0940438fcdc6065b77aae04642a98d46965455ca3ebdf67fef08c"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"has_sig": false,
"md5_digest": "42372562ba07c6a003fcb63302e9c92d",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 305861,
"upload_time": "2024-10-08T11:32:22",
"upload_time_iso_8601": "2024-10-08T11:32:22.655025Z",
"url": "https://files.pythonhosted.org/packages/5d/eb/78b30ccad4a8141f085f5fe8a69344892f90fbc6374badb45006e49dacc8/ocdiff-0.0.33-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e65fa900ba02f5958f990b722b2e4f40c30a522d538a8fe90cf2016a8025cd45",
"md5": "ba857d6459e4219861b39dd1b4874b3f",
"sha256": "52c9b428cf025e5409cd52732d229642989f51e074e4cc87d4a8f2c4b3cac774"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl",
"has_sig": false,
"md5_digest": "ba857d6459e4219861b39dd1b4874b3f",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": ">=3.11",
"size": 326857,
"upload_time": "2024-10-08T11:32:24",
"upload_time_iso_8601": "2024-10-08T11:32:24.540857Z",
"url": "https://files.pythonhosted.org/packages/e6/5f/a900ba02f5958f990b722b2e4f40c30a522d538a8fe90cf2016a8025cd45/ocdiff-0.0.33-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "bbab3433d2bcf3a8de8b44d3ea19b157deb1939fe69c2e14fcc58f082e68611d",
"md5": "7aaa8521d0453c318acb32e8465e85fd",
"sha256": "5f7c3f36c94a1894f9bccd74afbaa980e4cebd48cb30e70fe137675dcd8d8400"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp312-cp312-macosx_10_13_x86_64.whl",
"has_sig": false,
"md5_digest": "7aaa8521d0453c318acb32e8465e85fd",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.11",
"size": 276700,
"upload_time": "2024-10-08T11:26:54",
"upload_time_iso_8601": "2024-10-08T11:26:54.760993Z",
"url": "https://files.pythonhosted.org/packages/bb/ab/3433d2bcf3a8de8b44d3ea19b157deb1939fe69c2e14fcc58f082e68611d/ocdiff-0.0.33-cp312-cp312-macosx_10_13_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "dbf9c288de4e1da404966fd9a0187913ceecb6a4d9925841b7a1dfd944c13e88",
"md5": "2232493a80423f0502c6c2809002fd3b",
"sha256": "e3a18e63564442059fbf8c8ee6692b655cbc0a7ab9d757cf937daddb2769f483"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp312-cp312-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "2232493a80423f0502c6c2809002fd3b",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": ">=3.11",
"size": 266357,
"upload_time": "2024-10-08T11:27:42",
"upload_time_iso_8601": "2024-10-08T11:27:42.306031Z",
"url": "https://files.pythonhosted.org/packages/db/f9/c288de4e1da404966fd9a0187913ceecb6a4d9925841b7a1dfd944c13e88/ocdiff-0.0.33-cp312-cp312-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ac3803320fd4f8dd7b1a6d24c0570fa637f4844f59f821da5c683539c71f729e",
"md5": "ecacdcd250890b27349e86ef7f7dd28a",
"sha256": "4b9f5b040334780553636324fc565a801a2d59449f37533f7e57061d9c3b920d"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp313-cp313-macosx_10_13_x86_64.whl",
"has_sig": false,
"md5_digest": "ecacdcd250890b27349e86ef7f7dd28a",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": ">=3.11",
"size": 276717,
"upload_time": "2024-10-08T11:28:01",
"upload_time_iso_8601": "2024-10-08T11:28:01.348039Z",
"url": "https://files.pythonhosted.org/packages/ac/38/03320fd4f8dd7b1a6d24c0570fa637f4844f59f821da5c683539c71f729e/ocdiff-0.0.33-cp313-cp313-macosx_10_13_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "174e611cd49a3ee7630597e1fac226322a3d2079042316468e64056eb239884e",
"md5": "45e90531f95ee1bd0969949a11bb0e44",
"sha256": "3e57a74dcb2e75076c7fd3d3bf6b335a4103e61bd6d40c229f6a8c36bb4b3f1a"
},
"downloads": -1,
"filename": "ocdiff-0.0.33-cp313-cp313-macosx_11_0_arm64.whl",
"has_sig": false,
"md5_digest": "45e90531f95ee1bd0969949a11bb0e44",
"packagetype": "bdist_wheel",
"python_version": "cp313",
"requires_python": ">=3.11",
"size": 266048,
"upload_time": "2024-10-08T11:28:00",
"upload_time_iso_8601": "2024-10-08T11:28:00.655172Z",
"url": "https://files.pythonhosted.org/packages/17/4e/611cd49a3ee7630597e1fac226322a3d2079042316468e64056eb239884e/ocdiff-0.0.33-cp313-cp313-macosx_11_0_arm64.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-08 11:26:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "leontrolski",
"github_project": "ocdiff",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "ocdiff"
}