Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add binary bool type #30

Merged
merged 16 commits into from
Nov 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions birdxplorer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,39 @@ def from_str(cls: Type[StrT], v: str) -> StrT:
return TypeAdapter(cls).validate_python(v)


class BinaryBool(str):
@classmethod
def __get_pydantic_core_schema__(
cls: Type["BinaryBool"], _source_type: Any, _handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.no_info_after_validator_function(
cls.validate,
core_schema.str_schema(**cls.__get_extra_constraint_dict__()),
serialization=core_schema.plain_serializer_function_ser_schema(cls.serialize, when_used="json"),
)

@classmethod
def validate(cls: Type["BinaryBool"], v: Any) -> "BinaryBool":
if v not in ["0", "1"]:
raise ValueError("Input should be 0 or 1")
return cls(v)

@classmethod
def __get_extra_constraint_dict__(cls: Type["BinaryBool"]) -> dict[str, Any]:
return {}

def serialize(self) -> str:
return str(self)

@classmethod
def to_bool(cls: Type["BinaryBool"], v: Any) -> bool:
if isinstance(v, str):
if v not in ["0", "1"]:
raise ValueError("Input should be 0 or 1")
return v == "1"
return bool(v)


class UpperCased64DigitsHexadecimalString(BaseString):
"""
>>> UpperCased64DigitsHexadecimalString.from_str("test")
Expand Down Expand Up @@ -469,26 +502,44 @@ class NotesBelievable(str, Enum):
believable_by_many = "BELIEVABLE_BY_MANY"
empty = ""


class NotesClassification(str, Enum):
not_misleading = "NOT_MISLEADING"
misinformed_or_potentially_misleading = "MISINFORMED_OR_POTENTIALLY_MISLEADING"


class NotesHarmful(str, Enum):
little_harm = "LITTLE_HARM"
considerable_harm = "CONSIDERABLE_HARM"
empty = ""


class NotesValidationDifficulty(str, Enum):
easy = "EASY"
challenging = "CHALLENGING"
empty = ""


class Note(BaseModel):
note_id: NoteId
note_author_participant_id: str = Field(pattern=r"^[0-9A-F]{64}$")
created_at_millis: TwitterTimestamp
tweet_id: str = Field(pattern=r"^[0-9]{9,19}$")
believable: NotesBelievable
misleading_other: BinaryBool
misleading_factual_error: BinaryBool
misleading_manipulated_media: BinaryBool
misleading_outdated_information: BinaryBool
misleading_missing_important_context: BinaryBool
misleading_unverified_claim_as_fact: BinaryBool
misleading_satire: BinaryBool
not_misleading_other: BinaryBool
not_misleading_factually_correct: BinaryBool
not_misleading_outdated_but_not_when_written: BinaryBool
not_misleading_clearly_satire: BinaryBool
not_misleading_personal_opinion: BinaryBool
trustworthy_sources: BinaryBool
is_media_note: BinaryBool
classification: NotesClassification
harmful: NotesHarmful
validation_difficulty: NotesValidationDifficulty
Expand Down