Skip to content

Commit

Permalink
!squash more
Browse files Browse the repository at this point in the history
  • Loading branch information
tony committed Feb 5, 2024
1 parent cc8ed2f commit 008ee4d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 28 deletions.
78 changes: 51 additions & 27 deletions src/unihan_etl/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,62 @@ class kTGHZ2013Location(pydantic.BaseModel):
position: int
# 0 for a main entry and greater than 0 for a parenthesized or bracketed variant #
# of the main entry
entry_type: int
entry_type: int = pydantic.Field(
description="0 for a main entry and greater than 0 for a parenthesized or bracketed variant of the main entry"
)


class kTGHZ2013(UCNBaseModel):
class kTGHZ2013Reading(pydantic.BaseModel):
"""kTGHZ2013 model."""

reading: str
locations: list[kTGHZ2013Location]

model_config = pydantic.ConfigDict(validate_assignment=True)

# @classmethod
# def validator(cls, raw: str) -> "kTGHZ2013":
# if len(raw) == 1:
# ucn, field, val = raw[0].split("\t")
# print(ucn, field, val)
# out = expand_kTGHZ2013([val])
#
# return cls(
# ucn=ucn, reading=out[0]["reading"], locations=out[0]["locations"]
# )
#

def __init__(self, *args, **kwargs) -> None:
if args and len(args) == 1:
ucn, field, val = args[0].split("\t")
print(ucn, field, val)

out = expand_kTGHZ2013([val])

return super().__init__(
ucn=ucn, reading=out[0]["reading"], locations=out[0]["locations"]
)

return super().__init__(*args, **kwargs)
class kTGHZ2013(UCNBaseModel):
"""kTGHZ2013 model."""

readings: list[kTGHZ2013Reading]

model_config = pydantic.ConfigDict(
validate_assignment=True,
arbitrary_types_allowed=True,
)

@classmethod
def from_string(cls, value: str) -> "kTGHZ2013":
"""Accept csv valdation from UNIHAN."""
if isinstance(value, str):
ucn, field, val = value.split("\t")
outs = expand_kTGHZ2013(val.split(" "))

return cls(
ucn=ucn,
readings=[
kTGHZ2013Reading(
reading=out["reading"],
locations=[
kTGHZ2013Location(
page=loc["page"],
position=loc["position"],
entry_type=loc["entry_type"],
)
for loc in out["locations"]
],
)
for out in outs
],
)
# reading=out["reading"],
# locations=[
# kTGHZ2013Location(
# page=location["page"],
# position=location["position"],
# entry_type=location["entry_type"],
# )
# for location in out["locations"]
# ],
# )
elif isinstance(value, dict):
return pydantic.parse_obj_as(cls, value)
raise pydantic.ValidationError("Invalid input for kTGHZ2013 model.") # noqa: TRY003
9 changes: 8 additions & 1 deletion tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,12 @@


def test_kTGHZ2013() -> None:
model = validator.kTGHZ2013("U+3447 kTGHZ2013 482.140:zhòu")
model = validator.kTGHZ2013.from_string("U+3447 kTGHZ2013 482.140:zhòu")
assert model.ucn == "U+3447"

model = validator.kTGHZ2013.from_string(
"U+4E07 kTGHZ2013 256.090:mò 379.160:wàn"
)
assert model.ucn == "U+4E07"

print(f"\n{model}\n")

0 comments on commit 008ee4d

Please sign in to comment.