relationalai.dsl.Context
class Context(dynamic: bool = False)
Contexts execute blocks of code written in RelationalAI’s declarative query builder syntax.
You create contexts using Model
methods,
such as Model.query()
and Model.rule()
.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
dynamic | bool | Whether or not the context is dynamic. Dynamic contexts support Python control flow as query builder macros. |
Example
Section titled “Example”You create Context
objects using Model
methods,
such as Model.rule()
and Model.query()
.
Context
objects are context managers
and must be called in a with
statement:
import relationalai as rai
model = rai.Model("myModel")MyType = model.Type("MyType")
# Create a rule context with `model.rule()` that adds an object to `MyType`.with model.rule(): MyType.add(name="my first object")
# Create a query context with `model.query()` to query the model.with model.query() as select: obj = MyType() response = select(obj.name)
print(response.results)# Output:# name# 0 my first object
The following Model
methods all return Context
objects:
Attributes
Section titled “Attributes”Name | Description | Type |
---|---|---|
model | The model for which the context is created. | Model |
results | The results of a query context. | Varies |
.model
Section titled “.model”import relationalai as rai
model = rai.model("people")Person = model.Type("Person")
with model.query() as select: person = Person() response = select(person.name)
# 'response' is a 'Context' object created by 'model.query()'.print(response.model == model)# Output:# True
Calling a ContextSelect
object, like select
in the preceding query, returns its Context
object.
In this case, response
is the Context
object created by model.query()
in the with
statement.
.results
Section titled “.results”Returns the results of a query.
By default, results are returned as a pandas DataFrame.
If the query is called with format="snowpark"
, the results are returned as a Snowpark DataFrame object.
Access .results
after selecting results in a query:
import relationalai as rai
model = rai.Model("people")Person = model.Type("Person")
with model.rule(): Person.add(name="Alice", age="31") Person.add(name="Alice", age="27") Person.add(name="Bob", age="19")
with model.query() as select: person = Person() response = select(person.name, person.age)
print(response.results)# Output:# name age# 0 Alice 27# 1 Alice 31# 2 Bob 19
Calling a ContextSelect
object, like select
in the preceding query, returns its Context
object.
In this case, response
is the Context
object created by model.query()
in the with
statement.
By default, results are in ascending lexicographic order.
In the preceding example, all names beginning with A
come first, followed by names starting with B
, and so on.
If two names are the same, such as the two people named "Alice"
,
the remaining columns are used to determine the sort position of the rows.
In this case, the row for the 27-year-old Alice comes first.
Refer to the pandas docs for details on working with pandas DataFrames.
Methods
Section titled “Methods”Name | Description | Returns |
---|---|---|
.__enter__() | Enters the context. Implements the context manager protocol. | Context |
.__exit__() | Exits the context. Implements the context manager protocol. | None |
.__enter__()
Section titled “.__enter__()”Context.__enter__() -> ContextSelect
Context
objects are context managers.
Although you can call the .__enter__()
method directly, it is typically called by a
with
statement.
In a with
statement, Python calls the context manager’s .__enter__()
method before executing the with
block.
Optionally, you may give the ContextSelect
object returned by .__enter__()
a name
in the as
part of the with
statement.
After the with
block executes,
the with
statement automatically executes the Context.__exit__()
method.
Returns
Section titled “Returns”A ContextSelect
object.
Example
Section titled “Example”You create rule contexts with Model.rule()
and query contexts with Model.query()
.
The Model.query()
context’s .__enter__()
method returns a ContextSelect
object,
typically given the name select
, that you use to select query results:
import relationalai as rai
model = rai.Model("people")Person = model.Type("Person")
# The `with` statement calls the `model.rule()` context's# `.__enter__()` method automatically. The `ContextSelect` object# returned by `.__enter__()` is not typically used in a rule.with model.rule(): Person.add(name="Fred")
# The `with` statement calls the `model.query()` context's# `.__enter__()` method and assigns the `ContextSelect`# object it returns to a Python variable named `select`.with model.query() as select: person = Person() response = select(person.name)
print(response.results)# Output:# name# 0 Fred
Calling select
returns the same Context
object created by
model.query()
in the with
statement.
The results of the query are stored as a pandas DataFrame
and are accessible via the Context.results
attribute.
.__exit__()
Section titled “.__exit__()”Context.__exit__() -> None
Context
objects are context managers.
Although you can call the .__exit__()
method directly, it is typically called by a
with
statement.
In a with
statement, Python calls the context manager’s .__enter__()
method
before executing the with
block.
After the with
block executes, the with
statement automatically executes the .__exit__()
method.
.__exit__()
translates query builder code inside the with
block into a RelationalAI query.
Returns
Section titled “Returns”None
Example
Section titled “Example”The .__exit__()
method is called automatically in a with
statement:
import relationalai as rai
model = rai.Model("people")Person = model.Type("Person")
# The `with` statement calls the `model.rule()` context's `.__exit__()`# method automatically after the `with` block terminates. `.__exit__()`# translates the query builder code into a RelationalAI query but does# not send the query to RelationalAI.with model.rule(): Person.add(name="Fred")
# The `with` statement calls the `model.query()` context's `.__exit__()`# method automatically after the `with` block terminates. `.__exit__()`# translates the query builder code into a RelationalAI query, sends the# query and all rules defined prior to it to RelationalAI, and blocks until# the results are returned.with model.query() as select: person = Person() response = select(person.name)
print(response.results)# Output:# name# 0 Fred