Skip to content

Conversation

@timjklein36
Copy link
Contributor

This change allows for the retention of insertion order when multiple dictionary items are added via a Delta.

Note: this change only accounts for retaining the order for dictionary_item_added since the keys, when sorted via Delta::_do_item_added, may end up being arbitrarily re-ordered prior to applying them to the resulting data set. I use the word "arbitrarily" here as the order of a given added key does not affect the addition of any other key to the dictionary.

Example:

from deepdiff import Delta, DeepDiff


t1 = {
    'pears': 2
}

t2 = {
    'pears': 2,
    'cucumbers': 2,
    'bananas': 3,
    'pumpkins': 4,
    'apples': 5
}

diff = DeepDiff(t1, t2)

delta = Delta(diff)

result = {'oranges': 1} + delta

In certain situations, it may be desired to retain the original order of the items added to the t2 data set when applying the Delta. The expected result would be the following:

{
    'oranges': 1,
    'cucumbers': 2,
    'bananas': 3,
    'pumpkins': 4,
    'apples': 5
}

Note: Please let me know if the unit test does not conform to the existing ones and I can update, if necessary.

@timjklein36
Copy link
Contributor Author

timjklein36 commented Feb 12, 2021

Forgot to remove some debugging print statements. Will update shortly.

Updated.

@timjklein36 timjklein36 force-pushed the tk/retain-order-dict-items-added branch from d4cb24a to 374f59d Compare February 12, 2021 20:12
- This alters the data structure used to determine dictionary
  items added (via storing the keys from `t1` and `t2` in
  OrderedSet objects)
- This also adds a unit test to verify that after applying a
  Delta with multiple dict items added, the added keys are not
  sorted, but are instead added to the resulting dictionary
  in the same insertion order as their source object (`t2`)
@timjklein36 timjklein36 force-pushed the tk/retain-order-dict-items-added branch from 374f59d to edff514 Compare February 12, 2021 20:14
@seperman
Copy link
Owner

Interesting. Thanks for the PR @timjklein36

@seperman seperman merged commit 3d389e6 into seperman:dev Feb 16, 2021
@timjklein36 timjklein36 deleted the tk/retain-order-dict-items-added branch February 16, 2021 02:58
@seperman
Copy link
Owner

@timjklein36
Copy link
Contributor Author

Thanks a lot, @seperman! Was not expecting this so soon. This is great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants