Created
March 2, 2026 15:12
-
-
Save yunruse/e1cdd5df58ce7df1507e6772e0b09956 to your computer and use it in GitHub Desktop.
Selective TOML diffing test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "dc7af5c7", | |
| "metadata": {}, | |
| "source": [ | |
| "What follows is an attempt at patching a diff in Python such that certain changes are ignored.\n", | |
| "\n", | |
| "For this case, we want to preserve TOML whitespace or comments (`# `), but the principle is the same." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "fa5577e0", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from difflib import Differ\n", | |
| "\n", | |
| "def patch(src: str, dst: str):\n", | |
| " \"Apply dst's changes to src, but preserve blank lines and comments\"\n", | |
| " diff = Differ().compare(src.splitlines(), dst.splitlines())\n", | |
| " for diffline in diff:\n", | |
| " op = diffline[0]\n", | |
| " line = diffline[2:]\n", | |
| " if op == ' ':\n", | |
| " yield line\n", | |
| " if op == '+':\n", | |
| " yield line\n", | |
| " if op == '-':\n", | |
| " sline = line.strip()\n", | |
| " if sline == '' or sline.startswith('#'):\n", | |
| " yield line" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "47254126", | |
| "metadata": {}, | |
| "source": [ | |
| "# Examples\n", | |
| "Very simple changes work – this is where internally `Differ` notices line-by-line changes of only a few characters:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "b076508c", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "foo = \"bar\"\n", | |
| "# comment 1\n", | |
| "\n", | |
| "baz = 124\n", | |
| "# comment 2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from toml import loads, dumps\n", | |
| "\n", | |
| "src = '''\\\n", | |
| "foo = \"bar\"\n", | |
| "# comment 1\n", | |
| "\n", | |
| "baz = 123\n", | |
| "# comment 2\n", | |
| "\\\n", | |
| "'''\n", | |
| "obj = loads(src)\n", | |
| "obj['baz'] = 124\n", | |
| "dst = dumps(obj)\n", | |
| "print('\\n'.join(patch(src, dst)))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "a0eff043", | |
| "metadata": {}, | |
| "source": [ | |
| "However, anything beyond these character-level changes will result in line order not being preserved:" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "7d7fe450", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "foo = \"bar\"\n", | |
| "baz = \"testing!\"\n", | |
| "# comment 1\n", | |
| "\n", | |
| "# comment 2\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "obj = loads(src)\n", | |
| "obj['baz'] = \"testing!\"\n", | |
| "dst = dumps(obj)\n", | |
| "print('\\n'.join(patch(src, dst)))" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.14.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment