Skip to content

Commit

Permalink
converter from json to TypedDict and BaseModel
Browse files Browse the repository at this point in the history
  • Loading branch information
evalott100 committed Jan 9, 2025
1 parent acd40a9 commit 4f40113
Show file tree
Hide file tree
Showing 46 changed files with 2,325 additions and 1,265 deletions.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ dev = [
"numpydoc",

# For schema generation.
"pydantic>=2.6",
"pydantic<3",
"datamodel-code-generator",
]

[project.scripts]
regenerate-schema = "event_model.documents.generate.__main__:main"
regenerate-documents = "event_model.generate.__main__:generate"

[project.urls]
GitHub = "https://github.com/bluesky/event-model"
Expand Down
20 changes: 15 additions & 5 deletions src/event_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@
from .documents.event_descriptor import (
Configuration,
DataKey,
Dtype,
EventDescriptor,
Limits,
LimitsRange,
PerObjectHint,
)
from .documents.event_page import EventPage, PartialEventPage
from .documents.resource import PartialResource, Resource
from .documents.run_start import Calculation, Hints, Projection, Projections, RunStart
from .documents.run_start import (
CalculatedEventProjection,
Calculation,
ConfigurationProjection,
Hints,
LinkedEventProjection,
Projections,
RunStart,
StaticProjection,
)
from .documents.run_stop import RunStop
from .documents.stream_datum import StreamDatum, StreamRange
from .documents.stream_resource import StreamResource
Expand All @@ -67,7 +75,6 @@
"PartialEvent",
"Configuration",
"DataKey",
"Dtype",
"EventDescriptor",
"Limits",
"LimitsRange",
Expand All @@ -78,6 +85,10 @@
"Resource",
"Calculation",
"Hints",
"LinkedEventProjection",
"StaticProjection",
"CalculatedEventProjection",
"ConfigurationProjection",
"Projection",
"Projections",
"RunStart",
Expand Down Expand Up @@ -1821,9 +1832,8 @@ class MismatchedDataKeys(InvalidData):
DocumentNames.resource: "schemas/resource.json",
DocumentNames.stream_datum: "schemas/stream_datum.json",
DocumentNames.stream_resource: "schemas/stream_resource.json",
# DEPRECATED:
DocumentNames.bulk_events: "schemas/bulk_events.json",
DocumentNames.bulk_datum: "schemas/bulk_datum.json",
DocumentNames.bulk_events: "schemas/bulk_events.json",
}
schemas = {}
for name, filename in SCHEMA_NAMES.items():
Expand Down
49 changes: 49 additions & 0 deletions src/event_model/basemodels/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# generated in `event_model/generate`

from typing import Tuple, Type, Union

from .bulk_datum import * # noqa: F403
from .bulk_events import * # noqa: F403
from .datum import * # noqa: F403
from .datum_page import * # noqa: F403
from .event import * # noqa: F403
from .event_descriptor import * # noqa: F403
from .event_page import * # noqa: F403
from .partial_resource import * # noqa: F403
from .resource import * # noqa: F403
from .run_start import * # noqa: F403
from .run_stop import * # noqa: F403
from .stream_datum import * # noqa: F403
from .stream_resource import * # noqa: F403

BasemodelType = Union[
Type[BulkDatum], # noqa: F405,
Type[BulkEvents], # noqa: F405,
Type[Datum], # noqa: F405,
Type[DatumPage], # noqa: F405,
Type[Event], # noqa: F405,
Type[EventDescriptor], # noqa: F405,
Type[EventPage], # noqa: F405,
Type[PartialResource], # noqa: F405,
Type[RunStart], # noqa: F405,
Type[RunStop], # noqa: F405,
Type[StreamDatum], # noqa: F405,
Type[StreamResource], # noqa: F405,
Type[Resource], # noqa: F405,
]

ALL_BASEMODELS: Tuple[BasemodelType, ...] = (
BulkDatum, # noqa: F405
BulkEvents, # noqa: F405
Datum, # noqa: F405
DatumPage, # noqa: F405
Event, # noqa: F405
EventDescriptor, # noqa: F405
EventPage, # noqa: F405
PartialResource, # noqa: F405
RunStart, # noqa: F405
RunStop, # noqa: F405
StreamDatum, # noqa: F405
StreamResource, # noqa: F405
Resource, # noqa: F405
)
32 changes: 32 additions & 0 deletions src/event_model/basemodels/bulk_datum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ruff: noqa
# type: ignore
# generated by datamodel-codegen:
# filename: bulk_datum.json

from __future__ import annotations

from typing import Any, Dict, List

from pydantic import BaseModel, ConfigDict


class BulkDatum(BaseModel):
"""
Document to reference a quanta of externally-stored data
"""

model_config = ConfigDict(
extra="forbid",
)
datum_kwarg_list: List[Dict[str, Any]]
"""
Array of arguments to pass to the Handler to retrieve one quanta of data
"""
resource: str
"""
UID of the Resource to which all these Datum documents belong
"""
datum_ids: List[str]
"""
Globally unique identifiers for each Datum (akin to 'uid' for other Document types), typically formatted as '<resource>/<integer>'
"""
52 changes: 52 additions & 0 deletions src/event_model/basemodels/bulk_events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# ruff: noqa
# type: ignore
# generated by datamodel-codegen:
# filename: bulk_events.json

from __future__ import annotations

from typing import Any, Dict, List, Optional

from pydantic import BaseModel, ConfigDict, Field, RootModel


class BulkEvent(BaseModel):
"""
Document to record a quanta of collected data
"""

model_config = ConfigDict(
extra="forbid",
)
data: Dict[str, Any]
"""
The actual measurement data
"""
timestamps: Dict[str, Any]
"""
The timestamps of the individual measurement data
"""
filled: Optional[Dict[str, Any]] = None
"""
Mapping the keys of externally-stored data to a boolean indicating whether that data has yet been loaded
"""
descriptor: str
"""
UID to point back to Descriptor for this event stream
"""
seq_num: int
"""
Sequence number to identify the location of this Event in the Event stream
"""
time: float
"""
The event time. This maybe different than the timestamps on each of the data entries
"""
uid: str
"""
Globally unique identifier for this Event
"""


class BulkEvents(RootModel[Dict[str, List[BulkEvent]]]):
root: Dict[str, List[BulkEvent]] = Field(..., title="bulk_events")
32 changes: 32 additions & 0 deletions src/event_model/basemodels/datum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ruff: noqa
# type: ignore
# generated by datamodel-codegen:
# filename: datum.json

from __future__ import annotations

from typing import Any, Dict

from pydantic import BaseModel, ConfigDict, Field


class Datum(BaseModel):
"""
Document to reference a quanta of externally-stored data
"""

model_config = ConfigDict(
extra="forbid",
)
datum_id: str = Field(..., title="Datum Id")
"""
Globally unique identifier for this Datum (akin to 'uid' for other Document types), typically formatted as '<resource>/<integer>'
"""
datum_kwargs: Dict[str, Any] = Field(..., title="Datum Kwargs")
"""
Arguments to pass to the Handler to retrieve one quanta of data
"""
resource: str = Field(..., title="Resource")
"""
The UID of the Resource to which this Datum belongs
"""
36 changes: 36 additions & 0 deletions src/event_model/basemodels/datum_page.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# ruff: noqa
# type: ignore
# generated by datamodel-codegen:
# filename: datum_page.json

from __future__ import annotations

from typing import Dict, List

from pydantic import BaseModel, ConfigDict, Field, RootModel


class DataFrameForDatumPage(RootModel[List[str]]):
root: List[str] = Field(..., title="DataFrameForDatumPage")


class DatumPage(BaseModel):
"""
Page of documents to reference a quanta of externally-stored data
"""

model_config = ConfigDict(
extra="forbid",
)
datum_id: DataFrameForDatumPage
"""
Array unique identifiers for each Datum (akin to 'uid' for other Document types), typically formatted as '<resource>/<integer>'
"""
datum_kwargs: Dict[str, List] = Field(..., title="Datum Kwargs")
"""
Array of arguments to pass to the Handler to retrieve one quanta of data
"""
resource: str = Field(..., title="Resource")
"""
The UID of the Resource to which all Datums in the page belong
"""
48 changes: 48 additions & 0 deletions src/event_model/basemodels/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# ruff: noqa
# type: ignore
# generated by datamodel-codegen:
# filename: event.json

from __future__ import annotations

from typing import Any, Dict, Optional, Union

from pydantic import BaseModel, Field


class PartialEvent(BaseModel):
data: Dict[str, Any] = Field(..., title="data")
"""
The actual measurement data
"""
filled: Optional[Dict[str, Union[bool, str]]] = Field(None, title="filled")
"""
Mapping each of the keys of externally-stored data to the boolean False, indicating that the data has not been loaded, or to foreign keys (moved here from 'data' when the data was loaded)
"""
time: float = Field(..., title="time")
"""
The event time. This maybe different than the timestamps on each of the data entries.
"""
timestamps: Dict[str, Any] = Field(..., title="timestamps")
"""
The timestamps of the individual measurement data
"""


class Event(PartialEvent):
"""
Document to record a quanta of collected data
"""

descriptor: str = Field(..., title="descriptor")
"""
UID of the EventDescriptor to which this Event belongs
"""
seq_num: int = Field(..., title="seq_num")
"""
Sequence number to identify the location of this Event in the Event stream
"""
uid: str = Field(..., title="uid")
"""
Globally unique identifier for this Event
"""
Loading

0 comments on commit 4f40113

Please sign in to comment.