Skip to content

Commit 43bf78e

Browse files
committed
Add __contains__ method
1 parent a3df330 commit 43bf78e

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

python/pycrdt/array.py

+3
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ def __getitem__(self, key: int) -> BaseType:
162162
def __iter__(self):
163163
return ArrayIterator(self)
164164

165+
def __contains__(self, item: Any) -> bool:
166+
return item in iter(self)
167+
165168
def __str__(self) -> str:
166169
with self.doc.transaction() as txn:
167170
return self.integrated.to_json(txn._txn)

python/pycrdt/map.py

+3
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ def __setitem__(self, key: str, value: Any) -> None:
9292
def __iter__(self):
9393
return self.keys()
9494

95+
def __contains__(self, item: str) -> bool:
96+
return item in self.keys()
97+
9598
def get(self, key: str, default_value: Any | None = None) -> Any | None:
9699
with self.doc.transaction():
97100
if key in self.keys():

python/pycrdt/text.py

+21
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def _init(self, value: str | None) -> None:
3737
def _get_or_insert(self, name: str, doc: Doc) -> _Text:
3838
return doc._doc.get_or_insert_text(name)
3939

40+
def __iter__(self):
41+
return TextIterator(self)
42+
43+
def __contains__(self, item: Any) -> bool:
44+
return item in str(self)
45+
4046
def __len__(self) -> int:
4147
with self.doc.transaction() as txn:
4248
return self.integrated.len(txn._txn)
@@ -129,5 +135,20 @@ class TextEvent(BaseEvent):
129135
__slots__ = "target", "delta", "path"
130136

131137

138+
class TextIterator:
139+
def __init__(self, text: Text):
140+
self.text = text
141+
self.length = len(text)
142+
self.idx = 0
143+
144+
def __next__(self):
145+
if self.idx == self.length:
146+
raise StopIteration
147+
148+
res = self.text[self.idx]
149+
self.idx += 1
150+
return res
151+
152+
132153
base_types[_Text] = Text
133154
event_types[_TextEvent] = TextEvent

0 commit comments

Comments
 (0)