Skip to content

py37 dataclass support#125

Merged
astanin merged 1 commit intoastanin:masterfrom
Box-s-ville:my-dataclass
Jun 22, 2022
Merged

py37 dataclass support#125
astanin merged 1 commit intoastanin:masterfrom
Box-s-ville:my-dataclass

Conversation

@cleoold
Copy link
Contributor

@cleoold cleoold commented May 16, 2021

Thanks for your awesome library.

This can help with #46.

Behavioural difference from that asdict() solution: asdict() will try to convert nested dataclasses to dicts as well (creating many copies), and producing different outputs:

>>> from dataclasses import dataclass, asdict
>>> from tabulate import tabulate

>>> @dataclass
... class Mydata:
...     @dataclass
...     class Yolo:
...         x: int
...         y: int
...     name: str
...     yolo: Yolo

>>> d = [Mydata('origin', Mydata.Yolo(0, 0)), Mydata('v', Mydata.Yolo(22, -18))]
>>> print(tabulate(map(asdict, d), headers='keys'))
name    yolo
------  -------------------
origin  {'x': 0, 'y': 0}
v       {'x': 22, 'y': -18}

>>> print(tabulate(d, headers='keys'))
name    yolo
------  ------------------------
origin  Mydata.Yolo(x=0, y=0)
v       Mydata.Yolo(x=22, y=-18)

The latter is more accurate.

@astanin astanin merged commit 08cfeb0 into astanin:master Jun 22, 2022
@astanin astanin mentioned this pull request Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants