PaperParser/paper/utils/classes.py

21 lines
488 B
Python
Raw Normal View History

2024-01-31 19:37:01 +10:00
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()
}