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

Document how to ignore field in array of dicts #108

Open
AlJohri opened this issue Jun 4, 2018 · 3 comments
Open

Document how to ignore field in array of dicts #108

AlJohri opened this issue Jun 4, 2018 · 3 comments

Comments

@AlJohri
Copy link

AlJohri commented Jun 4, 2018

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?

@jirikuncar
Copy link
Member

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 jirikuncar changed the title ignore field in array of dicts? Document how to ignore field in array of dicts Jun 4, 2019
@testautomation
Copy link

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

@srgg
Copy link

srgg commented Dec 7, 2021

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

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

No branches or pull requests

3 participants