Type.__or__()#
relationalai
#Type.__or__(self, __value: Any) -> TypeUnion
Supports the |
operator for expressing the union of two types.
Returns#
A TypeUnion
object.
Example#
#import relationalai as rai
# =====
# SETUP
# =====
model = rai.Model("BookModel")
# Declare Book, Fiction, NonFiction, Fantasy, and SciFi types.
Book = model.Type("Book")
Fiction = model.Type("Fiction")
NonFiction = model.Type("NonFiction")
Fantasy = model.Type("Fantasy")
SciFi = model.Type("SciFi")
# Add some book instance to the Book type.
with model.rule():
Book.add(Fiction, Fantasy, title="The Hobbit", author="J.R.R. Tolkien")
Book.add(Fiction, SciFi, title="Foundation", author="Isaac Asimov")
Book.add(NonFiction, title="Humble Pi", author="Matt Parker")
# =======
# EXAMPLE
# =======
# Use the | operator to define a TypeUnion of NonFiction and Fantasy.
NonFictionOrFantasy = NonFiction | Fantasy
# You can query a TypeUnion the same way you query a Type.
with model.query() as select:
book = NonFictionOrFantasy()
response = select(book.title, book.author)
print(response.results)
# title author
# 0 Humble Pi Matt Parker
# 1 The Hobbit J.R.R. Tolkien
# You may also use the | operator to define a TypeUnion inline.
with model.query() as select:
book = Book(NonFiction | Fantasy)
response = select(book.title, book.author)
print(response.results)
# title author
# 0 Humble Pi Matt Parker
# 1 The Hobbit J.R.R. Tolkien