Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify: unified_patches duplicates fix #110

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dictdiffer/unify.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ def unify(self, first_patches, second_patches, conflicts):
if conflict.take_patch() != patch:
continue

self.unified_patches.append(patch)
if patch not in self.unified_patches:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be quite slow if you have a long list of unified_patches.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any suggestions perhaps?

Copy link
Member

@jirikuncar jirikuncar Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a separate set for the lookup.

Copy link
Member

@jirikuncar jirikuncar Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even propose a change to this method:

unified_patches = set()
self._build_index(conflicts)

sorted_patches = sorted(first_patches + second_patches, key=get_path)

for patch in sorted_patches:
    conflict = self._index.get(nested_hash(patch))

    # Apply only the patches that were taken as part of conflict
    # resolution.
    if conflict:
        if conflict.take_patch() != patch:
            continue

    if patch not in unified_patches:
        unified_patches.add(patch)
        yield patch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also _build_index method could become static and simply return the conflict mapping.

Copy link
Author

@iulianav iulianav Jul 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jirikuncar I think maybe before this commit the bug was not here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iulianav maybe instead of sorting the lists should be merged.

>>> a = [1, 4, 3, 2]
>>> b = [1, 5, 3]
>>> merge_keys(a, b)
[1, 4, 5, 3, 2]

self.unified_patches.append(patch)

return self.unified_patches

Expand Down
12 changes: 12 additions & 0 deletions tests/test_unify.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,15 @@ def test_unify(self):
u.unify([p1], [p2], [c])

self.assertEqual(u.unified_patches, [p1])

def test_unify_duplicate_patches(self):
u = Unifier()

p1 = ('remove', '', [('a', 'b')])
p2 = ('remove', '', [('a', 'b')])
c = Conflict(p1, p2)
c.take = 'f'

u.unify([p1], [p2], [c])

self.assertEqual(u.unified_patches, [p1])