-
Notifications
You must be signed in to change notification settings - Fork 14
/
e06_ngsi_v2_autogenerate_context_data_models.py
69 lines (57 loc) · 2.29 KB
/
e06_ngsi_v2_autogenerate_context_data_models.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
# This example shows how you can automatically build useful data model
# classes from context entity data or json-schema files.
# Afterwards, the generated models can be used for validation purposes.
#
# E.g. the Smart Data Models provide their models in such format.
# https://www.fiware.org/developers/smart-data-models/
# **Note**: Although the generator function in principle works quite well.
# It usage might be limited to uncommon definitions in the provided files or
# rather definitions that the used third-party library is currently not able to
# handle. Unfortunately, is especially happens a lot with the smart data model
# definitions
"""
# ## Import packages
import os
from pathlib import Path
from filip.models.ngsi_v2.context import ContextEntity, NamedContextAttribute
from filip.utils.model_generation import (
create_data_model_file,
create_context_entity_model,
)
# ## Parameters
path = Path(os.getcwd()).joinpath("./data_models")
if __name__ == "__main__":
# ## 1. Create a custom context entity model
# # create an entity that looks like the one you want to creat a model for
attr = NamedContextAttribute(name="someAttr", type="TEXT", value="something")
entity = ContextEntity(id="myId", type="MyType")
entity.add_attributes(attrs=[attr])
# ### 1.1 create the model and write it to a json-schema file
model = create_context_entity_model(
name="MyModel", data=entity.model_dump(), path=path.joinpath("entity.json")
)
# ### 1.2 create the model and write it to a python file
model = create_context_entity_model(
name="MyModel", data=entity.model_dump(), path=path.joinpath("entity.py")
)
# ## 2. Parsing from external resources
#
create_data_model_file(
path=path.joinpath("commons.py"),
url="https://smart-data-models.github.io/data-models/" "common-schema.json",
)
# ## 3. Use generated data models
from examples.ngsi_v2.data_models.commons import (
OpeningHoursSpecificationItem,
time,
DayOfWeek,
datetime,
)
opening_hours = OpeningHoursSpecificationItem(
opens=time(hour=10),
closes=time(hour=19),
dayOfWeek=DayOfWeek.Saturday,
validFrom=datetime(year=2022, month=1, day=1),
)
print(opening_hours.json(indent=2))