Skip to content

Commit af516c2

Browse files
committed
feature: add more pythonic methods
1 parent ebb5cdf commit af516c2

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

openms_python/py_aasequence.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,23 @@ def __mul__(self, times: int) -> Py_AASequence:
308308
def __rmul__(self, times: int) -> Py_AASequence:
309309
"""Support int * Py_AASequence."""
310310
return self.__mul__(times)
311-
311+
def __contains__(self, substring: str) -> bool:
312+
"""Check if substring is in sequence."""
313+
return self.has_substring(substring)
314+
315+
def __hash__(self) -> int:
316+
"""Make sequences hashable for use in sets/dicts."""
317+
return hash(self.sequence)
318+
319+
def __lt__(self, other: Py_AASequence) -> bool:
320+
"""Lexicographic comparison by sequence."""
321+
if not isinstance(other, Py_AASequence):
322+
return NotImplemented
323+
return self.sequence < other.sequence
324+
def count(self, residue: str) -> int:
325+
"""Count occurrences of a residue, to be consistent with str.count()."""
326+
return self._sequence.count(residue)
327+
312328
# ==================== Additional Utilities ====================
313329

314330
def get_mz(self, charge: int) -> float:

0 commit comments

Comments
 (0)