Comments (5)
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.
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.
Can you give me a more complex example?
from cattrs.
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.
Hi Tin,
Is there anything else I should add or is it just a busy schedule?
from cattrs.
Related Issues (20)
- Constructor selection for structuring HOT 7
- Tagged Union: How to do structure/unstructure hook(s) for particular member type? HOT 4
- A dictionary gets structured into a list without errors/warnings HOT 2
- Question: Correct use of converters HOT 3
- Documentation does not explain unstruct_collection_overrides' input HOT 4
- Detailed validation exception groups with hook errors HOT 2
- cattrs effectively disables attrs validation HOT 3
- Covert to defaultdict instead of plain dict HOT 4
- Dataclass structuring fails with misleading error message.
- Default field conversion conflicts with user-defined field converter function HOT 3
- [macOS] Error when trying to run tests: `ImportError: cannot import name 'CodecOptions' from 'bson'` HOT 2
- How to hook into structuring of a simple dict? HOT 6
- Incorrect dispatch in Python 3.12 HOT 4
- Why does structuring of datetime in attrs class need a structure hook? HOT 1
- `prefer_attrib_converters` doesn't work when the field is aliased HOT 2
- Register structure hook only for optional types HOT 2
- TYPE_CHECKING and init=False HOT 5
- Problem with tagged union example HOT 3
- Subclass disambiguation for nested structures. HOT 3
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 cattrs.