Type.__and__()#

relationalai
#Type.__and__(self, __value: Any) -> TypeUnion

Supports the & operator for expressing the intersection of two types.

Returns#

A TypeIntersection 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")

# Define some book objects.
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(Fiction, SciFi, Fantasy, title="The Dark Tower", author="Stephen King")


# =======
# EXAMPLE
# =======

# Use the & operator to define a TypeIntersection of SciFi and Fantasy.
SciFiFantasy = SciFi & Fantasy

# You can query a TypeIntersection the same way you query a Type.
with model.query() as select:
    book = SciFiFantasy()
    response = select(book.title, book.author)

# You can also use the & operator to define a TypeIntersection inline.
with model.query() as select:
    book = Book(SciFi & Fantasy)
    response = select(book.title, book.author)

print(response.results)
#              title         author
# 0   The Dark Tower   Stephen King

# Note that Book(SciFi & Fantasy) is equivalent to Book(SciFi, Fantasy).
with model.query() as select:
    book = Book(SciFi, Fantasy)
    response = select(book.title, book.author)

print(response.results)
#              title         author
# 0   The Dark Tower   Stephen King

See Also#