From 90313689b257c9e5a0a519783d7079d69a1183e5 Mon Sep 17 00:00:00 2001 From: Paul van Schayck Date: Mon, 16 Oct 2023 12:48:56 +0200 Subject: [PATCH] Use Parameters() and DataQueries() in example --- README.md | 14 +++++++------- example.py | 26 +++++++++++++++----------- src/edr_pydantic/collections.py | 5 ++--- src/edr_pydantic/parameter.py | 5 +++++ 4 files changed, 29 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d639ca9..0bcc34d 100644 --- a/README.md +++ b/README.md @@ -38,11 +38,11 @@ pip install git+https://github.com/KNMI/edr-pydantic.git ```python from edr_pydantic.collections import Collection -from edr_pydantic.data_queries import EDRQuery, EDRQueryLink +from edr_pydantic.data_queries import EDRQuery, EDRQueryLink, DataQueries from edr_pydantic.extent import Extent, Spatial from edr_pydantic.link import Link from edr_pydantic.observed_property import ObservedProperty -from edr_pydantic.parameter import Parameter +from edr_pydantic.parameter import Parameters, Parameter from edr_pydantic.unit import Unit from edr_pydantic.variables import Variables @@ -64,8 +64,8 @@ c = Collection( rel="service-doc" ) ], - data_queries={ - 'position': EDRQuery( + data_queries=DataQueries( + position=EDRQuery( link=EDRQueryLink( href="https://example.org/edr/collections/hrly_obs/position?coords={coords}", rel="data", @@ -77,8 +77,8 @@ c = Collection( ) ) ) - }, - parameter_names={ + ), + parameter_names=Parameters({ "Wind Direction": Parameter( unit=Unit( label="degree true" @@ -88,7 +88,7 @@ c = Collection( label="Wind Direction" ) ) - } + }) ) print(c.model_dump_json(indent=2, exclude_none=True)) diff --git a/example.py b/example.py index 6546c73..bb6e17f 100644 --- a/example.py +++ b/example.py @@ -1,4 +1,5 @@ from edr_pydantic.collections import Collection +from edr_pydantic.data_queries import DataQueries from edr_pydantic.data_queries import EDRQuery from edr_pydantic.data_queries import EDRQueryLink from edr_pydantic.extent import Extent @@ -6,6 +7,7 @@ from edr_pydantic.link import Link from edr_pydantic.observed_property import ObservedProperty from edr_pydantic.parameter import Parameter +from edr_pydantic.parameter import Parameters from edr_pydantic.unit import Unit from edr_pydantic.variables import Variables @@ -15,23 +17,25 @@ description="Observation data for UK observing sites", extent=Extent(spatial=Spatial(bbox=[[-15.0, 48.0, 5.0, 62.0]], crs="WGS84")), links=[Link(href="https://example.org/uk-hourly-site-specific-observations", rel="service-doc")], - data_queries={ - "position": EDRQuery( + data_queries=DataQueries( + position=EDRQuery( link=EDRQueryLink( href="https://example.org/edr/collections/hrly_obs/position?coords={coords}", rel="data", variables=Variables(query_type="position", output_formats=["CoverageJSON"]), ) ) - }, - parameter_names={ - "Wind Direction": Parameter( - unit=Unit(label="degree true"), - observedProperty=ObservedProperty( - id="https://codes.wmo.int/common/quantity-kind/_windDirection", label="Wind Direction" - ), - ) - }, + ), + parameter_names=Parameters( + { + "Wind Direction": Parameter( + unit=Unit(label="degree true"), + observedProperty=ObservedProperty( + id="https://codes.wmo.int/common/quantity-kind/_windDirection", label="Wind Direction" + ), + ) + } + ), ) print(c.model_dump_json(indent=2, exclude_none=True)) diff --git a/src/edr_pydantic/collections.py b/src/edr_pydantic/collections.py index a01b11a..50595ed 100644 --- a/src/edr_pydantic/collections.py +++ b/src/edr_pydantic/collections.py @@ -1,4 +1,3 @@ -from typing import Dict from typing import List from typing import Optional @@ -6,7 +5,7 @@ from .data_queries import DataQueries from .extent import Extent from .link import Link -from .parameter import Parameter +from .parameter import Parameters class Collection(EdrBaseModel): @@ -20,7 +19,7 @@ class Collection(EdrBaseModel): # TODO According to req A.13 it shall be CRS object, according to C.1 it is a string array crs: Optional[List[str]] = None output_formats: Optional[List[str]] = None - parameter_names: Dict[str, Parameter] + parameter_names: Parameters # TODO According to req A.13 may have distanceunits. If radius is in link, it shall have distanceunits distanceunits: Optional[List[str]] = None diff --git a/src/edr_pydantic/parameter.py b/src/edr_pydantic/parameter.py index 6a4b9b6..d8da3cb 100644 --- a/src/edr_pydantic/parameter.py +++ b/src/edr_pydantic/parameter.py @@ -1,8 +1,10 @@ +from typing import Dict from typing import List from typing import Literal from typing import Optional from pydantic import model_validator +from pydantic import RootModel from .base_model import EdrBaseModel from .observed_property import ObservedProperty @@ -28,6 +30,9 @@ def must_not_have_unit_if_observed_property_has_categories(self): return self +Parameters = RootModel[Dict[str, Parameter]] + + class ParameterGroup(EdrBaseModel, extra="allow"): type: Literal["ParameterGroup"] = "ParameterGroup" id: Optional[str] = None