Skip to main content

A simple one way diff tool

Project description

ShortDiff

This is a Python implementation of a diff algorithm. It aims at producing one-way as-short-as-possible patch to go from a file to another. This is useful to keep track of modifications in a text file without keeping a copy of each state (this is the easy part of version control). This produce shorter patch than any output from the GNU diff tool. Since it is one-way only, the patch to go from A to B does not permit to to go from B to A.

Disclaimer

This algorithm have a time complexity of O(N*M) (where N and M are the number of line of each file) and is implemented in pure python. There performances are not great. You should probably not use it in any serious project. I wrote it for educational purpose and I use it in a really small scale project.

Still for educational project there are more naive version in the archive directory. The final module is a refinment of these.

Installation

Use pip to install it or copy this directory in a relevant place if you know what you are doing.

pip install ShortDiff

Usage

CLI

Create a patch

python -m ShortDiff diff FILA_A [FILE_B]

Create the patch to go from A to B. If FILE_B is ommited, its content is expected from the standard input. Output the patch to standard output.

Apply a patch

python -m ShortDiff patch PATCH [FILE]

Apply PATCH to FILE. If FILE is ommited, apply patch to standard input. Result is printed to standard output.

API

Functions can be imported from the module to be used in other apps. Useful functions include:

  • differ(old, new): (low level) take to arbitrary sequences and return the Levenstein distance and the path from one to another as a sequence of pairs (position in old, position in new). If the position in old is the same in two consecutive pair, it correspond to an insertion. If the position in new is the same in two consecutive pair, it correspond to a deletion. Else it correspond to an unmodified chunk. Path is made so to consecutive operations cannot be of the same kind (a deletion cannot be immediatly followed by another deletion).

  • get_chunks(old_txt, new_txt): (high level) generator taking two strings and compute the live diff. It yields 4 elements tuples (kind, start, stop, content). kind is one of 'd' (deletion) 'i' (insertion) and 'k' (keep/unmodified). start and stop are the line number boundaries of the chunk concerned by the operations in old_txt for deletion and keep and in new_txt for insertion. content is the list of lines of the chunk.

  • create_patch(old_txt, new_txt): (high level) return the patch as printed by the CLI

  • apply_patch(old_txt, patch): (high level) return the result of applying patch to old_txt

Project details


Download files

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

Source Distribution

ShortDiff-0.1.3.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

ShortDiff-0.1.3-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file ShortDiff-0.1.3.tar.gz.

File metadata

  • Download URL: ShortDiff-0.1.3.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.4

File hashes

Hashes for ShortDiff-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ae548193dafe0a4200bd9baf959c88f99f06b64ea68837c32bcab3feab409718
MD5 97017383009fb768ce0b30460d549b4e
BLAKE2b-256 ec5077f9c607201f6158829fda1cee65b47da255567281627bf10758bb56a572

See more details on using hashes here.

File details

Details for the file ShortDiff-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ShortDiff-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.28.1 CPython/3.7.4

File hashes

Hashes for ShortDiff-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4c9c12cf44a5ea4ac9535ece4fdc7d7c5938e7d58ea347b1a4b9cef251cd3c7b
MD5 cbd46fcd92f6416670033d0452695402
BLAKE2b-256 de3653fec31d5ecb9f73a9b8fcf962fb46040744c2198c2da28a0e1dac033052

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