21 lines
488 B
Python
21 lines
488 B
Python
|
import dataclasses
|
||
|
|
||
|
|
||
|
@dataclasses.dataclass(init=False)
|
||
|
class DataclassBase:
|
||
|
"""
|
||
|
It just works™
|
||
|
"""
|
||
|
|
||
|
def __init__(self, **kwargs):
|
||
|
names = set([f.name for f in dataclasses.fields(self)])
|
||
|
for k, v in kwargs.items():
|
||
|
if k in names:
|
||
|
setattr(self, k, v)
|
||
|
|
||
|
def dict(self):
|
||
|
return {
|
||
|
k: v.dict() if isinstance(v, DataclassBase) else v
|
||
|
for k, v in dataclasses.asdict(self).items()
|
||
|
}
|