Comments (6)
I think we should start deprecating exclude_type
and instead make it so it takes exclude
.
Then it checks if the item in the exclude list is a class or not. If it is a class it should basically do exclude_type
, otherwise it should use is
as you mentioned to exclude exact objects that need to be excluded. Please make a PR if you have time. Also please make the appropriate changes to the contenthash.py
so the exclude works there too since ignore_order
uses the content hashes.
from deepdiff.
I'm not sure I agree with the approach of magically deciding whether to do isinstance
vs ==
vs is
comparison. Classes are also objects, what you want to exclude a class object from a diff? Or what about objects with well-defined ==
operations, like datetimes and uuids? It almost seems like we'd need a more complex API, something like this:
DD = DeepDiff()
DD.exclude(object=None, id_comparison=True)
DD.exclude(object=date(2017, 6, 13))
DD.exclude(type=UUID)
DD.diff(first, second)
I'm not terribly fond of this specific API, but my point is more that it seems like we need kwargs to pull it off.
from deepdiff.
Thinking about this a little more, being able to match based on ID or ==
would also be useful in DeepSearch
for matching the thing you're searching for. Additionally, I can imagine users wanting to add more complex matching rules, so maybe this is a good opportunity for a hole-in-the-middle approach, something like this:
from deepdiff import DeepSearch, Match
from deepdiff.matchers import by_id
DeepSearch(thing1, thing2, matcher=by_id, exclude=[123, 'foo', (lambda x: return x%2 == 0)])
This way, users could specify their own matchers, and existing flags like case_sensitive
could become matcher functions (though it might be good to leave in the kwarg as a shortcut).
from deepdiff.
Hi @maxrothman
Sorry for such a long delay in responding to you. I finally found some time to release the v4.0.0 and take a look at the tickets.
Are you still interested in making the PR? That would be great! I like your ideas. Based on what you came up with, the API that I'm imagining could be:
from deepdiff import exclude_condition # for advanced exclusions
ex_cond1 = exclude_condition(type_=DateTime, condition=lambda x: x > 2018)
DeepDiff(t1, t2, exclude_objects=(123, "blah",...), exclude_conditions=[ex_cond1]
So the exclude_condition takes type_
and condition
.
And then you can pass those conditions into the exclude_conditions
parameter.
exclude_objects will be a list of objects that will be ignore based on ==
. I think using is
will be tricky. Where do you see is
working better than ==
?
from deepdiff.
Looking at this design, I think it could be simplified to the following:
DeepDiff(t1, t2, exclude: List[Callable[[itm], bool]])
# e.g.
DeepDiff(t1, t2, exclude=[lambda x: x==1, lambda x: isinstance(x, DateTime), lambda x: x is None])
This API supports the use cases of both exclude_objects
and exclude_conditions
, makes the ==
/is
choice a non-issue, and it supports any other use cases anyone could come up with.
Now that you're calling N functions for each object in the diff, performance could become a concern. In that case, you could build special-case optimizations for common patterns:
from deepdiff import exclude_equals, exclude_type
DeepDiff(t1, t2, exclude=[exclude_equals(123), exclude_type(DateTime)]
Unlike functions, the sentinel objects returned by these functions would only result in a single function call, and special logic could be added to the diffing algorithm that expects these sentinel objects and evaluates their conditions in fewer function calls.
I'm no longer actively using deepdiff, so it's unlikely I'll have the time to contribute this change. I'd be happy to continue helping with the design though.
from deepdiff.
I'm going to close this ticket since we have had the Exclude Obj Callback for a while.
from deepdiff.
Related Issues (20)
- Error after comparing: Can not produce a hash (type datetime.date doesn't define __round__ method) HOT 1
- parse_path() cannot parse a path HOT 4
- Is there a way to systematically cycle through all corresponding attributes that haven't changed? HOT 1
- support compre string as float specify precision HOT 1
- Error when comparing two nested dicts with 2 added fields HOT 5
- why exclude_regex_paths dont work with ignore_order HOT 1
- Change of integer attribute is not detected HOT 3
- DeepDiff include_path param doesn't work with list of dictionaries HOT 1
- Instantiating a Delta with a flat_dict_list unexpectedly mutates the flat_dict_list HOT 1
- DeepDiff result got errors but not raising an exception
- Documentation on how to create an object only containing changed elements. HOT 1
- DeepDiff cannot compare two zero-dimensional `ndarrays` HOT 2
- Deepdiff incompatible with numpy 2.0 HOT 3
- DeepDiff cannot distinguish between datetime timezones HOT 1
- DeepDiff significiant digit 0 is comparing digits after the decimal point
- deepdiff diff error when i haved set a custom iterable_compare_func HOT 1
- load 2 json files to dict, the compare result is not correct HOT 1
- exclude_regex_paths does not work correctly with regular expressions including $
- TypeError when diffing dict that have bytes keys HOT 1
- deepdiff 7.0.1 test failure in Fedora rawhide with Python 3.13 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepdiff.