Context.__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#
A ContextSelect
object.
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.