Giter Site home page Giter Site logo

Comments (3)

isohedronpipeline avatar isohedronpipeline commented on June 18, 2024

Another thing I tried was:

_cattr_converter.register_unstructure_hook(
    Toy,
    lambda o: {"_type": type(o).__name__, **_cattr_converter.unstructure(o)},
)

but that caused an infinite loop.

from cattrs.

Tinche avatar Tinche commented on June 18, 2024

Hi,

sorry for the delayed response, I was on vacation.

These strategies are somewhat stateful since a lot of them do some of the work at configuration time, rather than structure/unstructure time. This means the order in which they are applied matters.

I tried switching the order of the hooks here:

c = cattr.Converter()
include_subclasses(Toy, c, union_strategy=configure_tagged_union)
include_subclasses(ToyBox, c, union_strategy=configure_tagged_union)

Doing this, the output now is:

{'_type': 'SmallToyBox',
 'contents': [{'_type': 'Lego', 'name': 'space'},
              {'_type': 'Lego', 'name': 'house'},
              {'_type': 'Train', 'name': 'stream'},
              {'_type': 'Train', 'name': 'electric'}],
 'material': 'wood'}

That that look like what you were going for?

from cattrs.

isohedronpipeline avatar isohedronpipeline commented on June 18, 2024

Yes! Thank you!

I hope you had a good holiday :-)

Can you explain why/how it works by registering Toy prior to ToyBox? It seems a little magical and I'd love to know how that works in case I encounter a simlar issue in the future.

Also, how might I include the hook = make_dict_unstructure_fn(Toy, c, _cattrs_omit_if_default=True) logic in addition to the subclass logic? I can put together some example code of what I'm looking for if you want. Let me know if you'd prefer that to be a separate issue too.

Thanks again, Tin!

from cattrs.

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.