Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

listdiffer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

listdiffer

Generate diffs on lists of objects

  • 0.2.0
  • PyPI
  • Socket score

Maintainers
1

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

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc