GithubHelp home page GithubHelp logo

Comments (5)

Tinche avatar Tinche commented on June 26, 2024

Here's the problem:

conv.register_unstructure_hook(
    Frame,
    lambda obj: {"to_add": "something special", **conv.unstructure_attrs_asdict(obj)},
)

If you call into conv.unstructure_attrs_asdict directly your _get_unstructure_without_nones won't get called (that function will simply do all the work).

Try this instead:

base_frame_hook = _get_unstructure_without_nones(Frame)
conv.register_unstructure_hook(
    Frame, lambda obj: {"to_add": "something special", **base_frame_hook(obj)}
)

from cattrs.

kkg-else42 avatar kkg-else42 commented on June 26, 2024

Greatly appreciated help at light speed!

What would it look like if the thing is a bit more complex? E.g. multiple hook factories and separate helpers for the hooks instead of an inline lambda definition.
I would be reluctant to couple the Frame hook with the hook factories.

from cattrs.

Tinche avatar Tinche commented on June 26, 2024

Can you give me a more complex example?

from cattrs.

kkg-else42 avatar kkg-else42 commented on June 26, 2024

Let's take this as a starting point:

from datetime import datetime
from functools import singledispatch
from types import UnionType
from typing import Any, Final, get_origin, get_args

import attrs
from cattrs.gen import make_dict_unstructure_fn
from cattrs.preconf.json import JsonConverter, make_converter
from cattrs.strategies import configure_tagged_union

@attrs.frozen
class Sub:
    foo: str = 'bar'
    # and some more fields (incl. other attrs types)

@attrs.frozen
class A:
    to_keep: str = 'foo'
    to_skip: str|None = None
    sub: Sub = Sub()

@attrs.frozen
class B:
    to_keep: str = 'bar'
    to_skip: str|None = None
    sub: Sub = Sub()

FrameData = dict | A | B

@attrs.frozen
class Frame:
    data: FrameData
    to_skip: str|None = None

_CUSTOMIZED_UNSTRUCTURE_TYPES: Final[set] = {
    datetime,
    Frame,
    Sub,
    # and some more...
}

_TAGGED_UNIONS: Final[set] = {
    FrameData,
    # and some more...
}

_TIMESTAMP_FORMAT: Final[str] = '%Y%m%d_%H%M%S'

conv = make_converter()

def _contains_nonetype(type_):
    if get_origin(type_) is UnionType:
        return type(None) in get_args(type_)
    return type_ is type(None)

def _is_attrs_with_none_defaults(type_: type) -> bool:
    return attrs.has(type_) and any(_contains_nonetype(a.type) and a.default is None
                                    for a in attrs.fields(type_))

def _get_unstructure_without_nones(cls):
        unstructure = make_dict_unstructure_fn(cl=cls, converter=conv)
        fields = [a.name for a in attrs.fields(cls) if _contains_nonetype(a.type) and a.default is None]

        def unstructure_without_nones(obj):
            unstructured = unstructure(obj)
            for field in fields:
                if unstructured[field] is None:
                    unstructured.pop(field)
            return unstructured

        return unstructure_without_nones

@singledispatch
def _unstructure(obj: Any) -> dict | str:
    raise NotImplementedError(f'Unsupported type: {type(obj)}.')

@_unstructure.register
def _(obj: datetime) -> str:
    return obj.strftime(_TIMESTAMP_FORMAT)

@_unstructure.register
def _(obj: Frame) -> dict:
    base_unstructure = _get_unstructure_without_nones(Frame)
    return {'to_add': 'something special', **base_unstructure(obj)}

@_unstructure.register
def _(obj: Sub) -> dict:
    modified_dict = {
        # re-arrange data structure...
    }
    return conv.unstructure(modified_dict)

conv.register_unstructure_hook_factory(predicate=_is_attrs_with_none_defaults, factory=_get_unstructure_without_nones)

for data_type in _CUSTOMIZED_UNSTRUCTURE_TYPES:
    conv.register_unstructure_hook(data_type, lambda obj: _unstructure(obj))

for tagged_union in _TAGGED_UNIONS:
    configure_tagged_union(union=tagged_union, converter=conv, tag_name='_type', tag_generator=lambda t: t.__name__.casefold(), default=dict)

Now I would like to add another hook factory, e.g. the to_camel_case thing from the docs.
How could I integrate this? And later on I might need to add another one (and another one...). ;-)

from cattrs.

kkg-else42 avatar kkg-else42 commented on June 26, 2024

Hi Tin,
Is there anything else I should add or is it just a busy schedule?

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.