-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from KNMI/KDP-1761
KDP-1761: Upgrade to Pydantic v2
- Loading branch information
Showing
14 changed files
with
185 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
import datetime | ||
from datetime import datetime | ||
from datetime import timezone | ||
|
||
from covjson_pydantic.coverage import Coverage | ||
from covjson_pydantic.domain import Axes | ||
from covjson_pydantic.domain import Domain | ||
from covjson_pydantic.domain import DomainType | ||
from covjson_pydantic.domain import ValuesAxis | ||
from covjson_pydantic.ndarray import NdArray | ||
from pydantic import AwareDatetime | ||
|
||
c = Coverage( | ||
domain=Domain( | ||
domainType="PointSeries", | ||
axes={ | ||
"x": {"dataType": "float", "values": [1.23]}, | ||
"y": {"values": [4.56]}, | ||
"t": {"dataType": "datetime", "values": [datetime.datetime.now()]}, | ||
}, | ||
domainType=DomainType.point_series, | ||
axes=Axes( | ||
x=ValuesAxis[float](values=[1.23]), | ||
y=ValuesAxis[float](values=[4.56]), | ||
t=ValuesAxis[AwareDatetime](values=[datetime.now(tz=timezone.utc)]), | ||
), | ||
), | ||
ranges={"temperature": NdArray(axisNames=["x", "y", "t"], shape=[1, 1, 1], values=[42.0])}, | ||
) | ||
|
||
print(c.json(exclude_none=True, indent=True)) | ||
print(c.model_dump_json(exclude_none=True, indent=4)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,13 @@ | ||
import orjson | ||
from pydantic import BaseModel as PydanticBaseModel | ||
from pydantic import Extra | ||
|
||
|
||
def orjson_dumps(v, *, default, indent=None, sort_keys=False): | ||
options = orjson.OPT_NON_STR_KEYS | orjson.OPT_UTC_Z | orjson.OPT_NAIVE_UTC | ||
if indent: | ||
options |= orjson.OPT_INDENT_2 | ||
|
||
if sort_keys: | ||
options |= orjson.OPT_SORT_KEYS | ||
|
||
# orjson.dumps returns bytes, to match standard json.dumps we need to decode | ||
return orjson.dumps(v, default=default, option=options).decode() | ||
|
||
|
||
class BaseModel(PydanticBaseModel): | ||
class Config: | ||
anystr_strip_whitespace = True | ||
min_anystr_length = 1 | ||
extra = Extra.forbid | ||
validate_all = True | ||
validate_assignment = True | ||
|
||
json_loads = orjson.loads | ||
json_dumps = orjson_dumps | ||
|
||
|
||
class CovJsonBaseModel(BaseModel): | ||
class Config: | ||
extra = Extra.allow # allow custom members | ||
from pydantic import ConfigDict | ||
|
||
|
||
class CovJsonBaseModel(PydanticBaseModel): | ||
model_config = ConfigDict( | ||
str_strip_whitespace=True, | ||
str_min_length=1, | ||
extra="forbid", | ||
validate_default=True, | ||
validate_assignment=True, | ||
strict=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.