ContextSelect.__call__()#

relationalai
#ContextSelect.__call__(*args: Producer) -> Context

Selects the data to be returned in a model.query() block and returns the Context object created by model.query(). The query results are assigned to the Context object’s .results attribute. ContextSelect objects may only be called in a query context.

Parameters#

NameTypeDescription
*argsProducer or Python string, number, date, datetime, or Boolean objects.The objects, properties, and other values to return in query results.

Returns#

A Context object.

Example#

Call a ContextSelect object in a query context to select the data returned by the query:

#import relationalai as rai


# =====
# SETUP
# =====

model = rai.Model("Books")
Book = model.Type("Book")
NonFiction = model.Type("NonFiction")
Fiction = model.Type("Fiction")
Horror = model.Type("Horror")
Fantasy = model.Type("Fantasy")

with model.rule():
    Book.add(Fiction, Horror, title="It", author="Stephen King", year=1986)
    Book.add(Fiction, Fantasy, title="The Dark Tower", author="Stephen King", year=1982)
    Book.add(NonFiction, title="On Writing", author="Stephen King", year=2000)


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

with model.query() as select:  # select is a ContextSelect object.
    book = Book()
    # response is the Context object returned by model.query().
    response = select(book.title, book.author, book.year)

# Query results are assigned to the response.results attribute.
print(response.results)
#             title        author  year
# 0              It  Stephen King  1986
# 1      On Writing  Stephen King  2000
# 2  The Dark Tower  Stephen King  1982


# Rows for each book filtered by the query are returned in the results. This
# means duplicate rows may be returned:

with model.query() as select:
    book = Book()
    response = select(book.author)

# There are two rows for Stephen King in the results because there are three
# books authored by him.
print(response.results)
#          author
# 0  Stephen King
# 1  Stephen King
# 2  Stephen King


# Use select.distinct() to return only unique rows in the results:

with model.query() as select:
    book = Book()
    response = select.distinct(book.author)

print(response.results)
#          author
# 0  Stephen King

You may call a ContextSelect object multiple times, but it must have the same number of arguments each time it is called:

#with model.query() as select:
    select("B", 1)
    # The return value only needs to be assigned on the last call.
    response = select("A", 2)

print(response.results)
#    v  v2
# 0  A   2
# 1  B   1

Each call adds rows to the query results. Calling a ContextSelect object multiple times lets you select data from different logical branches of a query:

#with model.query() as select:
    book = Book()
    with NonFiction(book):
        response = select(book.title, "NonFiction")
    with Fiction(book):
        select(book.title, "Fiction")
    with Horror(book):
        select(book.title, "Horror")
    with Fantasy(book):
        response = select(book.title, "Fantasy")

print(response.results)
#             title           v
# 0              It     Fiction
# 1              It      Horror
# 2      On Writing  NonFiction
# 3  The Dark Tower     Fantasy
# 4  The Dark Tower     Fiction

See Also#