Skip to main content

Generate diffs on lists of objects

Project description

List Differ

Version: 0.2.0

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.

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

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

listdiffer-0.2.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

listdiffer-0.2.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file listdiffer-0.2.0.tar.gz.

File metadata

  • Download URL: listdiffer-0.2.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for listdiffer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ae5717b16eb340c2313d032d3f4f75a8afa6bb393995307df8d215e4f0bf1c9a
MD5 2f0d3e464436a9ab469cd87bddd914a8
BLAKE2b-256 7968f1387d7970e67f9f50379759d973fe0db740851d5687aed37de8b42c85fb

See more details on using hashes here.

File details

Details for the file listdiffer-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: listdiffer-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for listdiffer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fac2934ddd52294f85f80471c4854cbcb3e04494c8bb4262f9625e0901d7ac3c
MD5 8041b59bd347e2ee4c5663702832ecf7
BLAKE2b-256 47e59aa4b00c7a86bf3194a211c87841c06f92fc07c4ba63079b2bdbcff63e35

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