
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
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.
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.
Calculate a diff between two strings
from listdiffer import differ
first = 'string'
second = 'string'
diff = differ.diff_text(first, second, False, False)
assert len(diff) == 0
from listdiffer import differ
first = 'first string'
second = 'second string'
diff = differ.diff_text(first, second, False, False)
assert len(diff) == 1
Calculate a diff between two strings
from listdiffer import differ
first = [1, 2, 3]
second = [1, 2, 3]
d = differ.diff(first, second)
assert len(d) == 0
from listdiffer import differ
first = [1, 2, 3]
second = [1, 2, 4]
d = differ.diff(first, second)
assert len(d) == 1
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
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
```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
FAQs
Generate diffs on lists of numbers, bytes or objects
We found that listdiffer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.