Skip to main content

List Differ

Version: 0.2.0

Description

Calculates longest common sequence on text, lists of numbers or characters, or lists of objects.

When comparing objects, make sure that the objects are hashable, i.e. override the __hash()__ method of the class. It is also a good idea to override the __eq()__ method if you have some custom logic for comparing items. This could be the case if your business logic considers close values as similar.

If you want to compare two strings ignoring casing, then simply call lower on each string before passing as argument.

Patch String Generation

The format_diff_text_as_patch, or the companion format_diff_as_patch, function can be used to generate a patch string from the diff result.

The patch string can be used to patch the first string to the second string. It can also be persisted and used later to patch the original string, thereby serving as a change log.

The patch string can be read back to a List[Delta[str]] using the parse_patch_text function in the patch_parser module.

Examples

Example 1 - Strings

Calculate a diff between two strings

Same strings

from listdiffer import differ

first = 'string'
second = 'string'
diff = differ.diff_text(first, second, False, False)

assert len(diff) == 0

Different strings

from listdiffer import differ

first = 'first string'
second = 'second string'
diff = differ.diff_text(first, second, False, False)

assert len(diff) == 1

Example 2 - List of integers

Calculate a diff between two strings

Same lists

from listdiffer import differ

first = [1, 2, 3]
second = [1, 2, 3]
d = differ.diff(first, second)

assert len(d) == 0

Different lists

from listdiffer import differ

first = [1, 2, 3]
second = [1, 2, 4]
d = differ.diff(first, second)

assert len(d) == 1

Example 3 - Lists of objects

Same lists

from listdiffer import differ

@dataclass
class TestItem:
    text: str
    value: int

    def __eq__(self, other):
        return self.text == other.text and self.value == other.value

    def __hash__(self):
        return hash((self.text, self.value))

source = [TestItem('test', 1), TestItem('test', 2), TestItem('test', 3)]
compare = [TestItem('test', 1), TestItem('test', 2), TestItem('test', 3)]
result = differ.diff(source, compare)

assert len(result) == 0

Different lists

from listdiffer import differ

@dataclass
class TestItem:
    text: str
    value: int

    def __eq__(self, other):
        return self.text == other.text and self.value == other.value

    def __hash__(self):
        return hash((self.text, self.value))

source = [TestItem('test', 1), TestItem('test', 2), TestItem('test', 3)]
compare = [TestItem('test', 1), TestItem('test', 2), TestItem('test', 3), TestItem('test', 4)]
result = differ.diff(source, compare)

assert len(result) == 1

Example 4 - Patch string generation

```python

text1 = """line1 line2 line3""" text2 = """line1 line2 lineX"""

    patch = format_diff_text_as_patch(text1, text2)

    assert """diff

@@ -2,1 +2,1 @@ line1 line2

  • lineX
  • line3 """ == patch

Metadata

Release files for listdiffer 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for listdiffer 0.3.0
File Size Uploaded Provenance
listdiffer-0.3.0.tar.gz 6.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for listdiffer 0.3.0
File Interpreter ABI Platform
listdiffer-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.6 kB

Release files / listdiffer-0.3.0.tar.gz

Download URL listdiffer-0.3.0.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9b0a54d7755261c7f0f31f85bc6d5b8a8d9c972cefc78463fcfc7fa03f334627
BLAKE2b-256 checksum
How to use checksums
629271a5a5e28e40151353bf1558e4aa7d8b274667e53346cef81eaf6a3598ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.8

Release files / listdiffer-0.3.0-py3-none-any.whl

Download URL listdiffer-0.3.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b9cb20d61f9d391fd4cc0f71d8543de03e79f319cf5ffc2daf15f5be5ed52ab4
BLAKE2b-256 checksum
How to use checksums
9d767d78050afa101891e99b6e05210e20c65b5bb1f2868de4dbde6d42047f84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.8

Security for listdiffer 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page