dictdiffer icon indicating copy to clipboard operation
dictdiffer copied to clipboard

Document how to ignore field in array of dicts

Open AlJohri opened this issue 6 years ago • 3 comments

lets say i have an array like:

"races": [
    {
        "reportingUnits": [
           {
               "apiAccessTime": sometimestamp
           }
        ]
    }
]

I want to ignore races[?].reportingUnits[?].apiAccessTime regardless of the array index for both the races array and the reportingUnits subarray. Is this possible?

AlJohri avatar Jun 04 '18 12:06 AlJohri

I don't think it's currently possible out of box, but you should be able to implement custom set-like object.

class IgnorePath(object):
     def __init__(self, *args):
         self.path = args
     def __contains__(self, key):
         return len(key) > len(self.path) and all(
             source is None or source == target
             for source, target in zip(self.path, key)
         )

diff(a, b, ignore=IgnorePath('races', None, 'reportingUnits', None, 'apiAccessTime'))

jirikuncar avatar Jun 04 '18 14:06 jirikuncar

just an idea: It would be realy handy to have something like ignore_path or ignore_paths which should be able to take a path or list of paths as returned by 'change' as input

example

> actual = {'a': 1, 'b': [{'_type': 'foo'}, 1, 2, 3]}
> expected = {'a': 3, 'b': [{'_type':'bar'}, 1, 2, 'X']}
> list(diff(actual, expected))

[('change', 'a', (1, 3)), ('change', ['b', 0, '_type'], ('foo', 'bar')), 
('change', ['b', 3], (3, 'X'))]

Now let's assume we want ignore _type property, we woul write something like

list(diff(actual, expected, ignore_path=['b', 0, '_type']))

Deepdiff has something similar: exclude_paths and exclude_regex_paths

testautomation avatar Apr 13 '20 12:04 testautomation

I feel like I have a similar, but a bit different question:

dictdiffer.diff(
   [{
    'Name': 'Name1'
   }],
   [{
    'Name': 'Name1'
    'Field2': 'how to ignore it?'
   }]
)

How I can ignore 'Field2'?

It will be great if dictdiffer support ignoringValue concept ('dictdiffre.ignore-this-field' i teh following example), then I can do like this:

'Field2': 'dictdiffre.ignore-this-field'

But this will not solve ny issue in generic, Im trying to specify a path within an anonymous array (probably random ordered).

I will appreciate any ideas and sorry in advance - python is not my strongest skill

srgg avatar Dec 07 '21 06:12 srgg