Created
December 29, 2022 16:13
-
-
Save dan-osull/af365fbc6200deb47f35915378912985 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from typing import Self | |
@dataclass | |
class Example: | |
field1: str | |
field2: str | |
@classmethod | |
def from_input_data(cls, data) -> Self: | |
field1 = data.do_something() | |
field2 = data.do_something_else() | |
return cls( | |
**{ | |
key: value | |
for key, value in locals().items() | |
if key in cls.__dataclass_fields__ | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment