Producer.__enter__()#
#Producer.__enter__() -> None
Producer
objects can be used as context managers
in a with
statement
to apply restrictions in a rule or query conditionally.
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 Producer.__exit__()
method.
Returns#
None
.
Example#
You may use Producer
objects in a with
statement to set a property or Type conditionally:
#import relationalai as rai
model = rai.Model("people")
Person = model.Type("Person")
Adult = model.Type("Adult")
with model.rule():
Person.add(name="Fred", age=39)
Person.add(name="Wilma", age=36)
Person.add(name="Pebbles", age=6)
# People who 18 years old or older are adults.
with model.rule():
person = Person()
with person.age >= 18:
person.set(Adult)
with model.query() as select:
adult = Adult()
response = select(adult.name, adult.age)
print(response.results)
# Output:
# name age
# 0 Fred 39
# 1 Wilma 36
The with person.age >= 18
block temporarily restricts person.age
to values greater than or equal to 18.
After Python executes the with
block, the Producer.__exit__()
method is called to remove the restriction.
This allows you to write multiple conditional with
statements
in the same rule or query:
#Child = model.Type("Child")
with model.rule():
person = Person()
with person.age >= 18:
person.set(Adult)
with person.age < 18:
person.set(Child)
with model.query() as select:
child = Child()
response = select(child.name, child.age)
print(response.results)
# Output:
# name age
# 0 Pebbles 6