Skip to content

Commit c6933d9

Browse files
authored
Merge pull request #15 from LeonardoLBraga/feature/in-operator
✨ Feature/in operator
2 parents 314b72f + 5617a47 commit c6933d9

File tree

8 files changed

+128
-0
lines changed

8 files changed

+128
-0
lines changed

synalinks/src/backend/common/json_data_model.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ def __rxor__(self, other):
308308
ops.Xor().call(other, self),
309309
)
310310

311+
def __contains__(self, other):
312+
"""Check if the schema of `other` is contained in this one.
313+
314+
Args:
315+
other (SymbolicDataModel | DataModel): The other data model to compare with.
316+
317+
Returns:
318+
(bool): True if all properties of `other` are present in this one.
319+
"""
320+
from synalinks.src.backend.common.json_schema_utils import contains_schema
321+
322+
return contains_schema(self.get_schema(), other.get_schema())
323+
311324
def factorize(self):
312325
"""Factorizes the data model.
313326

synalinks/src/backend/common/json_data_model_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,21 @@ class DocumentGraph(KnowledgeGraph):
286286
obj = relations[0].get_nested_entity("obj")
287287
self.assertTrue(subj.get_schema() == Document.get_schema())
288288
self.assertTrue(obj.get_schema() == Document.get_schema())
289+
290+
def test_contains_json_data_model(self):
291+
class Foo(DataModel):
292+
foo: str
293+
294+
class FooBar(DataModel):
295+
foo: str
296+
bar: str
297+
298+
class Bar(DataModel):
299+
bar: str
300+
301+
foo_json = JsonDataModel(data_model=Foo(foo="a"))
302+
foobar_json = JsonDataModel(data_model=FooBar(foo="a", bar="b"))
303+
bar_json = JsonDataModel(data_model=Bar(bar="c"))
304+
305+
self.assertTrue(foo_json in foobar_json)
306+
self.assertFalse(bar_json in foo_json)

synalinks/src/backend/common/symbolic_data_model.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,19 @@ def __rxor__(self, other):
300300
ops.Xor().symbolic_call(other, self)
301301
)
302302

303+
def __contains__(self, other):
304+
"""Check if the schema of `other` is contained in this one.
305+
306+
Args:
307+
other (SymbolicDataModel | DataModel): The other data model to compare with.
308+
309+
Returns:
310+
(bool): True if all properties of `other` are present in this one.
311+
"""
312+
from synalinks.src.backend.common.json_schema_utils import contains_schema
313+
314+
return contains_schema(self.get_schema(), other.get_schema())
315+
303316
def factorize(self):
304317
"""Factorizes the data model.
305318

synalinks/src/backend/common/symbolic_data_model_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ class Query(DataModel):
4545
f"<SymbolicDataModel schema={standardize_schema(Query.get_schema())}",
4646
repr(x),
4747
)
48+
49+
def test_contains_symbolic_data_model(self):
50+
class Foo(DataModel):
51+
foo: str
52+
53+
class FooBar(DataModel):
54+
foo: str
55+
bar: str
56+
57+
class Bar(DataModel):
58+
bar: str
59+
60+
foo_symbolic = SymbolicDataModel(data_model=Foo)
61+
foobar_symbolic = SymbolicDataModel(data_model=FooBar)
62+
bar_symbolic = SymbolicDataModel(data_model=Bar)
63+
64+
self.assertTrue(foo_symbolic in foobar_symbolic)
65+
self.assertFalse(bar_symbolic in foo_symbolic)

synalinks/src/backend/common/variables.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,19 @@ def get_schema(self):
265265
"""
266266
return self._schema
267267

268+
def __contains__(self, other):
269+
"""Check if the schema of `other` is contained within the schema of this variable.
270+
271+
Args:
272+
other (SymbolicDataModel | DataModel): The data model to compare against this variable's schema.
273+
274+
Returns:
275+
bool: True if `other`'s schema is a subset of this variable's schema.
276+
"""
277+
from synalinks.src.backend.common.json_schema_utils import contains_schema
278+
279+
return contains_schema(self.get_schema(), other.get_schema())
280+
268281
@property
269282
def trainable(self):
270283
"""Whether the variable is trainable."""

synalinks/src/backend/common/variables_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,16 @@ class Instructions(DataModel):
9393
variable_from_dict.get_schema(),
9494
standardize_schema(Instructions.get_schema()),
9595
)
96+
97+
def test_contains_operator(self):
98+
class Foo(DataModel):
99+
foo: str = ""
100+
101+
class FooBar(DataModel):
102+
foo: str = ""
103+
bar: str = ""
104+
105+
variable = Variable(initializer={"foo": "value", "bar": "value2"}, data_model=FooBar)
106+
107+
self.assertTrue(Foo in variable)
108+
self.assertFalse(FooBar in Foo)

synalinks/src/backend/pydantic/core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ def __rxor__(cls, other):
176176
ops.Xor().symbolic_call(other, cls)
177177
)
178178

179+
def __contains__(cls, other):
180+
"""Check if the schema of `other` is contained in this one.
181+
182+
Args:
183+
other (SymbolicDataModel | DataModel): The other data model to compare with.
184+
185+
Returns:
186+
(bool): True if all properties of `other` are present in this one.
187+
"""
188+
from synalinks.src.backend.common.json_schema_utils import contains_schema
189+
190+
return contains_schema(cls.get_schema(), other.get_schema())
191+
179192
def factorize(cls):
180193
"""Factorizes the data model.
181194
@@ -549,6 +562,19 @@ def __rxor__(self, other):
549562
ops.Xor()(other, self),
550563
)
551564

565+
def __contains__(cls, other):
566+
"""Check if the schema of `other` is contained in this one.
567+
568+
Args:
569+
other (SymbolicDataModel | DataModel): The other data model to compare with.
570+
571+
Returns:
572+
(bool): True if all properties of `other` are present in this one.
573+
"""
574+
from synalinks.src.backend.common.json_schema_utils import contains_schema
575+
576+
return contains_schema(cls.get_schema(), other.get_schema())
577+
552578
def get_config(self):
553579
return self.get_json()
554580

synalinks/src/backend/pydantic/core_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,17 @@ class Query(DataModel):
123123
query: str
124124

125125
self.assertFalse(is_meta_class(Query(query="What is the French capital?")))
126+
127+
def test_contains_meta_class(self):
128+
class Foo(DataModel):
129+
foo: str
130+
131+
class FooBar(DataModel):
132+
foo: str
133+
bar: str
134+
135+
class Bar(DataModel):
136+
bar: str
137+
138+
self.assertTrue(Foo in FooBar)
139+
self.assertFalse(Bar in Foo)

0 commit comments

Comments
 (0)