Repository navigation
Expand file tree
/
Copy pathmodel.pyi
More file actions
44 lines (36 loc) · 1.46 KB
/
Copy pathmodel.pyi
File metadata and controls
44 lines (36 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from __future__ import annotations
from dataclasses import dataclass
from datetime import datetime
from typing import Dict, List, Optional, cast
{% for relative in schema.relative_imports %}
{{ relative }}
{% endfor %}
@dataclass
class {{ schema.reference.class_name }}:
""" {{ schema.description }} """
{% for property in schema.required_properties + schema.optional_properties %}
{{ property.to_string() }}
{% endfor %}
def to_dict(self) -> Dict:
return {
{% for property in schema.required_properties %}
"{{ property.name }}": self.{{ property.transform() }},
{% endfor %}
{% for property in schema.optional_properties %}
"{{ property.name }}": self.{{ property.transform() }} if self.{{ property.name }} is not None else None,
{% endfor %}
}
@staticmethod
def from_dict(d: Dict) -> {{ schema.reference.class_name }}:
{% for property in schema.required_properties + schema.optional_properties %}
{% if property.constructor_template %}
{% include property.constructor_template %}
{% else %}
{{ property.name }} = {{ property.constructor_from_dict("d") }}
{% endif %}
{% endfor %}
return {{ schema.reference.class_name }}(
{% for property in schema.required_properties + schema.optional_properties %}
{{ property.name }}={{ property.name }},
{% endfor %}
)