Giter Site home page Giter Site logo

Comments (6)

seperman avatar seperman commented on August 15, 2024

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.

maxrothman avatar maxrothman commented on August 15, 2024

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.

maxrothman avatar maxrothman commented on August 15, 2024

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.

seperman avatar seperman commented on August 15, 2024

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.

maxrothman avatar maxrothman commented on August 15, 2024

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.

seperman avatar seperman commented on August 15, 2024

I'm going to close this ticket since we have had the Exclude Obj Callback for a while.

from deepdiff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.