Skip to content

Commit 29a4559

Browse files
committed
Match: support comparison operations.
1 parent 1019fc0 commit 29a4559

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

‎language_tool/__init__.py‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def replacement_list(string, sep="#"):
8787
return string.split(sep) if string else []
8888

8989

90+
@total_ordering
9091
class Match:
9192
"""Hold information about where a rule matches text.
9293
"""
@@ -107,14 +108,15 @@ def __init__(self, attrib, text=None):
107108

108109
def __repr__(self):
109110
def _ordered_dict_repr():
110-
slots = list(self._SLOTS.keys())
111-
attrs = slots + list(set(self.__dict__).difference(slots))
111+
slots = list(self._SLOTS)
112+
slots += list(set(self.__dict__).difference(slots))
113+
attrs = [slot for slot in slots
114+
if slot in self.__dict__ and not slot.startswith("_")]
112115
return "{{{}}}".format(
113-
", ".join(
114-
"{!r}: {!r}".format(attr, self.__dict__[attr])
116+
", ".join([
117+
"{!r}: {!r}".format(attr, getattr(self, attr))
115118
for attr in attrs
116-
if attr in self.__dict__ and not attr.startswith("_")
117-
)
119+
])
118120
)
119121

120122
return "{}({})".format(self.__class__.__name__, _ordered_dict_repr())
@@ -135,6 +137,15 @@ def __str__(self):
135137
)
136138
return s
137139

140+
def __eq__(self, other):
141+
return list(self) == list(other)
142+
143+
def __lt__(self, other):
144+
return list(self) < list(other)
145+
146+
def __iter__(self):
147+
return iter(getattr(self, attr) for attr in self._SLOTS)
148+
138149
def __setattr__(self, name, value):
139150
if name in self._SLOTS:
140151
value = self._SLOTS[name](value)
@@ -147,7 +158,11 @@ def __getattr__(self, name):
147158
return self._frompos
148159
elif name == "topos":
149160
return self._topos
150-
return None
161+
elif name not in self._SLOTS:
162+
raise AttributeError(
163+
"{!r} object has no attribute {!r}"
164+
.format(self.__class__.__name__, name)
165+
)
151166

152167
@property
153168
def _frompos(self):

0 commit comments

Comments
 (0)