Skip to content

Commit 85149ee

Browse files
authored
Make marks hashable and comparable (#8)
* Add eq and hash to marks * Bump version
1 parent d9edda6 commit 85149ee

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

parse_document_model/marks.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,34 @@ class Color(BaseModel):
1010
g: int
1111
b: int
1212

13+
def __eq__(self, other: Any) -> bool:
14+
return (isinstance(other, Color) and
15+
self.id == other.id and
16+
self.r == other.r and
17+
self.g == other.g and
18+
self.b == other.b)
19+
20+
def __hash__(self) -> int:
21+
return hash((self.id, self.r, self.g, self.b))
22+
1323

1424
class Font(BaseModel):
1525
id: str
1626
name: str
1727
size: int
1828

29+
def __eq__(self, other: Any) -> bool:
30+
return (isinstance(other, Font) and
31+
self.id == other.id and
32+
self.size == other.size and
33+
self.name == other.name)
34+
35+
def __hash__(self) -> int:
36+
return hash((self.id, self.size, self.name))
37+
1938

2039
class Mark(BaseModel):
21-
category: Literal['bold', 'italic', 'textStyle', 'link']
40+
category: Literal['bold', 'italic', 'superscripted', 'serifed', 'monospaced', 'textStyle', 'link']
2241

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

59+
def __eq__(self, other):
60+
return isinstance(other, Mark) and self.category == other.category
61+
62+
def __hash__(self) -> int:
63+
return hash(self.category)
64+
4065

4166
class TextStyleMark(Mark):
4267
color: Optional[Color] = None
4368
font: Optional[Font] = None
4469

70+
def __eq__(self, other):
71+
return (isinstance(other, TextStyleMark) and
72+
self.category == other.category and
73+
self.color == other.color and
74+
self.font == other.font)
75+
76+
def __hash__(self) -> int:
77+
return hash((self.category, self.color, self.font))
78+
4579

4680
class UrlMark(Mark):
4781
url: str
82+
83+
def __eq__(self, other):
84+
return (isinstance(other, UrlMark) and
85+
self.category == other.category and
86+
self.url == other.url)
87+
88+
def __hash__(self) -> int:
89+
return hash((self.category, self.url))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
setup(
1212
name='parse-document-model',
13-
version='0.2.1',
13+
version='0.2.2',
1414
description='Pydantic models for representing a text document as a hierarchical structure.',
1515
long_description=long_description,
1616
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)