Skip to content

Commit 72cca46

Browse files
committed
Add __contains__ method
1 parent a3df330 commit 72cca46

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-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 list(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

+6
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 iter(str(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)

0 commit comments

Comments
 (0)