Last active
November 13, 2021 02:20
-
-
Save podhmo/dc07d6020ac9d60a958fb059a2b2ca3f to your computer and use it in GitHub Desktop.
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
from __future__ import annotations | |
import typing as t | |
import dataclasses | |
import json | |
@dataclasses.dataclass | |
class Person: | |
name: str | |
age: int | |
father: t.Optinal[Person] = None | |
s = """ | |
{"name": "foo", "age": 20, "father": {"name": "bar", "age": 40}} | |
""" | |
d = json.loads(s) # nestした表現がダメ | |
print(Person(**d)) | |
s = """ | |
{"name": "foo"} | |
""" | |
d = json.loads(s) | |
print(Person(**d)) |
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
from __future__ import annotations | |
import typing as t | |
import dataclasses | |
from dataclass_type_validator import dataclass_validate | |
import json | |
@dataclass_validate | |
@dataclasses.dataclass | |
class Person: | |
name: str | |
age: int | |
father: t.Optinal[Person] = None | |
s = """ | |
{"name": "foo", "age": 20, "father": {"name": "bar", "age": 40}} | |
""" | |
d = json.loads(s) # nestした表現がダメ | |
print(Person(**d)) | |
s = """ | |
{"name": "foo"} | |
""" | |
d = json.loads(s) | |
print(Person(**d)) |
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
# from __future__ import annotations | |
import typing as t | |
import dataclasses | |
import marshmallow_dataclass | |
import json | |
@dataclasses.dataclass | |
class Person: | |
name: str | |
age: int | |
father: t.Optional["Person"] = None | |
PersonSchema = marshmallow_dataclass.class_schema(Person) | |
s = """ | |
{"name": "foo", "age": 20, "father": {"name": "bar", "age": 40}} | |
""" | |
d = json.loads(s) | |
print(PersonSchema().load(d)) | |
s = """ | |
{"name": "foo"} | |
""" | |
d = json.loads(s) | |
print(PersonSchema().load(d)) |
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
from __future__ import annotations | |
import typing as t | |
import pydantic | |
import json | |
class Person(pydantic.BaseModel): | |
name: str | |
age: int | |
father: t.Optional[Person] = None | |
Person.update_forward_refs() | |
s = """ | |
{"name": "foo", "age": 20, "father": {"name": "bar", "age": 40}} | |
""" | |
print(Person.parse_raw(s)) | |
s = """ | |
{"name": "foo"} | |
""" | |
d = json.loads(s) | |
try: | |
print(Person(**d)) | |
except pydantic.ValidationError as err: | |
print(err.json()) | |
raise |
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
from __future__ import annotations | |
import typing as t | |
import pydantic.dataclasses as dataclasses | |
import json | |
@dataclasses.dataclass | |
class Person: | |
name: str | |
age: int | |
father: t.Optional[Person] = None | |
Person.__pydantic_model__.update_forward_refs() | |
s = """ | |
{"name": "foo", "age": 20, "father": {"name": "bar", "age": 40}} | |
""" | |
d = json.loads(s) | |
print(Person(**d)) | |
s = """ | |
{"name": "foo"} | |
""" | |
d = json.loads(s) | |
print(Person(**d)) |
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
from __future__ import annotations | |
import typesystem | |
import json | |
definitions = typesystem.SchemaDefinitions() | |
class Person(typesystem.Schema, definitions=definitions): | |
name = typesystem.String() | |
age = typesystem.Integer() | |
father = typesystem.Reference(to="Person", allow_null=True) | |
s = """ | |
{"name": "foo", "age": 20, "father": {"name": "bar", "age": 40}} | |
""" | |
d = json.loads(s) | |
print(Person.validate(d)) | |
s = """ | |
{"name": "foo"} | |
""" | |
try: | |
d = typesystem.validate_json(s, Person) | |
print(d) | |
except typesystem.ValidationError as e: | |
for message in e.messages(): | |
line_no = message.start_position.line_no | |
column_no = message.start_position.column_no | |
print(f" Error {message.text!r} at line {line_no}, column {column_no}.") |
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
PYTHON ?= .venv/bin/python | |
00: $(PYTHON) | |
$(PYTHON) $(shell echo $@*.py) | |
01: $(PYTHON) | |
$(PYTHON) $(shell echo $@*.py) | |
02: $(PYTHON) | |
$(PYTHON) $(shell echo $@*.py) | |
03: $(PYTHON) | |
$(PYTHON) $(shell echo $@*.py) | |
04: $(PYTHON) | |
$(PYTHON) $(shell echo $@*.py) | |
05: $(PYTHON) | |
$(PYTHON) $(shell echo $@*.py) | |
.venv/bin/python: | |
python -m venv .venv | |
.venv/bin/pip install -U pip wheel | |
$(MAKE) install | |
install: $(PYTHON) | |
$(PYTHON) -m pip install -r requirements.txt | |
.PHONY: install |
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
dataclass-type-validator | |
marshmallow_dataclass | |
pydantic | |
typesystem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment