2022-07-27 22:44:40 +01:00
|
|
|
import re
|
|
|
|
from dataclasses import dataclass
|
|
|
|
|
|
|
|
|
|
|
|
def to_camel_case(s):
|
2022-07-27 23:19:10 +01:00
|
|
|
return "".join(word.title() for word in s.split("_"))
|
2022-07-27 22:44:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def to_snake_case(s):
|
2022-07-27 23:19:10 +01:00
|
|
|
return re.sub(r"(?<!^)(?=[A-Z])", "_", s).lower()
|
2022-07-27 22:44:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
def as_dataclass(identifier, data):
|
2022-07-28 11:55:05 +01:00
|
|
|
def attrs():
|
2022-07-29 02:42:44 +01:00
|
|
|
return list(to_snake_case(k) for k in data.keys())
|
2022-07-28 11:55:05 +01:00
|
|
|
|
2022-07-27 22:44:40 +01:00
|
|
|
return dataclass(
|
|
|
|
type(
|
|
|
|
f"{identifier}Dataclass",
|
|
|
|
(),
|
2022-07-28 11:55:05 +01:00
|
|
|
{
|
|
|
|
"attrs": attrs,
|
|
|
|
**{to_snake_case(k): v for k, v in data.items()},
|
|
|
|
},
|
2022-07-27 22:44:40 +01:00
|
|
|
)
|
|
|
|
)
|