Skip to content

How to align objects in lists properly? #185

Closed
@MKaras93

Description

@MKaras93

Hi. Sorry, I don't know whether it's a bug/feature or just a question. I've asked it some time ago on StackOverflow, but didn't receive any answer, so I decided to try here.

I'm trying to compare two lists of objects (dicts in this case) with deepdiff:

old = [
       {'name': 'war', 'status': 'active'},
       {'name': 'drought', 'status': 'pending'}
]

new = [
       {'name': 'war', 'status': 'pending'},
       {'name': 'fire', 'status': 'pending'}]

DeepDiff(old, new)

# Result:
{'values_changed': 
  {"root[0]['status']": {'new_value': 'pending', 'old_value': 'active'},
   "root[1]['name']": {'new_value': 'fire', 'old_value': 'drought'}}}

The problem is that I need a different way of aligning objects. In my project a particular state (for example war) have a strict life cycle: appears as pending, transforms to active and disappears. I want to use deepdiff to track these changes. Objects with different names are different objects and I don't want them to align with each other.

So the result I expect is:

'values_changed':
    {"root[0]['status']": {'new_value': 'pending', 'old_value': 'active'},
 'iterable_item_removed':
    {'root[1]': {'name': 'drought', 'status': 'pending'}}}
 'iterable_item_added':
    {'root[1]': {'name': 'fire', 'status': 'pending'}}}

Is there a way I can modify my object to align it properly? I've tried replacing the dict with a class with custom __eq__ method, but it didn't work. Do you have any other suggestion how I can make objects with the same name align only with each other?

Here is the original stack overflow question.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions