Skip to content

Make marks hashable and comparable #8

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

Merged
merged 2 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
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
44 changes: 43 additions & 1 deletion parse_document_model/marks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,34 @@ class Color(BaseModel):
g: int
b: int

def __eq__(self, other: Any) -> bool:
return (isinstance(other, Color) and
self.id == other.id and
self.r == other.r and
self.g == other.g and
self.b == other.b)

def __hash__(self) -> int:
return hash((self.id, self.r, self.g, self.b))


class Font(BaseModel):
id: str
name: str
size: int

def __eq__(self, other: Any) -> bool:
return (isinstance(other, Font) and
self.id == other.id and
self.size == other.size and
self.name == other.name)

def __hash__(self) -> int:
return hash((self.id, self.size, self.name))


class Mark(BaseModel):
category: Literal['bold', 'italic', 'textStyle', 'link']
category: Literal['bold', 'italic', 'superscripted', 'serifed', 'monospaced', 'textStyle', 'link']

@model_validator(mode='before')
def check_details(self: Any) -> Any:
Expand All @@ -37,11 +56,34 @@ def check_details(self: Any) -> Any:
raise ValueError('textStyle should not be provided when type is link')
return self

def __eq__(self, other):
return isinstance(other, Mark) and self.category == other.category

def __hash__(self) -> int:
return hash(self.category)


class TextStyleMark(Mark):
color: Optional[Color] = None
font: Optional[Font] = None

def __eq__(self, other):
return (isinstance(other, TextStyleMark) and
self.category == other.category and
self.color == other.color and
self.font == other.font)

def __hash__(self) -> int:
return hash((self.category, self.color, self.font))


class UrlMark(Mark):
url: str

def __eq__(self, other):
return (isinstance(other, UrlMark) and
self.category == other.category and
self.url == other.url)

def __hash__(self) -> int:
return hash((self.category, self.url))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='parse-document-model',
version='0.2.1',
version='0.2.2',
description='Pydantic models for representing a text document as a hierarchical structure.',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down